jpayne@68: #!/usr/bin/env python3 jpayne@68: """ xturtle-example-suite: jpayne@68: jpayne@68: xtx_kites_and_darts.py jpayne@68: jpayne@68: Constructs two aperiodic penrose-tilings, jpayne@68: consisting of kites and darts, by the method jpayne@68: of inflation in six steps. jpayne@68: jpayne@68: Starting points are the patterns "sun" jpayne@68: consisting of five kites and "star" jpayne@68: consisting of five darts. jpayne@68: jpayne@68: For more information see: jpayne@68: http://en.wikipedia.org/wiki/Penrose_tiling jpayne@68: ------------------------------------------- jpayne@68: """ jpayne@68: from turtle import * jpayne@68: from math import cos, pi jpayne@68: from time import perf_counter as clock, sleep jpayne@68: jpayne@68: f = (5**0.5-1)/2.0 # (sqrt(5)-1)/2 -- golden ratio jpayne@68: d = 2 * cos(3*pi/10) jpayne@68: jpayne@68: def kite(l): jpayne@68: fl = f * l jpayne@68: lt(36) jpayne@68: fd(l) jpayne@68: rt(108) jpayne@68: fd(fl) jpayne@68: rt(36) jpayne@68: fd(fl) jpayne@68: rt(108) jpayne@68: fd(l) jpayne@68: rt(144) jpayne@68: jpayne@68: def dart(l): jpayne@68: fl = f * l jpayne@68: lt(36) jpayne@68: fd(l) jpayne@68: rt(144) jpayne@68: fd(fl) jpayne@68: lt(36) jpayne@68: fd(fl) jpayne@68: rt(144) jpayne@68: fd(l) jpayne@68: rt(144) jpayne@68: jpayne@68: def inflatekite(l, n): jpayne@68: if n == 0: jpayne@68: px, py = pos() jpayne@68: h, x, y = int(heading()), round(px,3), round(py,3) jpayne@68: tiledict[(h,x,y)] = True jpayne@68: return jpayne@68: fl = f * l jpayne@68: lt(36) jpayne@68: inflatedart(fl, n-1) jpayne@68: fd(l) jpayne@68: rt(144) jpayne@68: inflatekite(fl, n-1) jpayne@68: lt(18) jpayne@68: fd(l*d) jpayne@68: rt(162) jpayne@68: inflatekite(fl, n-1) jpayne@68: lt(36) jpayne@68: fd(l) jpayne@68: rt(180) jpayne@68: inflatedart(fl, n-1) jpayne@68: lt(36) jpayne@68: jpayne@68: def inflatedart(l, n): jpayne@68: if n == 0: jpayne@68: px, py = pos() jpayne@68: h, x, y = int(heading()), round(px,3), round(py,3) jpayne@68: tiledict[(h,x,y)] = False jpayne@68: return jpayne@68: fl = f * l jpayne@68: inflatekite(fl, n-1) jpayne@68: lt(36) jpayne@68: fd(l) jpayne@68: rt(180) jpayne@68: inflatedart(fl, n-1) jpayne@68: lt(54) jpayne@68: fd(l*d) jpayne@68: rt(126) jpayne@68: inflatedart(fl, n-1) jpayne@68: fd(l) jpayne@68: rt(144) jpayne@68: jpayne@68: def draw(l, n, th=2): jpayne@68: clear() jpayne@68: l = l * f**n jpayne@68: shapesize(l/100.0, l/100.0, th) jpayne@68: for k in tiledict: jpayne@68: h, x, y = k jpayne@68: setpos(x, y) jpayne@68: setheading(h) jpayne@68: if tiledict[k]: jpayne@68: shape("kite") jpayne@68: color("black", (0, 0.75, 0)) jpayne@68: else: jpayne@68: shape("dart") jpayne@68: color("black", (0.75, 0, 0)) jpayne@68: stamp() jpayne@68: jpayne@68: def sun(l, n): jpayne@68: for i in range(5): jpayne@68: inflatekite(l, n) jpayne@68: lt(72) jpayne@68: jpayne@68: def star(l,n): jpayne@68: for i in range(5): jpayne@68: inflatedart(l, n) jpayne@68: lt(72) jpayne@68: jpayne@68: def makeshapes(): jpayne@68: tracer(0) jpayne@68: begin_poly() jpayne@68: kite(100) jpayne@68: end_poly() jpayne@68: register_shape("kite", get_poly()) jpayne@68: begin_poly() jpayne@68: dart(100) jpayne@68: end_poly() jpayne@68: register_shape("dart", get_poly()) jpayne@68: tracer(1) jpayne@68: jpayne@68: def start(): jpayne@68: reset() jpayne@68: ht() jpayne@68: pu() jpayne@68: makeshapes() jpayne@68: resizemode("user") jpayne@68: jpayne@68: def test(l=200, n=4, fun=sun, startpos=(0,0), th=2): jpayne@68: global tiledict jpayne@68: goto(startpos) jpayne@68: setheading(0) jpayne@68: tiledict = {} jpayne@68: tracer(0) jpayne@68: fun(l, n) jpayne@68: draw(l, n, th) jpayne@68: tracer(1) jpayne@68: nk = len([x for x in tiledict if tiledict[x]]) jpayne@68: nd = len([x for x in tiledict if not tiledict[x]]) jpayne@68: print("%d kites and %d darts = %d pieces." % (nk, nd, nk+nd)) jpayne@68: jpayne@68: def demo(fun=sun): jpayne@68: start() jpayne@68: for i in range(8): jpayne@68: a = clock() jpayne@68: test(300, i, fun) jpayne@68: b = clock() jpayne@68: t = b - a jpayne@68: if t < 2: jpayne@68: sleep(2 - t) jpayne@68: jpayne@68: def main(): jpayne@68: #title("Penrose-tiling with kites and darts.") jpayne@68: mode("logo") jpayne@68: bgcolor(0.3, 0.3, 0) jpayne@68: demo(sun) jpayne@68: sleep(2) jpayne@68: demo(star) jpayne@68: pencolor("black") jpayne@68: goto(0,-200) jpayne@68: pencolor(0.7,0.7,1) jpayne@68: write("Please wait...", jpayne@68: align="center", font=('Arial Black', 36, 'bold')) jpayne@68: test(600, 8, startpos=(70, 117)) jpayne@68: return "Done" jpayne@68: jpayne@68: if __name__ == "__main__": jpayne@68: msg = main() jpayne@68: mainloop()