Friday, February 22, 2008

Introducing L-Systems

L-Systems
---------
L Systems stands for Lindenmayer systems stands for a class of fractals that are rendered by repetitive expansion of some simple formula using rewriting rules similar to a grammar. First a small introduction to LOGO and then exploration into how one can draw L-Systems using a LOGO like language.

Introducing LOGO
----------------
The simple graphics programming language “LOGO” consists of instructing a ‘turtle’ graphics pointer to plot an image by moving around in various directions. The state of the turtle is represented by the triplet (X,Y,theta), where X,Y represent the co-ordinates of the turtle’s current location and theta represents the direction in which the turtle will move when asked to move forward. For example, the graphics command f+f+f+f with f=10 and dtheta=90 will draw a square with side=f as follows - for every f, the turtle will plot a move forward by length f (and in the process draw a line of length f), and for every +, the cursor will turn by 90 degrees anti-clockwise. For every -, the cursor will turn by 90 degrees clock-wise



This simple LOGO command language can be used to draw a fractal as follows:
This consists of using an axiom and a production. Let us consider the axiom to be ‘f’ and the production is ‘f -> f +f +f +f’ (means replace f by f + f +f + f). Given these two, replace every letter in the axiom with the applicable production rules up to the required depth of ‘n’ (n = 4 here) as shown below:
1. f
2. f + f + f +f
3. f+f+f+f + f+f+f+f + f+f+f+f + f+f+f+f
4. f+f+f+f + f+f+f+f + f+f+f+f + f+f+f+f + f+f+f+f + f+f+f+f + f+f+f+f + f+f+f+f + f+f+f+f + f+f+f+f + f+f+f+f + f+f+f+f + f+f+f+f + f+f+f+f + f+f+f+f + f+f+f+f +
It is important to note that the productions are applied in parallel and not sequentially.

For the above equation, the image will be the same square that the graphics cursor will repeatedly traverse. However, such a substitution and expansion can lead to interesting fractal images, when different values for the axiom, production and dtheta are used. Interesting patterns can be created with these equations, as shown below:


------------------------------------------------------------

This idea of axioms, productions and turtle graphics forms the basis of Lindermayer systems (L-Systems for short).

For simplicity, we have assumed the axiom to be ‘f’, while the axiom itself could be a complete turtle command like ‘f + f + f –f –‘. Also, we have used only one variable ‘f’. We could use more than one variable say a, b and define different production rules for each variable like for example:
Axiom – aab
Productions
a -> aba
b -> ba

Hence, the system in general can be represented using an axiom, a set of productions, and the required depth n. These can lead to more interesting fractals as we will discuss further. Two factors specified when drawing the image are the rules for the variables in the axiom, and the angle dtheta to be applied for the ‘+’ and ‘-‘ operations.

Given below are some fractals and the system used to generate the same.