1- #
2- # Code under the MIT license by Alexander Pruss
3- #
4- # L-system with turtle graphics
5- #
6-
7- import collections
8- import random
9- import mcpi .settings
10- from mcturtle import *
11-
12- def playProgram (s , dictionary ):
13- for c in s :
14- if c in dictionary :
15- dictionary [c ]()
16-
17-
18- def transform (c , t ):
19- if isinstance (t , basestring ):
20- return t
21- else :
22- r = random .random ()
23- for p ,out in t :
24- if r < p :
25- return out
26- r -= p
27- return c
28-
29- def evolve (axiom , rules , levelCount ):
30- for i in range (levelCount ):
31- out = ""
32- for c in axiom :
33- if c in rules :
34- out += transform (c , rules [c ])
35- else :
36- out += c
37- axiom = out
38- return axiom
39-
40-
41- def lsystem (axiom , rules , dictionary , levelCount ):
42- out = evolve (axiom , rules , levelCount )
43- playProgram (out , dictionary )
44-
45- if __name__ == "__main__" :
46- t = Turtle ()
47- t .pendelay (0 )
48- t .penup ()
49- t .turtle (None )
50- t .go (10 )
51- t .verticalangle (90 )
52- t .pendown ()
53- t .penblock (WOOD )
54-
55- # a fairly simple example with rules from http://www.nbb.cornell.edu/neurobio/land/OldStudentProjects/cs490-94to95/hwchen/
56- # rules = {'F':'F[-&<F][<++&F]||F[--&>F][+&F]'}
57- #
58- # angle = 22.5
59- #
60- # dictionary = {
61- # '[': t.push,
62- # ']': t.pop,
63- # 'F': lambda: t.go(5),
64- # '-': lambda: t.yaw(-angle),
65- # '+': lambda: t.yaw(angle),
66- # '&': lambda: t.pitch(angle),
67- # '^': lambda: t.pitch(-angle),
68- # '<': lambda: t.roll(-angle),
69- # '>': lambda: t.roll(angle),
70- # '|': lambda: t.pitch(180)
71- # }
72- #
73- # lsystem('F', rules, dictionary, 3)
74-
75-
76- #
77- # A more complex example with
78- # rules based on http://www.geekyblogger.com/2008/04/tree-and-l-system.html
79- #
80- rules = {'A' : '^f[^^f>>>>>>A]>>>[^^f>>>>>>A]>>>>>[^^f>>>>>>A]' }
81-
82- #randomized version:
83- # rules = {'A': [(0.75,'^f[^^f>>>>>>A]>>>[^^f>>>>>>A]>>>>>[^^f>>>>>>A]'),
84- # (0.25,'^f>>[^^f>>>>>>A]>>>[^^f>>>>>>A]')]}
85-
86- axiom = 'fA'
87- angle = 15
88- thickness = 8
89- length = 10 if mcpi .settings .isPE else 15 ;
90- material = WOOD
91- t .penwidth (thickness )
92- t .penblock (material )
93-
94- stack = []
95- def push ():
96- global length
97- global thickness
98- stack .append ((length ,thickness ))
99- t .push ()
100- thickness = thickness * 0.6
101- length = length * 0.75
102- if thickness < 1 :
103- thickness = 1
104- if length <= 1 :
105- t .penblock (LEAVES_OAK_PERMANENT )
106- t .penwidth (thickness )
107-
108- def pop ():
109- global length
110- global thickness
111- length ,thickness = stack .pop ()
112- t .pop ()
113-
114- dictionary = {
115- '[' : push ,
116- ']' : pop ,
117- '^' : lambda : t .pitch (angle ),
118- '>' : lambda : t .roll (angle ),
119- 'f' : lambda : t .go (length )
120- }
121-
122- lsystem (axiom , rules , dictionary , 9 if mcpi .settings .isPE else 11 )
1+ #
2+ # Code under the MIT license by Alexander Pruss
3+ #
4+ # L-system with turtle graphics
5+ #
6+
7+ import collections
8+ import random
9+ import mcpi .settings
10+ from mcturtle import *
11+
12+ def playProgram (s , dictionary ):
13+ for c in s :
14+ if c in dictionary :
15+ dictionary [c ]()
16+
17+
18+ def transform (c , t ):
19+ if isinstance (t , basestring ):
20+ return t
21+ else :
22+ r = random .random ()
23+ for p ,out in t :
24+ if r < p :
25+ return out
26+ r -= p
27+ return c
28+
29+ def evolveGenerator (axiom ):
30+ for c in axiom :
31+ if c in rules :
32+ yield transform (c , rules [c ])
33+ else :
34+ yield c
35+
36+ def evolve (axiom , rules , levelCount ):
37+ for i in range (levelCount ):
38+ axiom = '' .join (evolveGenerator (axiom ))
39+ return axiom
40+
41+
42+ def lsystem (axiom , rules , dictionary , levelCount ):
43+ out = evolve (axiom , rules , levelCount )
44+ playProgram (out , dictionary )
45+
46+ if __name__ == "__main__" :
47+ t = Turtle ()
48+ t .pendelay (0 )
49+ t .penup ()
50+ t .turtle (None )
51+ t .go (10 )
52+ t .verticalangle (90 )
53+ t .pendown ()
54+ t .penblock (WOOD )
55+
56+ # a fairly simple example with rules from http://www.nbb.cornell.edu/neurobio/land/OldStudentProjects/cs490-94to95/hwchen/
57+ # rules = {'F':'F[-&<F][<++&F]||F[--&>F][+&F]'}
58+ #
59+ # angle = 22.5
60+ #
61+ # dictionary = {
62+ # '[': t.push,
63+ # ']': t.pop,
64+ # 'F': lambda: t.go(5),
65+ # '-': lambda: t.yaw(-angle),
66+ # '+': lambda: t.yaw(angle),
67+ # '&': lambda: t.pitch(angle),
68+ # '^': lambda: t.pitch(-angle),
69+ # '<': lambda: t.roll(-angle),
70+ # '>': lambda: t.roll(angle),
71+ # '|': lambda: t.pitch(180)
72+ # }
73+ #
74+ # lsystem('F', rules, dictionary, 3)
75+
76+
77+ #
78+ # A more complex example with
79+ # rules based on http://www.geekyblogger.com/2008/04/tree-and-l-system.html
80+ #
81+ rules = {'A' : '^f[^^f>>>>>>A]>>>[^^f>>>>>>A]>>>>>[^^f>>>>>>A]' }
82+
83+ #randomized version:
84+ # rules = {'A': [(0.75,'^f[^^f>>>>>>A]>>>[^^f>>>>>>A]>>>>>[^^f>>>>>>A]'),
85+ # (0.25,'^f>>[^^f>>>>>>A]>>>[^^f>>>>>>A]')]}
86+
87+ axiom = 'fA'
88+ angle = 15
89+ thickness = 8
90+ length = 10 if mcpi .settings .isPE else 15 ;
91+ material = WOOD
92+ t .penwidth (thickness )
93+ t .penblock (material )
94+
95+ stack = []
96+ def push ():
97+ global length
98+ global thickness
99+ stack .append ((length ,thickness ))
100+ t .push ()
101+ thickness = thickness * 0.6
102+ length = length * 0.75
103+ if thickness < 1 :
104+ thickness = 1
105+ if length <= 1 :
106+ t .penblock (LEAVES_OAK_PERMANENT )
107+ t .penwidth (thickness )
108+
109+ def pop ():
110+ global length
111+ global thickness
112+ length ,thickness = stack .pop ()
113+ t .pop ()
114+
115+ dictionary = {
116+ '[' : push ,
117+ ']' : pop ,
118+ '^' : lambda : t .pitch (angle ),
119+ '>' : lambda : t .roll (angle ),
120+ 'f' : lambda : t .go (length )
121+ }
122+
123+ lsystem (axiom , rules , dictionary , 9 if mcpi .settings .isPE else 11 )
124+
0 commit comments