-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecursion_graphics.py
More file actions
61 lines (46 loc) · 1.64 KB
/
recursion_graphics.py
File metadata and controls
61 lines (46 loc) · 1.64 KB
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
from turtle import *
# ================================================== Draw Snowflake ===============================================
class SnowFlake:
def __init__(self):
self.turtle = Turtle()
self.turtle.speed('fastest')
self.window = self.turtle.getscreen()
def draw(self, distance, iterations):
for i in range(3):
self.mountain(distance, iterations)
self.turtle.right(120)
def mountain(self, distance, iterations):
if iterations == 0:
self.turtle.forward(distance)
else:
for rotation in [60, -120, 60, 0]:
self.mountain(distance/3, iterations-1)
self.turtle.left(rotation)
snowflake = SnowFlake()
snowflake.draw(300, 3)
snowflake.turtle.hideturtle()
# ==================================================== Draw Maze =================================================
def hilbert_maze(length, iteration, char, first=False):
if first:
length = int(length / (2.4**iteration))
if iteration > 0:
if char == 'a':
seq = "-bF+aFa+Fb-"
else:
seq = "+aF-bFb-Fa+"
for char in seq:
if char == "F":
turtle.backward(length)
elif char == "+":
turtle.left(90)
elif char == "-":
turtle.right(90)
elif char == "a":
hilbert_maze(length, iteration - 1, 'a')
elif char == "b":
hilbert_maze(length, iteration - 1, 'b')
turtle = Turtle()
window = turtle.getscreen()
turtle.speed('fastest')
hilbert_maze(600, 4, 'a', True)
window.exitonclick()