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