# How to create L-Systems

Using examples from: The Algorithmic Beauty of Plants: http://algorithmicbotany.org/papers/abop/abop.pdf

## Turtle sequence

The basic L-system commands are used to draw a line or series of lines. Each command follows on from the last, so as to draw a line through space. It helps to imagine a point (or turtle) moving forwards, and rotating left, right, pitching up and down, and rolling about its axis.

## Branching

If turtle commands are placed inside square brackets [ ]  the drawing point of the sequence jumps back to its position before the brackets. This way branches can be drawn from a main trunk

In the first example, only an initial premise string is used, consisting of F+- and [ ]

I have coloured the lines to show the main trunk, branches and nested branches. If I remove the branches is square brackets we are left with the simple instructions for the main trunk FFF-F+FF

## Edge Rewrite

Each edge F is replaced by a new sequence. Here it is replaced by a line with a triangular detour which is drawn by the turtle sequence F+F–F+F

Every generation all the Fs are replaced again.

To keep the overall size constant divide the size of each new F by 3:

And here a different sequence FF++F++F+F++F-F inserts a diamond shape:

Edge Rewrite Examples from The Algorithmic Beauty of Plants p9 – p11.

## Node Rewrite

Instead of replacing an edge we can replace an unused character in our sequence with a new sequence. We can insert these characters anywhere. Commonly used characters are A, B, C, X as they are not otherwise used as Turtle commands.

In this example a node A is inserted at the end of the first F line (FA) In the second generation it is replaced with a triangular detour, and another A is added to the end (+H–H+A) and this is repeated to keep adding the same shape over and over again. Note H is a line half the size of F.

If the initial premise had been AFA the zigzag shapes would have been inserted at both the start and the end of the lines

In the second example a node A is inserted at the end of the first F line (FA). In the second generation A is inserted (to allow further iterations of the sequence) then the point turns 30 degrees, then a branch containing a half line is drawn, and the drawing point jumps back to just after the 30 degree turn.

After each branch the angle (-) is added because it is outside the brackets, but the distance travelled by H is ignored.

This example is a slightly simplified version of the Houdini default L-System settings. I have coloured each generation with a different colour to make it clearer how the tree grows.

This tree uses two rules, but it is pretty straightforward. Rule 1 establishes the branches. Rule 2 uses the B character to insert a rotated line at each branch.

## Branching Structures

from The Algorithmic Beauty of Plants p25. Also available as defaults ‘2D Plants’ in Houdini

Figure 1.24: Examples of plant-like structures generated by bracketed OLsystems. L-systems (a), (b) and (c) are edge-rewriting, while (d), (e) and (f) are node-rewriting.

## Symbol variables

You may have noticed that many of the turtle symbols have one or more optional variables after them. Usually they have a default value which can be edited in the ‘Values’ menu, or this can be inserted manually into the sequence and can be manipulated using simple maths. This gives us more control over the way the system changes over the generations.

Examples

### Control variables over time

To set up a variable to change with each generation, you have to declare a value for it in the premise, then use a variable to represent this on both sides of the rule, and when a node or edge is declared for rewriting, manipulate this value (ie. add, multiply, divide a constant from it) This will be iterated every time the edge or node is replaced.

The effect achieved is similar to using the scale values “, _,!, ?,;, @ but with greater control

From the L-System documentation:

To create an L-system which goes forward x percent less on each iteration, you need to start your Premise with a value, and then in a rule multiply that value by the percentage you want to remain.

`Premise A(1)`

`Rule A(i)= F(i)A(i*0.5)`

This way i is scaled before A is re-evaluated. The important part is the premise: you need to start with a value to be able to scale it.

### Usage in a branching tree:

This example uses a simplified 2D version of the default branching L-System

Every time A is declared in rule 1, the value i (length) is halved so each generation starts with a smaller value.

## Using Custom Values

In order to make an L-System easier to edit you can replace the variables in your rules with custom values from the ‘Values’ menu. By default there are three of these, b, c and d, but there are options to add as many more of them as you want, and supply your own letters for them. This can make it much easier to tweak the settings of your L-System. It is a technique that is extensively used in The Algorithmic Beauty of Plants for their trees. These trees are available in the Houdini presets menu, but in my examples I have added an extra menu to tweak the values more easily.

### Monopodial Tree

The Algorithmic Beauty of Plants p.56, figure 2.6, also shown by the Houdini preset “Monopodial Tree”

Custom Values

b = contraction ratio trunk

e = contraction ratio branches

c = branching angle trunk

d = branching angle lateral axes

h = width decrease rate

i = divergence angle

For each tree below the rules are the same, only the values are changed.

### Sympodial Tree

The Algorithmic Beauty of Plants p.59, figure 2.7, also shown by the Houdini preset “Sympodial Tree”

Custom Values

b = contraction ratio 1

e = contraction ratio 2

c = branching angle 1

d = branching angle 2

h = width decrease rate

For each tree below the rules are the same, only the values are changed.

## Gravity (tropism)

Adding the variable T to an L-System adds gravity in the -Y direction. This will increase over the generations if it is called with each generation. It can also be inserted into a branch so it only effects that branch

### Ternary Tree

The Algorithmic Beauty of Plants p.60,  figure 2.8, also shown by the Houdini preset “Ternary Tree”

Custom Values

b = divergence angle 1

e = divergence angle 2

c = branching angle

d = elongation rate

h = width increase rate

T = tropism (gravity)

For each tree below the rules are the same, only the values are changed. The exception is the final tree which is grown at an angle using the initial premise +(75)F(0.5,1)A. This allows T to pull the tree to one side instead of directly downwards. It is then rotated back into an upright position using a transform node.

## Input Geometry in L-Systems

The L-System node has three geometry inputs which allow you to hook up any piece of geometry and use it in your L-System. J, K and M respectively. Any geometry connected to these inputs is created in the sequence by these letters

This example uses the default L-System tree again.

It adds leaf geometry [&&J] before each occurance of [B]. The brackets are used so that the pitch (&&) rotates the leaves but doesn’t affect the rest of the system. It also adds a berry geometry K at the end of each branch.

A plant generated by an L-system, The Algorithmic Beauty of Plants p.27, Figure 1.26

## Stamping Geometry

You can use the stamp expression to influence the inputs to an L-System. The syntax to use is

stamp(“/path/to/lsystem”, “lsys”,0)

The “lsys” variable can be changed inside the l-system using the parameters after the J, K or M symbols

So to change the parameters of the stamped input use eg.

J(0.1, 0, ”value for stamping”)

Note that the documentation suggests the following will work J(,,”value for stamping”) but in practice this doesn’t create any geometry, which seems to be a bug.

### Example using stamp expression to choose geometry

Input Geometry connected to inputs 0 to 3 of a switch node:

The switch node is connected to input J of an L-System set up as shown below. The ‘Select ‘Input’ parameter of the switch node has the expression

stamp(“../lsystem1″,”lsys”,0)

The sequence here is very straightforward. For every generation a straight line F is drawn upwards and 3 nodes B are branched off around the trunk.

These nodes are then replaced with the input connected to J

The stamping selects which input of the switch node connected to J is used. To determine the value connected to the third variable in J we use the ‘t’ character which returns the number of iterations. This starts at 1 at the top and descends to the last generation (21) at the bottom. We divide t by a variable b of about 4.5 to give us a number between 1 and 4 (representing the 4 inputs) then subtract 1 to give us 0 to 3. Changing the value of b in the values menu changes the positions at which the leaf/flower types change.

### Example using stamp expression to change colour

In this example we only have one geometry model, and we use stamping to change the colour.

The l-system is very similar to above, except that the J third character value is simply ‘t’ which is tweaked in the colour node to give an output between 0 and 1, and the first (scaling) character of J also uses ‘t’ to make the petals smaller as they ascend.

## Probability

Appending a colon followed by a fraction to the end of a rule determines how often that rule is applied

Eg.` B=&FFFAK:0.75 `

would replace B with &FFFAK only ¾ of the time. This is a good way to introduce more natural looking dropoup to L-Systems.

This example is a copy of the default L-System with geometry leaves and berries, only this time the second rule only executes in 75% of cases (:0.75)

This gives a much more natural appearance. Note that more than 25% of the foliage is missing because some branches drop out earlier on eliminating everything above them.

In this version a second Rule is added containing flower geometry and both rules are given low probability

Changing ‘seed’ in the geometry menu will give a different random dropout

Example of probability from The Algorithmic Beauty of Plants p.28

Stochastic L-systems

## Conditionals

We can add a condition to an L_Systems rule so that it only executes when the condition is true. The symbol for a conditional is a colon : after which the condition is given. For example

`A:t<4=J`

This would replace A with geometry input J only for the first 4 iterations

In this example there is a single leaf geometry input (J) There are two rules. The first is used for the first iteration. It places a single leaf at the end of a short branch. The second rule is used for all other iterations (ie. generations). It places two leaves angled away from the main branch.

Increasing the generations always adds more pairs of leaves

Conditional Edge Rewrite Examples from The Algorithmic Beauty of Plants p9 – p11.

### Bringing together Geometry Inputs, Custom Variables and Conditionals J(,,0) J(,,1) K(,,0) K(,,1) K(,,2)

## Context Sensitive L-Systems

From The Algorithmic Beauty of Plants p.30

A context-sensitive extension of tree L-systems requires neighbor Context in tree edges of the replaced edge to be tested for context matching. 2L-systems use productions of the form a <b>c → χ where the letter b (called the strict predecessor) can produce word χ if and only if b is preceded by letter a and followed by c.

This can be used in Houdini if the option “Context includes Siblings” is turned on.

Note that the > and < signs don’t mean “greater than” or “less than”, they mean “comes after” or “comes before”.

Example of Context sensitive leaf drawing

In this example, there are two conditions for drawing leaf geometry (J)

• If B comes after A and before A, a leaf is drawn to the left
• If A comes after B and before A, a leaf is drawn to the right

The result of these rules is that leaves are only drawn when the direction changes and are always situated on outside curves.

Examples of branching structures generated using L-systems based on the results of Hogeweg and Hesper. The Algorithmic Beauty of Plants p.34

## Phyllotaxis

Phyllotaxis refs to the arrangement of petals or seeds in a flower head. The key to this is the Fibonacci angle, approximately equal to 137.5◦. The angle is the result of dividing a circle by the golden ratio.

The central illustration below gives the basic L-System rule used to create the pattern. The patterns to the left and right demonstrate how much the pattern changes when the angle is changed by a fraction of a degree.

### Sunflower

The sunflower uses a single rule to draw the seeds and petals. The only change is that the seeds become petals as the generations increase, which is done using Conditionals J K M(,,0) M(,,1) M(,,2) M(,,3)

The same principle can be used to create flowerheads. In this flower the angle and size of the petals is increased as they get further from the centre (as n becomes greater)

The & symbol in rule 2 is used to rotate the petals outwards as the generations increase. Variable b is linked to the total number of generations. This is also used to grow the petals as they get further out.

## Creating Leaves from Surface Geometry

It is possible to use L-Systems to draw polygon surfaces, using the following commands:

{ Start a polygon

. Make a polygon vertex

} End a polygon

The Algorithmic Beauty of Plants contains several examples of surfaces built from polygons, and one of these is integrated into the houdini L-System examples as ‘Cordate Leaf’

### Cordate Leaf

The Algorithmic Beauty of Plants p.123, Figure 5.5

This is created by drawing polygon triangles and joining them up to form the shape of a leaf. If we look at the turtle generated for each generation we can see that the rules add progressively longer lines (lists of FFF), and join these up with progressively wider angles (lists of +++)

The results below show only the right side for simplicity ie. they ignore rule 2

Gen 0 [A]

Gen 1 [[+A{.].C.}]

Gen 2 [[+[+A{.].C.}{.].FFFC.}]

Gen 3 [[+[+[+A{.].C.}{.].FFFC.}{.].FFFFFFC.}]

Gen4 [[+[+[+[+A{.].C.}{.].FFFC.}{.].FFFFFFC.}{.].FFFFFFFFFC.}]

### A family of simple leaves

The Algorithmic Beauty of Plants p.124, Figure 5.6

Variable Values Represent:

b =  initial length – main segment

c = growth rate – main segment

d = initial length – lateral segment

e = growth rate – lateral segment

f = growth potential decrement

### A Rose Leaf

From The Algorithmic Beauty of Plants p.126, Figure 5.8 Variable Values Represent:

b =  5 :     initial length – main segment

c = 1.15 :  growth rate – main segment

d = 1.3  : initial length – lateral segment

e = 1.25 :  growth rate – lateral segment

f =  3 :     initial length – marginal notch

h = 1.09 :  growth rate – marginal notch

## Ferns and other Compound Leaves

From The Algorithmic Beauty of Plants p.129, Figure 5.11

• b =  apical delay (number of children on a frond, a whole number)
• c = internode elongation rate (decrease in size of fronds)

### Compound leaves with alternating branching patterns

From The Algorithmic Beauty of Plants p.130, Figure 5.12

• b =  apical delay (number of children on a frond, a whole number)
• c = internode elongation rate (decrease in size of fronds)

## Join the Conversation

1. 