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