Sitemap
Level Up Coding

Coding tutorials and news. The developer homepage gitconnected.com && skilled.dev && levelup.dev

Programming L-Systems in PostScript

6 min readDec 20, 2021

--

Using printers to run recursive functions

Press enter or click to view image in full size
Dragon
Photo by Ridwan Meah on Unsplash

Here be dragons!

Dragon curve

The Dragon Curve is a famous fractal. I first saw it on the cover of a (French) book I was reading as a teenager. I then started to draw beautiful dragons, by hand. Later, I decided to write a program to draw the curve. The program was written in Pascal. It worked and drew nice dragons. Unfortunately, I’ve lost the source code for it. I would be curious to look at it today, because the program did not use recursion! I vaguely remember that it was performing repeated iterations over a large array, but I don’t remember the details. It must have been an ugly piece of code…

L-systems

Years later, I found on the web a PostScript program that could draw the Dragon Curve. It intrigued me enough to actually make me learn PostScript just to understand it. It turns out that PostScript is a full-fledged (Turing-complete, in pedantic terms) programming language, and that printers can thus execute recursive functions.

I couldn’t understand the algorithm used in the PostScript program. It seemed to rely on two mutually-recursive functions X and Y, and rotations of 45 degrees (even though all angles in a Dragon Curve are right angles). Once you clean up the code (which was horrible, with global variables shared between recursive calls), it boiled down to this:

X -> -FX++FY-
Y -> +FX--FY+
F ->

where a form or “turtle graphics” is used: - represents a clockwise 45 degrees rotation, + is an anticlockwise 45 degrees rotation and F draws a line forward.

I thought this algorithm was remarkable — certainly too good to have been invented by a guy who uses global variables between mutually recursive functions. After searching for more Dragon Curve programs on the web, I found somewhere the exact formulation above. It was part of a list of Lindenmayer systems (L-systems), which drew everything from classic fractals (Hilbert, Koch) to various plant-like pictures.

Get Michel Charpentier’s stories in your inbox

Join Medium for free to get updates from this writer.

L-systems were invented by Aristid Lindenmayer, a Swedish biologist who used them to model the growth of plants. He ended up publishing about them in both biology and computer science journals. L-systems have been extended to 3D, and widely used in plant biology and computer graphics. The reference book seems to be The Algorithmic Beauty of Plants, by Przemyslaw Prusinkiewicz and Aristid Lindenmayer. Although all the equations that are used in this page were found on the web, I strongly suspect they originated from this book. (All I can say is that they are definitely not mine.)

The Dragon Curve as an L-system

Using the set of rewriting rules above (starting from X) and applying them iteratively, here is what you get (read a curve from left to right, starting horizontally):

Press enter or click to view image in full size
Dragon curve, orders 1 through 7

If you “round the corners”, you better see how the curve is drawn. Here it is, at order 7:

dragon curve, order 7

And if it still looks more like a French poodle than a dragon to you, it’s because you have no imagination.

Dragon Curve in PostScript

Now, here’s the PostScript program that does the job. First, tell the printer we are a PostScript file:

%!PS

Then, specify the order (number of iterations). Beyond 20, it may take forever to draw and print:

/N 7 def

Here is the core of the computation, functions X and Y. The order value (or depth) is on the stack and is decremented by one and duplicated 4 times for the 4 function calls. When the order is zero, stop; otherwise, do the recursive calls according to the equation:

/X {
dup 0 ne
{1 sub 4 {dup} repeat - F X + + F Y -}
if pop
} def

/Y {
dup 0 ne
{1 sub 4 {dup} repeat + F X - - F Y +}
if pop
} def

Function F does nothing and just disappears, except at the end, when order is 0, it draws a piece of line:

/F {
0 eq { 10 0 rlineto } if
} bind def

Functions - and + do right and left 45-degree rotations, as expected:

/- { -45 rotate } bind def
/+ { 45 rotate } bind def

Make sure that pieces of lines are joined nicely:

1 setlinejoin
1 setlinecap

Finally, decide where to start, and scale the drawing by a square root of two factor for each iteration, so that it’s always the same size, no matter what the order is:

newpath
220 180 moveto
50 N { 2 sqrt div } repeat dup scale

Rotate the drawing to landscape, and start:

90 rotate
N X

Finish by proudly showing the resulting page:

stroke
showpage

The whole PostScript program can be downloaded here.

More L-systems

On my way to understand the mysteries of this Dragon Curve program I had found, I encountered a number of L-system equations. Some of them are classic fractals, like Hilbert’s curve:

angle 90
START -> X
X -> -YF+XFX+FY-
Y -> +XF-YFY-FX+
F ->

or Koch’s snowflake:

angle 60
START -> +F--F--F
F -> F+F--F+F

But the ones I like best are plant-like pictures. (After all, Lindenmayer was a biologist.) What I find fascinating is how different they are from each other, even though they are based on similar sets of equations. Some of my favorites are below:

angle 22.5
START -> F
F -> FF-[-F+F+F]+[+F-F-F]
angle 22.5
START -> X
X -> F-[[X]+X]+F[+FX]-X
F -> FF
Press enter or click to view image in full size
angle 20
START -> F
F -> F[+F]F[-F][F]
angle 25
START -> X
X -> F[+X][-X]FX
F -> FF
angle 20
START -> X
X -> F[+X]F[-X]+X
F -> FF
angle 25
START -> F
F -> F[+F]F[-F]F
angle 25
START -> Y
X -> X[-FFF][+FFF]FX
Y -> YFX[+Y][-Y]
F ->
Press enter or click to view image in full size
angle 30
START -> F
F -> F[+F[+F][-F]F][-F[+F][-F]F]F[+F][-F]F

I have not mentioned [ and ], which are used for branching. Basically, ] brings you back in the drawing at the point where the corresponding [ was issued. They are easily implemented in PostScript using gsave and grestore, to push and pop the graphics context on and off the stack.

L-systems in PostScript

The plants on this page were all rendered using this PostScript program by simply modifying the equations ([ and ] are called B and Ebecause the square brackets are part of PostScript’s syntax). This program is a little more complicated than the Dragon Curve above, but it’s mostly due to the use of colors, and because it’s made generic to work with different equations.

For reasons that were never clear (and probably involve cosmic forces beyond our comprehension), this experiment in programming L-systems in PostScript was published by the Zpravodaj Československého sdružení uživatelů TeXu (2012, vol. 1) under the title Programujeme L-systémy v PostScriptu. The paper contains a few more technical details and, in spite of its Czech title, is written in English.

--

--

Written by Michel Charpentier⁠

Learning and teaching programming, especially Java/Scala, with a focus on concurrency, functional programming and formal verification.