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