ยปCore Development>Code coverage>Lib/turtledemo/penrose.py

Python code coverage for Lib/turtledemo/penrose.py

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