annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/turtledemo/penrose.py @ 68:5028fdace37b

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 16:23:26 -0400
parents
children
rev   line source
jpayne@68 1 #!/usr/bin/env python3
jpayne@68 2 """ xturtle-example-suite:
jpayne@68 3
jpayne@68 4 xtx_kites_and_darts.py
jpayne@68 5
jpayne@68 6 Constructs two aperiodic penrose-tilings,
jpayne@68 7 consisting of kites and darts, by the method
jpayne@68 8 of inflation in six steps.
jpayne@68 9
jpayne@68 10 Starting points are the patterns "sun"
jpayne@68 11 consisting of five kites and "star"
jpayne@68 12 consisting of five darts.
jpayne@68 13
jpayne@68 14 For more information see:
jpayne@68 15 http://en.wikipedia.org/wiki/Penrose_tiling
jpayne@68 16 -------------------------------------------
jpayne@68 17 """
jpayne@68 18 from turtle import *
jpayne@68 19 from math import cos, pi
jpayne@68 20 from time import perf_counter as clock, sleep
jpayne@68 21
jpayne@68 22 f = (5**0.5-1)/2.0 # (sqrt(5)-1)/2 -- golden ratio
jpayne@68 23 d = 2 * cos(3*pi/10)
jpayne@68 24
jpayne@68 25 def kite(l):
jpayne@68 26 fl = f * l
jpayne@68 27 lt(36)
jpayne@68 28 fd(l)
jpayne@68 29 rt(108)
jpayne@68 30 fd(fl)
jpayne@68 31 rt(36)
jpayne@68 32 fd(fl)
jpayne@68 33 rt(108)
jpayne@68 34 fd(l)
jpayne@68 35 rt(144)
jpayne@68 36
jpayne@68 37 def dart(l):
jpayne@68 38 fl = f * l
jpayne@68 39 lt(36)
jpayne@68 40 fd(l)
jpayne@68 41 rt(144)
jpayne@68 42 fd(fl)
jpayne@68 43 lt(36)
jpayne@68 44 fd(fl)
jpayne@68 45 rt(144)
jpayne@68 46 fd(l)
jpayne@68 47 rt(144)
jpayne@68 48
jpayne@68 49 def inflatekite(l, n):
jpayne@68 50 if n == 0:
jpayne@68 51 px, py = pos()
jpayne@68 52 h, x, y = int(heading()), round(px,3), round(py,3)
jpayne@68 53 tiledict[(h,x,y)] = True
jpayne@68 54 return
jpayne@68 55 fl = f * l
jpayne@68 56 lt(36)
jpayne@68 57 inflatedart(fl, n-1)
jpayne@68 58 fd(l)
jpayne@68 59 rt(144)
jpayne@68 60 inflatekite(fl, n-1)
jpayne@68 61 lt(18)
jpayne@68 62 fd(l*d)
jpayne@68 63 rt(162)
jpayne@68 64 inflatekite(fl, n-1)
jpayne@68 65 lt(36)
jpayne@68 66 fd(l)
jpayne@68 67 rt(180)
jpayne@68 68 inflatedart(fl, n-1)
jpayne@68 69 lt(36)
jpayne@68 70
jpayne@68 71 def inflatedart(l, n):
jpayne@68 72 if n == 0:
jpayne@68 73 px, py = pos()
jpayne@68 74 h, x, y = int(heading()), round(px,3), round(py,3)
jpayne@68 75 tiledict[(h,x,y)] = False
jpayne@68 76 return
jpayne@68 77 fl = f * l
jpayne@68 78 inflatekite(fl, n-1)
jpayne@68 79 lt(36)
jpayne@68 80 fd(l)
jpayne@68 81 rt(180)
jpayne@68 82 inflatedart(fl, n-1)
jpayne@68 83 lt(54)
jpayne@68 84 fd(l*d)
jpayne@68 85 rt(126)
jpayne@68 86 inflatedart(fl, n-1)
jpayne@68 87 fd(l)
jpayne@68 88 rt(144)
jpayne@68 89
jpayne@68 90 def draw(l, n, th=2):
jpayne@68 91 clear()
jpayne@68 92 l = l * f**n
jpayne@68 93 shapesize(l/100.0, l/100.0, th)
jpayne@68 94 for k in tiledict:
jpayne@68 95 h, x, y = k
jpayne@68 96 setpos(x, y)
jpayne@68 97 setheading(h)
jpayne@68 98 if tiledict[k]:
jpayne@68 99 shape("kite")
jpayne@68 100 color("black", (0, 0.75, 0))
jpayne@68 101 else:
jpayne@68 102 shape("dart")
jpayne@68 103 color("black", (0.75, 0, 0))
jpayne@68 104 stamp()
jpayne@68 105
jpayne@68 106 def sun(l, n):
jpayne@68 107 for i in range(5):
jpayne@68 108 inflatekite(l, n)
jpayne@68 109 lt(72)
jpayne@68 110
jpayne@68 111 def star(l,n):
jpayne@68 112 for i in range(5):
jpayne@68 113 inflatedart(l, n)
jpayne@68 114 lt(72)
jpayne@68 115
jpayne@68 116 def makeshapes():
jpayne@68 117 tracer(0)
jpayne@68 118 begin_poly()
jpayne@68 119 kite(100)
jpayne@68 120 end_poly()
jpayne@68 121 register_shape("kite", get_poly())
jpayne@68 122 begin_poly()
jpayne@68 123 dart(100)
jpayne@68 124 end_poly()
jpayne@68 125 register_shape("dart", get_poly())
jpayne@68 126 tracer(1)
jpayne@68 127
jpayne@68 128 def start():
jpayne@68 129 reset()
jpayne@68 130 ht()
jpayne@68 131 pu()
jpayne@68 132 makeshapes()
jpayne@68 133 resizemode("user")
jpayne@68 134
jpayne@68 135 def test(l=200, n=4, fun=sun, startpos=(0,0), th=2):
jpayne@68 136 global tiledict
jpayne@68 137 goto(startpos)
jpayne@68 138 setheading(0)
jpayne@68 139 tiledict = {}
jpayne@68 140 tracer(0)
jpayne@68 141 fun(l, n)
jpayne@68 142 draw(l, n, th)
jpayne@68 143 tracer(1)
jpayne@68 144 nk = len([x for x in tiledict if tiledict[x]])
jpayne@68 145 nd = len([x for x in tiledict if not tiledict[x]])
jpayne@68 146 print("%d kites and %d darts = %d pieces." % (nk, nd, nk+nd))
jpayne@68 147
jpayne@68 148 def demo(fun=sun):
jpayne@68 149 start()
jpayne@68 150 for i in range(8):
jpayne@68 151 a = clock()
jpayne@68 152 test(300, i, fun)
jpayne@68 153 b = clock()
jpayne@68 154 t = b - a
jpayne@68 155 if t < 2:
jpayne@68 156 sleep(2 - t)
jpayne@68 157
jpayne@68 158 def main():
jpayne@68 159 #title("Penrose-tiling with kites and darts.")
jpayne@68 160 mode("logo")
jpayne@68 161 bgcolor(0.3, 0.3, 0)
jpayne@68 162 demo(sun)
jpayne@68 163 sleep(2)
jpayne@68 164 demo(star)
jpayne@68 165 pencolor("black")
jpayne@68 166 goto(0,-200)
jpayne@68 167 pencolor(0.7,0.7,1)
jpayne@68 168 write("Please wait...",
jpayne@68 169 align="center", font=('Arial Black', 36, 'bold'))
jpayne@68 170 test(600, 8, startpos=(70, 117))
jpayne@68 171 return "Done"
jpayne@68 172
jpayne@68 173 if __name__ == "__main__":
jpayne@68 174 msg = main()
jpayne@68 175 mainloop()