Saturday, November 10, 2007

L-Systems, part II

In the last post, we looked at L-Systems and the basic principles of an L-System. This time we will take a quick look at how to use that in computer graphics.

An L-System can based on a string of characters, as seen in the previous post. Now, consider the following statement:
Each letter in this string represents an action.

Lets say that the system consists of three letters, D,R and L. Each of these letters represent an action.

D: Draw a line 1 unit long
R: Turn 90 degrees right. Do nothing else
L: Turn 90 degrees left. Do nothing else

So, the combination DRD would mean "Draw a line, turn right, and then draw another line in the new direction."

DRDRDRD would mean Draw a line, turn right, draw a line, turn right, draw a line, turn right and draw a final line. This would result in a cube.

Now, let's say we use rules to transform a string of these commands.

Example:

Starting string: DRD

Rules: D = D
R = DRDLDRD
L = DLDRDLD

As a small excerscise, draw the shapes DRD and it's "child" DDRDLDRDD.

As you see, small corners are added to each corner. For each generation, more corners are getting "cut out".

This is a simple start on how to use L-Systems for drawing. There are more advanced examples, and we will get back to those in a later post.


Powered by ScribeFire.

Tuesday, November 06, 2007

L-Systems pt I

L-Systems are commonly used to create complex patterns and constructs.

This and following posts are my attempt to understand these L-Systems more.

First of all, what is an L-System?

An L-system consists of two parts (roughly speaking). A chain (or a text string) and rules.

The chain can consist of any pre-defined components. To demonstrate I will use a chain of letters. The string AABA could be such a chain.

The rules describe how this chain should change. For instance

A = A
B = AB

You let the chain grow or change by running it through the rules, one component at a time. Let's consider the example above:

Chain: AABA
Rules: A=A
B=AB

First run:
First letter: A, becomes an A
Second letter: A, becomes an A
Third letter: B, becomes AB
Fourth letter: A, becomes A

The result is A+A+AB+A = AAABA

Second run:
First letter: A, becomes an A
Second letter: A, becomes an A
Third letter: A, becomes an A
Fourth letter: B, becomes AB
Fifthletter: A, becomes A

The result is A+A+A+AB+A = AAAABA

In this example, the chain was put through the same set of rules twice. In theory an L-System could be run any number of times, applying the rules over and over again.

Which each iteration, the string evolves and changes according to the rules.

There are a couple of basic patterns that is worth mentioning.

The first one is where you add complexity in the middle of the string.
Example:
Chain: ABA
Rules A = AB, B = A

First run: AB+A+AB = ABAAB
Second run: AB+A+AB+AB+A = ABAABABA
Third run: AB+A+AB+AB+A+AB+A+AB = ABAABABAABAAB

and so on. As can be seen, the string grows longer, and more complex all the time.

Consider instead this string, and in this case, I'll use three letters. The basic principle is the same.

Chain ABX
Rules, A=A, B=B, X=ABX

First run A+B+ABX = ABABX
Second run: A+B+A+B+ABX = ABABABX
Third run: A+B+A+B+A+B+ABX = ABABABABX

In this example, the inner structure isn't changed, instead a tail is added to the end for each run. This is accomplished with a tail component that adds a string+the tail component for each run.

These L-systems can use an indefinite number of rules and components. I could have an L-System with all the letters of the alphabet and a multitude of rules, creating something very complex.

So, what can we do with this then?