if __name__ == '__main__': # demo(); time.sleep(1); goto((0, 0)); clear() seed(None) for n in xrange(1, 13): tracer(True if n < 5 else False) up(); goto((-30 * n, -30 * n)); write('depth=%d' % n, True); down(); if n % 2: left(90) draw(n, False, (24 / n) if n < 8 else 2 if n < 9 else 1); time.sleep(3); reset()
#非再帰のドラゴン曲線 from turtle import * import time from random import seed, random
def edges(n): e = b = 0 for i in xrange(32): j = 1 if n & 2 ** (31 - i) else 0 if j != b: e += 1 b = j return e
def draw(n, d, s): '''n:length, d:direction, s:step''' for i in xrange(n): color((.25 + random() / 2.0, .25 + random() / 2.0, .25 + random() / 2.0)) e = edges(i) % 4 right(((4 + e - d) % 4) * 90) turn(d, e) d = e forward(s)
if __name__ == '__main__': # demo(); time.sleep(1); goto((0, 0)); clear() seed(None) for n in xrange(16): tracer(True if n < 5 else False) up(); goto((0, 0)); write('length=%d' % 2 ** n, True); down(); draw(2 ** n, 0, (100 / (n + 1)) if n < 12 else 2); time.sleep(3); reset()
#ドラゴン曲線(再帰) 丸め誤差のモアレっぽいの出てるけど気にしない from turtle import * import math import time
def draw(n, q, p): if n == 0: up(); goto((int(p[0]), int(p[1]))); down(); goto((int(q[0]), int(q[1]))) else: x1, y1 = p x2, y2 = q dx, dy = x2 - x1, y2 - y1 x0, y0 = (x1 + x2) / 2.0, (y1 + y2) / 2.0 if dy == 0: s = (x0, y1 + dx / 2.0) else: d = dx / float(dy) r = math.sqrt(((y1 - y0) ** 2 + (x1 - x0) ** 2) / (1 + d ** 2)) if dy > 0: r = -r s = (x0 + r, y0 - r * d) draw(n - 1, s, p) draw(n - 1, s, q)
if __name__ == '__main__': for n in xrange(20): q = (128, 128); p = (-128, -128) tracer(False); up(); goto(p); write('depth=%d' % n, True); down(); draw(n, q, p); time.sleep(3); reset()