Repository navigation
Expand file tree
/
Copy pathLSysFractals.py
More file actions
59 lines (47 loc) · 1.49 KB
/
Copy pathLSysFractals.py
File metadata and controls
59 lines (47 loc) · 1.49 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
import turtle
def create_l_system(iters, axiom, rules):
starting_axiom = axiom
if iters == 0:
return starting_axiom
else:
for i in range(iters):
aux = []
for letter in starting_axiom:
if letter in rules:
aux.append(rules[letter])
else:
aux.append(letter)
starting_axiom = "".join(aux)
return starting_axiom
def draw_l_systems(t, instructions, distance, angle):
for letter in instructions:
if letter == "F":
t.forward(distance)
elif letter == "+":
t.right(angle)
elif letter == "-":
t.left(angle)
def fractal(iterations, axiom, rules, angle, length, x_offset, y_offset):
instructions = create_l_system(iterations, axiom, rules)
t = turtle.Turtle()
wn = turtle.Screen()
wn.setup(500, 500)
t.speed(0)
t.up()
t.backward(x_offset)
t.left(90)
t.backward(y_offset)
t.right(90)
t.down()
t.pensize(2)
print(instructions)
draw_l_systems(t, instructions, length, angle)
t.hideturtle()
wn.exitonclick()
if __name__ == "__main__":
# Quadratic SnowFlake
# fractal(4, "F--F", {"F":"F-F+F+F-F"}, 90, 5, 200, 0)
# Koch-Snowflake
fractal(3, "F--F--F", {"F": "F+F--F+F"}, 60, 3, 100, 100)
# Quadratic-Koch-Island
# fractal(2, "F+F+F+F", {"F":"F-F+F+FFF-F-F+F"}, 90, 5, 100, -100)