Debian Codegraph @

Lib/turtledemo/fractalcurves.py

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