-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsof.py.old
More file actions
158 lines (122 loc) · 4.01 KB
/
Copy pathsof.py.old
File metadata and controls
158 lines (122 loc) · 4.01 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
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
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""MyLSystem
My implementation of an L-system I've been obsessing with since my
middle school days. I used to call it "Sort of Fractal". :)
"""
# quick and dirty implementation
import turtle
def draw(system, length=20):
# TODO: add docstring -- may raise ValueError or IndexError -- .pop()
#turtle.clear()
#turtle.penup()
#turtle.home()
#turtle.pendown()
turtle.reset()
turtle.hideturtle()
turtle.left(90)
stack = []
for symbol in system:
#print('symbol', symbol)
#print('before pos angle', turtle.pos(), turtle.heading())
if symbol == '0':
# option 1
#turtle.forward(length)
#turtle.dot(3)
# option 2
turtle.pencolor('red')
turtle.forward(length)
turtle.pencolor('black')
elif symbol == 'x':
# my rule: blocked 0 (no longer a 0) -- later info
turtle.forward(length)
elif symbol == '1':
turtle.forward(length)
elif symbol == '[':
stack.append((*turtle.pos(), turtle.heading()))
turtle.left(45)
elif symbol == ']':
x, y, angle = stack.pop() # can raise IndexError
turtle.penup()
turtle.setposition(x, y)
turtle.pendown()
turtle.setheading(angle)
turtle.right(45)
else:
raise ValueError("symbol {} isn't valid".format(symbol))
#print('after pos angle', turtle.pos(), turtle.heading())
#print('stack', stack)
#input()
turtle.hideturtle()
print()
def _step(system):
# TODO: add docstring
print('System: {}'.format(system))
draw(system)
system = _apply_rules(system, rules='mine')
#input('Press ENTER to continue') # TODO: not necessary for last loop
return system
def _apply_wikip_rule(symbol):
# TODO: add docstring
if symbol == '0':
return '1[0]0'
elif symbol == '1':
return '11'
elif symbol == '[':
return '['
elif symbol == ']':
return ']'
else:
raise ValueError("symbol {} isn't valid".format(symbol))
def _apply_my_rule(symbol):
# TODO: add docstring
if symbol == '0':
return '1[0]0'
elif symbol == '1':
return '1'
elif symbol == '[':
return '['
elif symbol == ']':
return ']'
else:
raise ValueError("symbol {} isn't valid".format(symbol))
def _apply_my_rule2(symbol):
# TODO: add docstring
return symbol if symbol == 'x' else _apply_my_rule(symbol)
def _apply_rules(system, rules='wikip'):
# TODO: add docstring
if rules == 'wikip':
return ''.join(list(map(_apply_wikip_rule, system)))
elif rules == 'mine':
return ''.join(list(map(_apply_my_rule, system)))
elif rules == 'mine2':
return ''.join(list(map(_apply_my_rule2, system)))
else:
raise ValueError('unknown rules')
def run(axiom='0', n=None):
# TODO: add docstring
# TODO: improve this bit of code
system = axiom
if n is None:
while True:
system = _step(system)
else:
for _ in range(n):
system = _step(system)
if __name__ == '__main__':
turtle.setup(width=400, height=400, startx=0, starty=0)
turtle.speed(0) # fastest, no animation
turtle.delay(0)
turtle.tracer(0)
#run(n=5)
# example of collision detection (by hand) -- hmmm, collision detection during...
system = '1[1[1[0]0]1[0]x]1[1[x]0]1[0]0'
#draw(system, length=20)
system = _apply_rules(system, rules='mine2')
draw(system, length=20)
# TODO: add save to image, re-implement turtle so it doesn't need to go to screen
screen = turtle.getscreen()
screen.getcanvas().postscript(file='test.eps')
# thanks https://github.com/kvoss/lsystem/blob/master/example-plant.py for this instruction
# https://pypi.org/project/lsystems/
# TODO: can use Tkinter Canvas to get exists of a point for collision test?