-
Notifications
You must be signed in to change notification settings - Fork 1
/
fractal_wheat.py
68 lines (56 loc) · 1.33 KB
/
fractal_wheat.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
########################################
# koch_curve.py
# lsystem representation of a koch curve
# author: stphndemos
########################################
import sys
from lsys import gen_lsys
import turtle
segment_length = 10
angle = 25
pos_stack = []
def draw_segment():
turtle.forward(segment_length)
def do_nothing():
pass
def turn_left():
turtle.left(angle)
def turn_right():
turtle.right(angle)
def init_turtle():
turtle.ht()
turtle.up()
turtle.speed(0)
# turtle.setpos(-500, -300)
turtle.left(60)
turtle.colormode(255)
turtle.pencolor((150,150,0))
turtle.down()
def push_pos():
pos_stack.append((turtle.xcor(), turtle.ycor(), turtle.heading()))
def pop_pos():
turtle.up()
x,y,h = pos_stack.pop()
turtle.setpos(x,y)
turtle.seth(h)
turtle.down()
alphabet = { 'F' : draw_segment,
'X' : do_nothing,
'+' : turn_right,
'-' : turn_left,
'[' : push_pos,
']' : pop_pos,
}
axiom = 'X'
rules = { 'F' : 'FF',
'f' : 'FX',
'X' : 'F-[[X]+X]+F[+FX]-X',
'x' : 'F+[[X]-X]-F[-FX]+X',
}
def main():
init_turtle()
gen_lsys(alphabet, axiom, rules, int(sys.argv[1]))
print 'click to exit'
turtle.exitonclick()
if __name__ == '__main__':
main()