from turtle import * from math import * from time import sleep from random import randint def rot(x,y): return [x * r2 - y * r2 - 70, x * r2 + y * r2 - 155] def fractales_arrondies(n, k, s, a, ra): x0, x1, x2, xd, xa = 0, 0, 0, 0, np [y0, y1, y2, yd, ya] = [np/5]*5 penup() [xe, ye] = rot(x0, y0) goto(xe, ye) pendown() a0 = atan2(ya-yd,xa-xd) l0 = sqrt((xa-xd)**2 + (ya-yd)**2) i = 0 while i <= n**k-1: ll, aa, bb, t1 = l0, a0, 1, i j = k-1 while j >= 0: r = n**j ; t2 = floor(t1/r) aa += a[t2] ; ll *= l[t2] cc = t2 in ra t1 -= t2*r j = -1 if cc else j-1 i += r if cc else 1 x0, x1 = x1, x2 ; y0, y1 = y1, y2 x2 += ll*cos(aa) ; y2 += ll*sin(aa) vx, vy, wx, wy = x1-x0, y1-y0, x2-x1, y2-y1 for k4 in range(s+1): an = pi/2*k4/s ; co, si = cos(an), sin(an) xq = (x0+x2+co*(-wx)+si*vx)/2 yq = (y0+y2+co*(-wy)+si*vy)/2 [xe, ye] = rot(xq, yq) goto(xe,ye) pendown() r2 = sqrt(2)/2 np = 310 n, k, s = 9, 3, 10 l = [1/3]*n a = [0, pi/2, 0, -pi/2, pi, -pi/2, 0, pi/2, 0] while True: reset() hideturtle() speed(0) pensize(2) ra = [] for i in range(n): if randint(0,1): ra.append(i) fractales_arrondies(n, k, s, a, ra) sleep(1)