-
Notifications
You must be signed in to change notification settings - Fork 0
/
hilbert-curve.py
79 lines (66 loc) · 1.7 KB
/
hilbert-curve.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
# Ashley Kang / Hilbert Curve L-System
'''
Axiom: L
Rule 1: L -> +RF-LFL-FR+
Rule 2: R -> -LF+RFR+FL-
Turn 90 degrees
Graphics encoding in Turtle:
F = move forward
B = move backward
+ = turn right
- = turn left
'''
import turtle
# Define axiom rules
def applyRules(ch):
newStr = ""
if ch == 'L': # Axiom
newStr = '+RF-LFL-FR+' # Rule 1
elif ch == 'R':
newStr = '-LF+RFR+FL-' # Rule 2
else:
newStr = ch # Keep character in string
return newStr
# Produce a new string based on axiom rules
def processString(oldStr):
newStr = ""
for ch in oldStr:
newStr = newStr + applyRules(ch)
return newStr
# Create L-system based on number of iterations and axiom
def createLsystem(numIterations, axiom):
startStr = axiom
endStr = ""
for i in range(numIterations):
endStr = processString(startStr)
startStr = endStr
return endStr
'''
# Test L-system with 2 iterations
print(createLsystem(2, "L"))
# Output: +-LF+RFR+FL-F-+RF-LFL-FR+F+RF-LFL-FR+-F-LF+RFR+FL-+
'''
# Draw L-system based on turtle object, instructions, angle, and distance
def drawLsystem(aTurtle, instructions, angle, distance):
for cmd in instructions:
if cmd == 'F':
aTurtle.forward(distance)
elif cmd == 'B':
aTurtle.backward(distance)
elif cmd == '+':
aTurtle.right(angle)
elif cmd == '-':
aTurtle.left(angle)
def main():
inst = createLsystem(6, "L")
print(inst)
turt = turtle.Turtle()
wn = turtle.Screen()
turt.speed(0)
#turtle.tracer(0.0)
turt.penup()
turt.setposition(-150, 0)
turt.pendown()
drawLsystem(turt, inst, 90, 5)
wn.exitonclick()
main()