%!
[ %begin dictionary construction
% productions are described by integer-key/string-value pairs
48(0+1F) %0 %ascii code for '0' defined as the string "0+1F"
49(F0-1) %1 % " " " '1' " " " " "F0-1"
43(+) %+ % " " " '+' defined as itself
45(-) %- % " " " '-' " " "
70(F) %F % " " " 'F' " " "
% parameters
/A 90 %angle
/R 2 %radius
/B 10 %recursion%maximum recursion-level
>>begin % L-system dictionary on top of dictstack
(F0) % initial string on stack
300 400 % starting position on stack
[ % begin dictionary construction
/T[ % /T is the Turtle dictionary containing
% integer-key/procedure-value pairs
% keyed to ascii values
70{R 0 rlineto} %F
48{} %0
49{} %1
43{A rotate} %+
45{A neg rotate} %-
% For brackets, create a dictionary containing a single procedure '.' (dot)
% which holds a saved matrix (orientation+size) and currentpoint.
% Since this procedure is called while the Turtle dict is on top of the
% dictstack, the temporary dictionary is placed just under the top.
91{currentdict end[/.[currentpoint matrix currentmatrix]cvx>>begin begin} %[
% Right bracket: reset position and orientation,
% pop the dict just under the top.
93{. setmatrix moveto currentdict end end begin} %]
>>
/S{ % stack contains: string recursion-level
dup B eq{ % hit recursion bound, interpret string as turtle commands
T begin
exch % level string
%dup =
{ % iterate through string
load exec % execute turtle command by integer code
} forall % level % string has been consumed
end
%(B)= pstack
}{ % recurse
%(A)= pstack
exch % level string
{ % level char iterate through string
load exch % string level process production:load string by int code
1 add S % string level+1 increase level and call self
} forall % string has been consumed
}ifelse
1 sub % return level-1
%(C)= pstack
}
>>begin
moveto 0 S stroke
To run it, these 3 blocks can be saved as 3 files: dragon.ps, stems.ps, lsys.ps (any of the above program blocks will work identically). Then run with gs: gs dragon.ps lsys.ps
or gs stems.ps lsys.ps
. They can also be concatenated first, if desired: cat dragon.ps lsys.ps | gs -
or cat stems.ps lsys.ps | gs -
.