2 """ turtle-example-suite:
6 This program draws two fractal-curve-designs:
7 (1) A hilbert curve (in a box)
8 (2) A combination of Koch-curves.
10 The CurvesTurtle class and the fractal-curve-
11 methods are taken from the PythonCard example
12 scripts for turtle-graphics.
15 from time
import sleep
, clock
17 class CurvesTurtle(Pen
):
18 # example derived from
19 # Turtle Geometry: The Computer as a Medium for Exploring Mathematics
20 # by Harold Abelson and Andrea diSessa
22 def hilbert(self
, size
, level
, parity
):
25 # rotate and draw first subcurve with opposite parity to big curve
26 self
.left(parity
* 90)
27 self
.hilbert(size
, level
- 1, -parity
)
28 # interface to and draw second subcurve with same parity as big curve
30 self
.right(parity
* 90)
31 self
.hilbert(size
, level
- 1, parity
)
34 self
.hilbert(size
, level
- 1, parity
)
36 self
.right(parity
* 90)
38 self
.hilbert(size
, level
- 1, -parity
)
39 # a final turn is needed to make the turtle
40 # end up facing outward from the large square
41 self
.left(parity
* 90)
43 # Visual Modeling with Logo: A Structural Approach to Seeing
45 # Koch curve, after Helge von Koch who introduced this geometric figure in 1904
47 def fractalgon(self
, n
, rad
, lev
, dir):
50 # if dir = 1 turn outward
51 # if dir = -1 turn inward
52 edge
= 2 * rad
* math
.sin(math
.pi
/ n
)
56 self
.rt(180 - (90 * (n
- 2) / n
))
58 self
.fractal(edge
, lev
, dir)
60 self
.lt(180 - (90 * (n
- 2) / n
))
66 def fractal(self
, dist
, depth
, dir):
70 self
.fractal(dist
/ 3, depth
- 1, dir)
72 self
.fractal(dist
/ 3, depth
- 1, dir)
74 self
.fractal(dist
/ 3, depth
- 1, dir)
76 self
.fractal(dist
/ 3, depth
- 1, dir)
88 ft
.setpos(-33*size
, -32*size
)
96 ft
.hilbert(size
, 6, 1)
113 res
= "Hilbert: %.2fsec. " % (tb
-ta
)
123 ft
.color("black", "blue")
125 ft
.fractalgon(3, 250, 4, 1)
128 ft
.fractalgon(3, 200, 4, -1)
131 res
+= "Koch: %.2fsec." % (tb
-ta
)
134 if __name__
== '__main__':