-
Notifications
You must be signed in to change notification settings - Fork 0
/
TurtleFractal.py
146 lines (113 loc) · 2.91 KB
/
TurtleFractal.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
import turtle
import re
turtle.mode("logo")
t = turtle.Turtle()
t.ht()
def draw (prodRule, rotate, length):
location = []
heading = []
for i in prodRule:
if i == "G":
t.fd(length)
if i == "F":
t.fd(length)
if i == "-":
t.left(rotate)
if i == "+":
t.right(rotate)
if i == "[":
location.append(t.pos())
heading.append(t.heading())
if i == "]":
t.penup()
t.setpos(location.pop())
t.seth(heading.pop())
t.pendown()
def generateProduction(initial, Frule, Grule, iteration):
for i in range(0, iteration):
new = ''
for j in initial:
if j == "G":
new += Grule
if j == "F":
new += Frule
if j == "[":
new += "["
if j == "]":
new += "]"
if j == "+":
new += "+"
if j == "-":
new += "-"
initial = new
return initial
def test():
Frule = "G[-F]G[+F]F"
Grule = "GG"
initial = "F"
rotate = 45
initial = generateProduction(initial, Frule, Grule, 2)
draw(initial, rotate, 25)
def fractal1():
initial = "G"
rotate = 22.5
Frule = 'FF'
Grule = "F+[[G]-G]-F][-FG]+G"
iterations = 8
for i in range(0, 8):
new = ''
for j in initial:
if j == "G":
new += Grule
if j == "F":
new += Frule
initial = new
def fractal2():
Frule = "FF+[+F-F-F]-[-F+F+F]"
Grule = ""
initial = "F"
rotate = 22.5
iteration = 4
initial = generateProduction(initial, Frule, Grule, iteration)
draw(initial, rotate, 5)
def fractal3():
Frule = "FF"
Grule = "F[+FFG][G]-FG"
initial = "G"
iteration = 6
rotate = 22.5
initial = generateProduction(initial, Frule, Grule, iteration)
draw(initial, rotate, 1)
def fractal4():
Frule = "FF"
Grule = "F[-G]F[+G]-G"
initial = "G"
iteration = 9
rotate = 20
initial = generateProduction(initial, Frule, Grule, iteration)
draw(initial, rotate, 0.25)
def fractal5():
Frule = "FF"
Grule = "F[-G][+G]FG"
def fractal6():
Frule = "FF"
Grule = "FG[-F[G]-G][G+G][+F[G]+G]"
initial = "G"
iteration = 5
rotate = 22.5
initial = generateProduction(initial, Frule, Grule, iteration)
draw(initial, rotate, 0.25)
def fractal7():
Frule = "FFF"
Grule = "G[-G]-FG+[G+G]"
def fractal8():
Frule = "F"
def fractal9():
Frule = "F-F"
def fractal10():
Frule = "FF-F"
def main():
fractal4()
input("Press Enter to continue...")
if __name__ == '__main__':
main()