Sol::Tutorials

# L-System Fractals

## the line age lsystem w/sources

L-system fractals are fun. While the basic idea is pretty easy to grasp, they provide lots of complexity. The most common use for L-systems are plant generation, but they can be used for all sorts of fractal structures. The fans of my demos may already know that I've used l-systems in several places, including Gateways and The line age.

In order to get to L-systems, we need to explain two different concepts. First is pretty simple, at least if you've played with the LOGO language. Yes, the one where you control a turtle..

That screenshot doesn't make too much sense if you don't know what the characters mean. Thus, here's all the commands the example application supports:

 Command Function . Move forward * Draw a star in current position + Increase current variable by 45 degrees - Decrease current variable by 45 degrees ( Push position and rotation in stack ) Pop position and rotation from stack X Select variable X (rotation around X axis) Y Select variable Y (rotation around Y axis) Z Select variable Z (rotation around Z axis)

Let's ignore the "a=" in the beginning for now. Basically we're selecting rotation around the Z axis, then move forward a couple of times, rotate, move forward, etc. With the commands, we can do other weird things like rotating around all of the axis, as well as branch using the stack.

Next up is what makes L-systems fractals. We define several axioms; basically assigning character strings to characters. Let's say, for example:

``````
a=ab
b=bc
c=aa
``````

First we take the first axiom as our starting data. Then we replace all of the characters in the string with the axioms. Then we take the resulting string and repeat the process. After a few iterations, we have a rather interesting result. I've colored the values based on where they're from in the most recent iteration:

1. ab
2. abbc
3. abbcbcaa
4. abbcbcaabcaaabab

Now, if you haven't guessed already, the thing that makes things interesting is combining the two. Interleaving the axiom strings with the control characters results in a long string of drawing commands.

Note that the number of axioms is by no means limited to three. Also, different L-system implementations also have a vastly different grammar; kind of like with particle systems, there's tons of new commands you may think up. And nobody says l-systems can only control graphics; there's no reason why you couldn't generate note data this way, for example.

You can download the 0.1 version of the simple l-system development here - it is the one that implements the above grammar, and always uses four iterations.

A slightly more advanced l-system, basically the same used in 'the line age' can be downloaded, along with its sources, here.

Have fun!