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

Python code coverage for Lib/turtledemo/fractalcurves.py

#countcontent
1n/a#!/usr/bin/env python3
2n/a""" turtle-example-suite:
3n/a
4n/a tdemo_fractalCurves.py
5n/a
6n/aThis program draws two fractal-curve-designs:
7n/a(1) A hilbert curve (in a box)
8n/a(2) A combination of Koch-curves.
9n/a
10n/aThe CurvesTurtle class and the fractal-curve-
11n/amethods are taken from the PythonCard example
12n/ascripts for turtle-graphics.
13n/a"""
14n/afrom turtle import *
15n/afrom time import sleep, clock
16n/a
17n/aclass CurvesTurtle(Pen):
18n/a # example derived from
19n/a # Turtle Geometry: The Computer as a Medium for Exploring Mathematics
20n/a # by Harold Abelson and Andrea diSessa
21n/a # p. 96-98
22n/a def hilbert(self, size, level, parity):
23n/a if level == 0:
24n/a return
25n/a # rotate and draw first subcurve with opposite parity to big curve
26n/a self.left(parity * 90)
27n/a self.hilbert(size, level - 1, -parity)
28n/a # interface to and draw second subcurve with same parity as big curve
29n/a self.forward(size)
30n/a self.right(parity * 90)
31n/a self.hilbert(size, level - 1, parity)
32n/a # third subcurve
33n/a self.forward(size)
34n/a self.hilbert(size, level - 1, parity)
35n/a # fourth subcurve
36n/a self.right(parity * 90)
37n/a self.forward(size)
38n/a self.hilbert(size, level - 1, -parity)
39n/a # a final turn is needed to make the turtle
40n/a # end up facing outward from the large square
41n/a self.left(parity * 90)
42n/a
43n/a # Visual Modeling with Logo: A Structural Approach to Seeing
44n/a # by James Clayson
45n/a # Koch curve, after Helge von Koch who introduced this geometric figure in 1904
46n/a # p. 146
47n/a def fractalgon(self, n, rad, lev, dir):
48n/a import math
49n/a
50n/a # if dir = 1 turn outward
51n/a # if dir = -1 turn inward
52n/a edge = 2 * rad * math.sin(math.pi / n)
53n/a self.pu()
54n/a self.fd(rad)
55n/a self.pd()
56n/a self.rt(180 - (90 * (n - 2) / n))
57n/a for i in range(n):
58n/a self.fractal(edge, lev, dir)
59n/a self.rt(360 / n)
60n/a self.lt(180 - (90 * (n - 2) / n))
61n/a self.pu()
62n/a self.bk(rad)
63n/a self.pd()
64n/a
65n/a # p. 146
66n/a def fractal(self, dist, depth, dir):
67n/a if depth < 1:
68n/a self.fd(dist)
69n/a return
70n/a self.fractal(dist / 3, depth - 1, dir)
71n/a self.lt(60 * dir)
72n/a self.fractal(dist / 3, depth - 1, dir)
73n/a self.rt(120 * dir)
74n/a self.fractal(dist / 3, depth - 1, dir)
75n/a self.lt(60 * dir)
76n/a self.fractal(dist / 3, depth - 1, dir)
77n/a
78n/adef main():
79n/a ft = CurvesTurtle()
80n/a
81n/a ft.reset()
82n/a ft.speed(0)
83n/a ft.ht()
84n/a ft.getscreen().tracer(1,0)
85n/a ft.pu()
86n/a
87n/a size = 6
88n/a ft.setpos(-33*size, -32*size)
89n/a ft.pd()
90n/a
91n/a ta=clock()
92n/a ft.fillcolor("red")
93n/a ft.begin_fill()
94n/a ft.fd(size)
95n/a
96n/a ft.hilbert(size, 6, 1)
97n/a
98n/a # frame
99n/a ft.fd(size)
100n/a for i in range(3):
101n/a ft.lt(90)
102n/a ft.fd(size*(64+i%2))
103n/a ft.pu()
104n/a for i in range(2):
105n/a ft.fd(size)
106n/a ft.rt(90)
107n/a ft.pd()
108n/a for i in range(4):
109n/a ft.fd(size*(66+i%2))
110n/a ft.rt(90)
111n/a ft.end_fill()
112n/a tb=clock()
113n/a res = "Hilbert: %.2fsec. " % (tb-ta)
114n/a
115n/a sleep(3)
116n/a
117n/a ft.reset()
118n/a ft.speed(0)
119n/a ft.ht()
120n/a ft.getscreen().tracer(1,0)
121n/a
122n/a ta=clock()
123n/a ft.color("black", "blue")
124n/a ft.begin_fill()
125n/a ft.fractalgon(3, 250, 4, 1)
126n/a ft.end_fill()
127n/a ft.begin_fill()
128n/a ft.color("red")
129n/a ft.fractalgon(3, 200, 4, -1)
130n/a ft.end_fill()
131n/a tb=clock()
132n/a res += "Koch: %.2fsec." % (tb-ta)
133n/a return res
134n/a
135n/aif __name__ == '__main__':
136n/a msg = main()
137n/a print(msg)
138n/a mainloop()