-
Notifications
You must be signed in to change notification settings - Fork 38
/
rainbow_fractals.py
257 lines (225 loc) · 4.6 KB
/
rainbow_fractals.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
from turtle import Turtle, done
import argparse
import random
from multiprocessing import Process
import colorsys
t = Turtle()
FIGURES = ("snowflake", "triangle", "carpet", "cross", "cross2")
col = random.randint(0, 259)
COLOR_STEP = 10
def main(figure, depth):
t.shape("turtle")
t.speed(0) # Max speed
color()
if figure == None:
figure = random.choice(FIGURES)
print(f"No figure specified, randomly choosing {figure}")
if figure == "snowflake":
snowflake(depth, 300);
elif figure == "triangle":
triangle(depth, 300)
elif figure == "carpet":
carpet(depth, 300)
elif figure == "cross":
cross(depth, 300)
elif figure == "cross2":
cross2(depth, 300)
done()
# Use hsv to iterate over color palette
# https://www.alanzucconi.com/2015/09/30/colour-sorting/
def color():
global col
r, g, b = colorsys.hsv_to_rgb(col/360.0,1,1)
t.color(r, g, b)
col = (col + COLOR_STEP + 360) % 360
# Kock snowflake
def snowflake(n, length):
# Go to position
t.penup()
t.goto(-length/2, 0)
t.left(60)
t.pendown()
# Draw triangle
snowflake_edge(n, length/3)
t.right(120)
snowflake_edge(n, length/3)
t.right(120)
snowflake_edge(n, length/3)
def snowflake_edge(n, l):
# End case: just draw a straight line
if n == 1:
color()
t.forward(3 * l)
# Recursive call
elif n > 1:
snowflake_edge(n - 1, l / 3)
t.left(60)
snowflake_edge(n - 1, l / 3)
t.right(120)
snowflake_edge(n - 1, l / 3)
t.left(60)
snowflake_edge(n - 1, l / 3)
# Wrong input
else:
print(f"n must be > 0, got {n}")
def triangle(n, length):
# Go to position
t.penup()
t.goto(-length/2, 0)
t.left(60)
t.pendown()
triangle_r(n, length)
def triangle_r(n, l):
# End case: just fill a triangle
if n == 1:
color()
t.begin_fill()
for _ in range(3):
t.forward(l)
t.right(120)
t.end_fill()
# Recursive call
elif n > 1:
for _ in range(3):
triangle_r(n-1, l/2)
t.penup()
t.forward(l)
t.right(120)
t.pendown()
# Wrong input
else:
print(f"n must be > 0, got {n}")
# Sierpinski carpet
def carpet(n, length):
# Go to position
t.penup()
t.goto(-length/2, length/2)
t.pendown()
carpet_square(n, length)
def carpet_square(n, l):
# End case: fill a black square
if n == 1:
color()
t.begin_fill()
for _ in range(4):
t.forward(l)
t.right(90)
t.end_fill()
# Recursive call
elif n > 1:
for _ in range(4):
carpet_square(n-1, l/3)
t.penup()
t.forward(l/3)
t.pendown()
carpet_square(n-1, l/3)
t.penup()
t.forward(l/3)
t.forward(l/3)
t.right(90)
t.pendown()
# Wrong input
else:
print(f"n must be > 0, got {n}")
# Vicsek cross (diagonal)
def cross(n, length):
# Go to position
t.penup()
t.goto(-length/2, length/2)
t.pendown()
cross_square(n, length)
def cross_square(n, l):
# End case: fill a black square
if n == 1:
color()
t.begin_fill()
for _ in range(4):
t.forward(l)
t.right(90)
t.end_fill()
# Recursive call
elif n > 1:
# 4 external squares
for _ in range(4):
cross_square(n-1, l/3)
t.penup()
t.forward(l)
t.right(90)
t.pendown()
# Central square
t.penup()
t.forward(l/3)
t.right(90)
t.forward(l/3)
t.left(90)
t.pendown()
cross_square(n-1,l/3)
# Go back to initial position
t.penup()
t.left(90)
t.forward(l/3)
t.left(90)
t.forward(l/3)
t.right(180)
# Wrong input
else:
print(f"n must be > 0, got {n}")
# Vicsek cross (vertical)
def cross2(n, length):
# Go to position
t.penup()
t.goto(-length/2, length/2)
t.pendown()
cross2_square(n, length)
def cross2_square(n, l):
# End case: fill a black square
if n == 1:
color()
t.begin_fill()
for _ in range(4):
t.forward(l)
t.right(90)
t.end_fill()
# Recursive call
elif n > 1:
# 4 external squares
for _ in range(4):
t.penup()
t.forward(l/3)
t.pendown()
cross2_square(n-1, l/3)
t.penup()
t.forward(l/3)
t.forward(l/3)
t.right(90)
t.pendown()
# Central square
t.penup()
t.forward(l/3)
t.right(90)
t.forward(l/3)
t.left(90)
t.pendown()
cross2_square(n-1,l/3)
# Go back to initial position
t.penup()
t.left(90)
t.forward(l/3)
t.left(90)
t.forward(l/3)
t.right(180)
# Wrong input
else:
print(f"n must be > 0, got {n}")
if __name__ == '__main__':
# Allow user input
parser = argparse.ArgumentParser()
parser.add_argument('--figure', type=str, default=None, choices=FIGURES, help=f'The fractal figure to draw {FIGURES}')
parser.add_argument('--depth', type=int, default=5, help='The number of recursive calls')
args = parser.parse_args()
# Ensure depth is valid
if args.depth < 1:
print(f"Depth must be >= 1, got {args.depth}")
args.depth = 5
# Main run
main(args.figure, args.depth)