-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL-systems.py
More file actions
69 lines (48 loc) · 1.74 KB
/
L-systems.py
File metadata and controls
69 lines (48 loc) · 1.74 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
62
63
64
65
66
67
68
69
#Name: Ying Zhang
#Lindenmayer Fractals (L-systems)
from turtle import*
bob = Turtle()
screen = bob.getscreen()
seed_string = screen.textinput("Seed String", "Enter seed string: ")
angle = screen.numinput("Angle", "Enter angle: ")
distance = screen.numinput("Distance", "Enter distance: ")
production_rule = screen.numinput("Production Rule", "Enter the number of production rules: ")
target = []
replacement = []
while production_rule >0:
val = screen.textinput("Target Character", "Enter target character: ")
target.append(val)
rep = screen.textinput("Replacement Character", "Enter replacement character: ")
replacement.append(rep)
production_rule-=1
ini_depth = screen.numinput("Depth", "Enter desired depth: ")
bob.speed(0)
bob.penup()
bob.goto(-150,-100) #different starting point to fit prompt image
bob.pendown()
def drawLSystem (string,angle,distance):
global bob
for letter in string:
if letter == "F":
bob.forward(distance)
elif letter == "+":
bob.right(angle)
elif letter == "-":
bob.left(angle)
def productionRules(old_val,new_val): #take in two lists
productionRules = {o:n for o, n in zip(old_val,new_val)}
return productionRules
def deriveLSystem(seed,productions,depth):
if depth <=0:
return seed
else:
result_seed= ""
for char in seed:
if char in productions.keys():
result_seed += productions[char]
else:
result_seed +=char
return deriveLSystem(result_seed,productions,depth-1)
rule = productionRules(target,replacement)
end_string = deriveLSystem(seed_string,rule,ini_depth)
drawLSystem(end_string,angle,distance)