L-System drawing program.
-- L-System
-- Herwig Van Marck
function setup()
print("L-System\
While title is red the next level is calculated.")
print("Change timeStep to slow down or speed up time to next level.")
print("Double tap to restart")
iparameter("timeStep",0,30,10)
iparameter("example",1,9,9)
time=0
curExample=example
lsys=initExample(curExample)
zoom=Zoom(WIDTH/2,HEIGHT/2)
strokeWidth(2)
noSmooth()
end
function touched(touch)
if (touch.state==BEGAN and touch.tapCount==2) then
lsys=initExample(curExample)
zoom:touched(touch)
else
zoom:touched(touch)
end
end
function draw()
if example~=curExample then
curExample=example
lsys=initExample(curExample)
zoom:clear()
end
if (time>=600) then
time=0
if (lsys.level<lsys.maxlevel) then
lsys:transform()
end
else
time = time + timeStep
end
background(0, 0, 0, 255)
lsys:drawTitle()
zoom:draw()
lsys:draw()
end
function initExample()
if (example==1) then
return LSystem("Plant",40,"NSL",
{
A=Step(Step.LINE,500),
B=Step(Step.LINE,100),
C=Step(Step.LINE,200),
D=Step(Step.LINE,300),
E=Step(Step.LINE,400),
L=Step(Step.LINE,200),
P=Step(Step.ROT,25),
M=Step(Step.ROT,-29),
U=Step(Step.PUSH),
O=Step(Step.POP),
N=Step(Step.TRANS,-250,0),
S=Step(Step.SCALE,0.875)
},
{
Rule("AB","AC"),
Rule("AC","AD"),
Rule("AD","AE"),
Rule("AE","AA"),
Rule("A","AB"),
Rule("BL","CL"),
Rule("CL","DL"),
Rule("DL","EL"),
Rule("L","BL"),
Rule("EL","AUPLOML"),
Rule("NS","NSS")
})
elseif example==2 then
return LSystem("Dragon",14,"NABFX",
{
A=Step(Step.ROT,-45),
B=Step(Step.SCALE,0.75),
N=Step(Step.TRANS,-50,50),
F=Step(Step.LINE,200),
X=Step(Step.NOP),
Y=Step(Step.NOP),
P=Step(Step.ROT,90),
M=Step(Step.ROT,-90)
},
{
Rule("X","XPYF"),
Rule("Y","FXMY"),
Rule("NAB","NABAB")
})
elseif example==3 then
return LSystem("Cantor Dust",8,"NSA",
{
N=Step(Step.TRANS,-250,0),
A=Step(Step.LINE,1500),
B=Step(Step.TRANS,1500,0),
S=Step(Step.SCALE,1/3.0)
},
{
Rule("NS","NSS"),
Rule("A","ABA"),
Rule("B","BBB")
}
)
elseif example==4 then
return LSystem("Koch curve",7,"NSA",
{
N=Step(Step.TRANS,-300,0),
S=Step(Step.SCALE,1/3.0),
A=Step(Step.LINE,1500),
P=Step(Step.ROT,60),
M=Step(Step.ROT,-120)
},
{
Rule("NS","NSS"),
Rule("A","APAMAPA")
}
)
elseif example==5 then
return LSystem("Right angle Koch curve",6,"NSA",
{
N=Step(Step.TRANS,-300,0),
S=Step(Step.SCALE,1/3.0),
A=Step(Step.LINE,1500),
P=Step(Step.ROT,90),
M=Step(Step.ROT,-90)
},
{
Rule("NS","NSS"),
Rule("A","APAMAMAPA")
}
)
elseif example==6 then
return LSystem("Sierpinsky Triangle",9,"NSQA",
{
N=Step(Step.TRANS,-200,-200),
S=Step(Step.SCALE,0.5),
Q=Step(Step.NOP),
R=Step(Step.ROT,60),
A=Step(Step.LINE,800),
B=Step(Step.LINE,800),
P=Step(Step.ROT,60),
M=Step(Step.ROT,-60)
},
{
Rule("NS","NSS"),
Rule("A","BMAMB"),
Rule("B","APBPA"),
Rule("Q","R"),
Rule("R","Q")
}
)
elseif example==7 then
return LSystem("Fractal Plant",7,"NSX",
{
N=Step(Step.TRANS,-300,0),
S=Step(Step.SCALE,0.5),
X=Step(Step.NOP),
F=Step(Step.LINE,500),
M=Step(Step.ROT,-25),
P=Step(Step.ROT,25),
U=Step(Step.PUSH),
O=Step(Step.POP)
},
{
Rule("NS","NSS"),
Rule("X","FMUUXOPXOPFUPFXOMX"),
Rule("F","FF")
}
)
elseif example==8 then
return LSystem("Hilbert curve",7,"NSA",
{
N=Step(Step.TRANS,-200,-200),
Q=Step(Step.TRANS,-100,-100),
S=Step(Step.SCALE,0.5),
A=Step(Step.NOP),
B=Step(Step.NOP),
M=Step(Step.ROT,-90),
P=Step(Step.ROT,90),
F=Step(Step.LINE,800)
},
{
Rule("NS","QSNS"),
Rule("A","PBFMAFAMFBP"),
Rule("B","MAFPBFBPFAM")
}
)
elseif example==9 then
return LSystem("Penrose Tiling",6,"NSUBOPPUBOPPUBOPPUBOPPUBO",
{
N=Step(Step.NOP),
S=Step(Step.SCALE,2.0/3.0),
A=Step(Step.NOP),
B=Step(Step.NOP),
C=Step(Step.NOP),
D=Step(Step.NOP),
F=Step(Step.LINE,200),
U=Step(Step.PUSH),
O=Step(Step.POP),
M=Step(Step.ROT,-36),
P=Step(Step.ROT,36)
},
{
Rule("NS","NSS"),
Rule("A","CFPPDFMMMMBFUMCFMMMMAFOPP"),
Rule("B","PCFMMDFUMMMAFMMBFOP"),
Rule("C","MAFPPBFUPPPCFPPDFOM"),
Rule("D","MMCFPPPPAFUPDFPPPPBFOMMBF"),
Rule("F",""),
Rule("PPPMMM",""),
Rule("MMMPPP",""),
Rule("PPMM",""),
Rule("MMPP",""),
Rule("PM",""),
Rule("MP","")
}
)
else -- placeholder to copy and paste from
return LSystem("",1,"N",
{
N=Step(Step.NOP)
},
{
Rule("N","N")
}
)
end
end