You are on page 1of 5

27/2/2015

LSystemRules

LindenmayerSystems:Details
Overview
Asabiologist,AristidLindenmayerstudiedgrowthpatternsvarioustypesofalgae.In1968he
developedLindenmayersystems(orLsystems)asamathematicalformalismfordescribingthe
growthofsimplemulticellularorganisms.Morerecently,Lsystemshavefoundseveral
applicationsincomputergraphics.Twoprincipalareasincludegenerationoffractalsandrealistic
modelingofplants.
Basically,aLindenmayersystembeginswithastringofsymbolscalledtheaxiom,andappliesto
theaxiomasetofproductionruleswhichareusedtorewritetheaxiom.
Forexample,lettheaxiomofaLindenmayersystembethestringofthreesymbols'peg',andlet
thesetofproductionrulesbethesinglerule:'e=i'.Thisissummarizedinthefollowingtable:
axiom: peg
rules: e=i
Whentheruleisappliedtotheaxiom,each'e'symbolsintheaxiomisreplacedwithan'i'symbol.
Thus,theaxiom'peg'becomes'pig'.Thisnewstring'pig'iscalledthefirstgenerationoftheL
systemsinceitistheresultofapplyingtherulestotheaxiomonetime.
ThesecondgenerationofthisLsystemistheresultofapplyingtherulestothefirstgeneration.
Sincethereareno'e'symbolsinthefirstgeneration,thereisnothingfortheruletoreplace.Thus
thesecondgenerationisalsothestring'pig'.ThisratherboringLsystemissummarizedbelow:
BoringLSystem
axiom:
peg
rules:
e=i
generation1: pig
generation2: pig
generation3: pig
Lsystemsaremoreinterestingwhentherulesarerecursive.Arecursiveruleisarulethatreplaces
asymbolwithacopyofitselfplussomethingextra.Anexampleofarecursiveruleis'e=eie'.The
tablebelowshowsthefirstthreegenerationsofanLsystemwiththisrecursiverule:
RecursiveLSystem
axiom:
peg
rules:
generation1:
generation2:
generation3:

e=eie
peieg
peieieieg
peieieieieieieieg

http://www.cs.unm.edu/~joel/PaperFoldingFractal/Lsystemrules.html

1/5

27/2/2015

LSystemRules

Inthefirstgeneration,thesingle'e'symbolintheaxiomwasreplacedwith'eie'creatingthestring
'peieg'.SincetheruleinthisLsystemisrecursive,thefirstgenerationstringhastwonew'e'
symbols,eachofwhichgetreplacedwith'eie'inthesecondgeneration...andsoitgoes.
ThenextsectionsummarizesthedefinitionofanLsystemmoreformally.

DefinitionofanLSystem
AnLsystemisaformalgrammarconsistingof4parts:
1. Asetofvariables:symbolsthatcanbereplacedbyproductionrules(seebelow).Inthe
FractalGrowersoftware,variablescanbeanyofthe26lowercaseEnglishlettersathrough
z
2. Asetofconstants:symbolsthatdonotgetreplaced.IntheFractalGrowersoftware,the
constantsareanyofthefollowingsymbols:!,[,],+,.
3. Asingleaxiomwhichisastringcomposedofsomenumberofvariablesand/orconstants.The
axiomistheinitialstateofthesystem.
4. Asetofproductionrulesdefiningthewayvariablescanbereplacedwithcombinationsof
constantsandothervariables.Aproductionconsistsoftwostringsthepredecessorandthe
successor.

GraphicInterpretationofStrings
RecursiveLsystems,liketheonedescribedabove,oftenproduceintricatelycomplexpatternsthat
areselfsimilaracrossmultiplescales.Thesepatternsarealmostimpossibleforahumanto
perceivedirectlyfromlongstringsofsymbols.Aswithmanytypesofdata,agraphical
representationofthestringscanexposethesepatterns.
ThemostcommongraphicalinterpretationappliedtoLSystemsisbasedonturtlegraphicswhich
isthebestknownfutureoftheLogoprogramminglanguage.Logowasinventedin1967byWally
FeurzeigandSeymorurPapertasakidfriendlywayofteachingcomputerscience.Theturtlein
turtlegraphicsisanonscreencursor(oronfloorrobot)whichcanbegivendrawing(or
movement)instructionssuchasmoveforwardbyaspecifieddistanceorturnrightorleftbya
specifiedangle.Traditionallythecursorinturtlegraphicsisrepresentedpictoriallyasaturtle
icon.
WhenapplyingturtlegraphicstoLsystems,astateoftheturtleisdefinedasaquadruple(x,y,a,
c).TheCartesiancoordinates(x,y)representtheturtle'sposition.Theanglea,calledtheheading,
isinterpretedasthedirectioninwhichtheturtleisfacing.Thecolorcisinterpretedasthecolor
penthattheturtlecurrentlyhaspressedtothefloorsothatanymovementoftheturtlewillcreate
alineofthatcolor.Giventhestepsizedandtheangleincrementb,theturtlecanrespondto
symbolsinanLsystemstringaccordingtothefollowinginstructions:
Moveforward(inthedirectionofthecurrentheading)adistanced
whiledrawingalineofcolorc.Thestateoftheturtlechangesto
f (x',y',a,c),where
http://www.cs.unm.edu/~joel/PaperFoldingFractal/Lsystemrules.html

2/5

27/2/2015

LSystemRules

x'=x+dcos(a)and
y'=y+dsin(a).
h Sameasf.
g Sameasfexceptnolineisdrawn.
Likethefandhsymbols,the!symbolmovestheturtleforward.
Unlikethefandhsymbols,thedistancethatthe!symbolmovesthe
turtleisafunctionofthesymbol'sage.

The!symbolisaconstant,soonceitisaddedtoastring,itremains
inthestringforallfuturegenerations.
EveryNEW!symbolmovestheturtlethesamedistanceasdoes
everyf,g,andhsymbol.
Everygenerationthata!symbolages,itslengthisdividedbythe
valueofShrinkage.
Thus,aShrinkagevalue>1makesolder!symbolsshorter,anda
Shrinkagevalue<1makesolder!symbolslonger.
Thecolorofthe!symbolisalsoafunctionofitsage.Thedefault
colorschemecausesnew!symbolstodrawinlightgreens.Older!
symbolsaredrawnwithdarkergreensandtheoldest!symbolsare
drawninbrowns.
Thestateoftheturtlechangesto
(x',y',a,c),
where
x'=x+d/sgenerationcos(a),
y'=y+d/sgenerationsin(a),and
sistheshrinkagevalue.

Turnrightbyangleb.Thenextstateoftheturtlebecomes(x',y',
a+b,c).

Turnleftbyangleb.Thenextstateoftheturtlebecomes(x',y',ab,
c).

[ Savethecurrentstateoftheturtlebypushingitontoastack.
]

Restoretheturtletothestateofthelast]bypoppingthestatefroma
stack.

a Changethecoloroftheturtletocolora.
b Changethecoloroftheturtletocolorb.
c Changethecoloroftheturtletocolorc.
d Changethecoloroftheturtletocolord.
e Changethecoloroftheturtletocolore.
Allothersymbolsareignoredbytheturtle(theturtlepreservesitscurrentstate).
SomeofthepresetsintheFractalGrowersoftwareuseothersymbolssuchasx,yandz.While
http://www.cs.unm.edu/~joel/PaperFoldingFractal/Lsystemrules.html

3/5

27/2/2015

LSystemRules

thesesymbolsareignoredbytheturtle,theyarestillusefulintheLsystemgrammarforbuilding
stringstructurescontainingsymbolsnotignoredbytheturtle.
Noteoncolor:thevariables'a'through'e'onlyeffectthecolorwhenthe'colorfandhsegmentsby
imbeddedvariables'radiobuttonisselectedonthecolortaboftheLSystemcontrolpanel.Ifthis
radiobuttonisnotselected,thentheturtleignoresthe'a'through'e'symbols,andthecolorof
eachlinesegmentisdeterminedbythelocationofeachforhinthestring:fandhsymbolsatthe
beginningofthestringdrawredsegments,inthemiddleofthestringdrawgreensegments,andat
theendofthestringdrawpurplesegments.
Noteonsteplength:InLsystemsthatdonotusethe'!'symbol,thesteplengthhasnoeffectonthe
appearanceofdrawing.Thisisbecause,all'f'and'h'symbolsaredrawnwiththesamesteplength,
thenthecompletedrawingisscaledsoastofillthedrawingwindow.Alargestepsizewilljust
causethesoftwaretoscaledownthefinaldrawingmore.However,whenthe'!'isused,thenthe
steplengthhasagreateffectonthedrawingsince'f'and'h'symbolsalwaysmoveforwarda
distanceequaltothesteplengthwhereas'!'symbolsmoveforwardadistanceequaltothestep
lengthraisedtothepowerequaltothesymbol'sage.

Example:DragonCurve
Manyfractals(oratleasttheirfiniteapproximations)canbethoughtofassequencesofline
segments.
Thedragoncurveisonesuchfractal,anditsfiniteapproximationcanbecreatedwithanL
System:
DragonCurveLSystem
variables:
constants:
axiom:

fh
+
f

rules:

f=fh
h=f+h

angleincrement: 90degrees
generation1:
fh
generation2:
fhf+h
generation3:
generation4:

fhf+hfh+f+h
fhf+hfh+f+hfhf+h+fh+f+h

ThisLSystemhastworules:oneforreplacing'f'symbolsandoneforreplacing'h'symbols.
Therefore,both'f'and'g'arevariablesoftheLSystem.The'+'and''symbolsareconsidered
constantsbecausetherearenorulesinthesystemforreplacingthesetwosymbols.Theaxiomof
thesystemisasingle'f'symbol.Therefore,whencreatingthefirstgeneration,therulefor
replacing'h'symbolsisnotused.
Theredandgreencolorsinthetablehelpmakethesubstitutionprocesseasiertoperceive.Ateach
generation,thenewsymbolsthatresultfromreplacingan'f'symbolareshowningreen,andthe
newsymbolsthatresultfromreplacingan'h'symbolareshowninred.Eachsymbolthatismore
thenonegenerationoldisshowninblack.Onlyconstantscanbemorethenonegenerationold,
http://www.cs.unm.edu/~joel/PaperFoldingFractal/Lsystemrules.html

4/5

27/2/2015

LSystemRules

becausevariablesarealwaysreplacedateachgeneration.Onlysomeofthe'+'and''symbolsare
showninblackbecauseonlythesearemorethenonegenerationold.
Let'stakeastepbystepexaminationofthesecondgenerationofthisLSystem.Thegeneration2
stringis:'fhf+h'.
Assumingthattheinitialheadingoftheturtleisupwardonthescreen,thenthefirst'f'willdrawa
linestraightup.The''causestheturtletochangeitsheadingby90degreestobepointeddirectly
totheleft.The'h'drawsalinedirectlytotheleftofthescreen.Thesecond''symbolagainturns
theturtle90degreestoitsleftwhichisdirectlydownonthescreen.Thefsymboldrawsaunit
lengthlineinthedowndirection.Thelastturnsymbolinthestringisa'+'whichturnstheturtleto
itsrightby90degrees.However,sincecurrentheadingoftheturtleispointeddownward,itsright
isthescreen'sleft,soagaintheturtle'sheadingistotheleftofthescreen.Finally,thehsymbol
drawsaunitlengthlinetothescreen'sleft.Theresultingimageisshownisfigure1withline
segmentcolorsmatchingthe'f'and'h'colorsinthedescriptionabove.

figure1
FractalGrowerHome
Copyright:JoelCastellanos,19962007

http://www.cs.unm.edu/~joel/PaperFoldingFractal/Lsystemrules.html

5/5

You might also like