from turtle import * from math import * hideturtle() speed(0) W, M, N, K = 160, 1, 9, 4 L = 1/3, 1/6, 1/3, 1/6, 1/6, 1/3, 1/6, 1/3, 1/3 A = 0, pi / 2, 0, -pi / 2, -pi / 2, -pi, pi / 2, 0, 0 def draw_fractal(): penup() goto(0 - W, 0) pendown() xa, ya = 2 * W, W x0, y0 = 0, W a0 = atan2(ya - y0, xa) i = 0 while i <= N**K - 1: ll, aa, bb, t1 = 2 * W, a0, 1, i for j in range(K): r = N ** (K - j - 1) t2 = t1 // r aa += bb * A[t2] ll *= L[t2] t1 -= t2 * r x0 += ll * cos(aa) y0 += ll * sin(aa) penup() if t2 == 7 else pendown() goto(x0 - W, y0 - W) i += 1 draw_fractal()