View difference between Paste ID: xgp7AnSf and K928gMzK
SHOW: | | - or go back to the newest paste.
1
from turtle import *
2
# L-Systems 0.1.0
3-
#Version 0.0.5 by J.B. Wincek
3+
# By J.B. Wincek
4-
#The next version will be 0.1.0 which will be code clean up and comments
4+
5-
#Changes from version 0.0.4: added in fractal Plant based grammar 
5+
6
# These are the rules,grammars and alphabets for the differ L-Systems
7
# They are organized by group and identified by the capitol letter
8
9
# Algae 
10
axiomA='A'
11
rulesA = {}
12
rulesA['A'] = 'AB'
13
rulesA['B'] = 'A'
14
15
# Algae with the letters switched for testing perposes 
16
axiomB='B'
17
rulesB = {}
18
rulesB['B'] = 'BA'
19
rulesB['A'] = 'B'
20
21
# Kock curve
22-
turtleRulesF['F'] = 'forward(10)'
22+
23-
turtleRulesF['+'] = 'left(90)'
23+
24-
turtleRulesF['-'] = 'right(90)'
24+
25
turtleRulesF = {}
26
turtleRulesF['F'] = 'forward(1)'
27
turtleRulesF['+'] = 'left(85)'
28
turtleRulesF['-'] = 'right(85)'
29
30
# Sierpinkski triangle 
31
axiomS='A'
32
rulesS = {}
33
rulesS['A'] = 'B-A-B'
34
rulesS['B'] = 'A+B+A'
35
turtleRulesS = {}
36
turtleRulesS['A'] = 'forward(3)'
37
turtleRulesS['B'] = 'forward(3)'
38
turtleRulesS['+'] = 'left(60)'
39
turtleRulesS['-'] = 'right(60)'
40
41-
turtleRulesD['F'] = 'forward(3)'
41+
# Dragon curve 
42
axiomD='FX'
43
rulesD={}
44
rulesD['X']='X+YF'
45
rulesD['Y']='FX-Y'
46
turtleRulesD={}
47
turtleRulesD['F'] = 'forward(7)'
48
turtleRulesD['-'] = 'left(90)'
49
turtleRulesD['+'] = 'right(90)'
50
turtleRulesD['X'] = 'nul'
51
turtleRulesD['Y'] = 'nul'
52
53
# Fractal Plant 
54
axiomP='X'
55
positionHash = ()
56
positionHeap = []
57
rulesP={}
58
rulesP['X']='F-[[X]+X]+F[+FX]-X'
59
rulesP['F']='FF'
60
turtleRulesP={}
61
turtleRulesP['F'] = 'forward(3)'
62
turtleRulesP['-'] = 'left(25)'
63
turtleRulesP['+'] = 'right(25)'
64
turtleRulesP['X'] = 'nul'
65
turtleRulesP['['] = """positionHeap.append(hashPosition())"""
66
turtleRulesP[']'] = 'restorePosition(positionHeap.pop())'
67
68
# these two methods are needed for storing the turles state
69
def hashPosition():
70
    output = ()
71
    output = output + pos()
72
    output = (output, heading())
73
    return output
74
def restorePosition(hashed):
75
    pu()
76
    setx(int(hashed[0][0]))
77
    sety(int(hashed[0][1]))
78
    setheading(hashed[1])
79
    pd()
80
    return hashed
81
82
# this applies the rules to the current axiom (the input string)
83
# format compose(<string>, <Dictionary>)
84
def compose(axiom, rules):
85
        output = ""
86
        for i in axiom:
87
            output = output + rules.get(i,i)
88
        return output
89
90
# the meat and potatoes of the genralized L system
91
# format iterate(<string>,<dictionary>,<number>)
92
# pass the starting condition as the axiom to this function
93
def iterate(axiom,rules, times):
94
    output = ''
95
    if times == 0:
96
        output = axiom
97
    else:
98-
#print iterate(axiomA,rulesB,6)
98+
99-
#print iterate(axiomB,rulesA,6)
99+
100-
#directions = iterate(axiomF,rulesF,3)
100+
101-
#print directions
101+
102
#                       <which rule set to use> ,
103
#                       < which ruleset for turtle navigation> ,
104
#                       <how many iterations>)
105
def evaluate(axiom,rules,turtleRules,times):
106
    for words in iterate(axiom,rules,times):
107-
setx(200)
107+
108-
sety(-200)
108+
109-
left(90)
109+
110
111-
color('red','yellow')
111+
112-
#begin_fill()
112+
113
speed(0)
114-
#evaluate(axiomD,rulesD,turtleRulesD,14)
114+
115-
#testString = "newHash = hashPosition()"
115+
setx(-350)
116-
#exec(testString)
116+
sety(-350)
117-
#print newHash
117+
118
# this adjusts the turtles starting angle
119-
evaluate(axiomP,rulesP,turtleRulesP,6)
119+
left(0)
120
pd()
121-
#end_fill()
121+
color('pink','yellow')
122
begin_fill()
123
# these are examples of different rule sets, becareful high numbers will be slow
124
#evaluate(axiomS,rulesS,turtleRulesS,6)
125
evaluate(axiomF,rulesF,turtleRulesF,4)
126
#evaluate(axiomP,rulesP,turtleRulesP,6)
127
128
end_fill()
129
done()
130
131
#special thanks to http://www.4dsolutions.net/ocn/lsystems.html