/
snowflake.py
147 lines (117 loc) · 2.66 KB
/
snowflake.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
'''
Drawing a Koch snowflake
'''
import turtle as tu
import math
def Koch(length):
"""draw a Koch fractal curve recursively"""
if length <= 3 :
tu.fd(length)
return
Koch(length/3)
tu.lt(60)
Koch(length/3)
tu.rt(120)
Koch(length/3)
tu.lt(60)
Koch(length/3)
return
def Solution(length, draw):
#face up at start
if length >=.1:
#Draw right third
Solution(length/3, 0)
if draw:
tu.dot(8,'red')
#Draw left third
Solution2(length, draw)
return
def Solution2(length, draw):
if draw:
tu.pendown()
else:
tu.penup()
if length >=.1:
#Draw middle third
tu.left(90)
tu.fd(2*length/9)
tu.right(90)
tu.fd(math.sqrt(3.0)/3.0*2.0/9.0 * length)
Solution2(length/3, draw)
def DrawAxes(length):
tu.speed(0)
# move to starting position
tu.penup()
tu.backward(length/2.0)
tu.right(90)
tu.fd(length/3.0)
tu.left(90)
tu.pendown()
#draw axes
tu.left(90)
tu.color('gray')
tu.fd(length)
tu.left(180)
tu.fd(length*1.5)
tu.backward(length*.5)
tu.left(90)
tu.fd(length*1.5)
tu.left(180)
tu.fd(length*.5)
tu.left(90)
tu.fd(10)
tu.backward(20)
tu.fd(10)
tu.right(90)
tu.fd(length*1.5)
tu.backward(length*.5)
tu.left(180)
def DrawArrows(length):
#face up to start
tu.color('black')
DrawArrow(-length/8, -10 -length/3.0, 90)
DrawArrow(0, -10 - length/3.0, 90)
DrawArrow(length/8, -10 - length/3.0, 90)
DrawArrow(-length/4, -40 -length/3.0, 90)
DrawArrow(-length/8, -40 -length/3.0, 90)
DrawArrow(0, -40 - length/3.0, 90)
DrawArrow(length/8, -40 - length/3.0, 90)
DrawArrow(length/4, -40 -length/3.0, 90)
DrawArrow(0, -length/3.0 + length/3.0, 180)
DrawArrow(-length/3.0, -length/3.0 + length/3.0, 180)
DrawArrow(-length/6.0, -length/3.0 + length/3.0, 180)
DrawArrow(length/6.0, -length/3.0 + length/3.0, 180)
DrawArrow(length/3.0, -length/3.0 + length/3.0, 180)
DrawArrow(length/3.0, -length/3.0 + length/4.0, 180)
DrawArrow(-length/3.0, -length/3.0 + length/4.0, 180)
def DrawArrow(xc, yc, head):
tu.seth(head)
tu.penup()
tu.setx(xc)
tu.sety(yc)
tu.pendown()
tu.fd(20)
tu.left(135)
tu.fd(5)
tu.right(180)
tu.fd(5)
tu.right(90)
tu.fd(5)
tu.title('Koch fractal curve')
length = 600.0
DrawAxes(length)
tu.color('black')
Koch(length)
tu.dot(8, 'blue')
'''
tu.left(90)
tu.color('blue')
Solution(length, 1)
tu.dot(8,'red')
tu.left(90)
tu.fd(100)
'''
DrawArrows(length)
tu.penup()
tu.setx(-500)
tu.done()