L-systems: draw a stochastic plant (II)
This is a continuation to my previous article, where L-systems were introduced and we have seen how to use them to generate fractals. Now, we are going to use L-systems to generate more complex structures: plants and vegetation.
In this episode, we will understand how to make a plant using L-system. Then we will improve our system by introducing some randomness.
Plant modelling:
To generate our plant, we will use the same “FG+-[]” alphabet that we used in the previous article to generate fractals, with the addition of the letters “X” and “Y” which will have no functional role (no drawing or movement).
Alphabet : F G X Y + — [ ]
Axiom : YYY
Rule : ( X -> X[-FFF][+FFF]FX ) ( Y -> YFX[+Y][-Y] )
Breaking down the process:
Let’s breakdown this process to see how this L-system works. We are going to isolate each rule and visualize it.
Here we are going to draw a little green circle whenever there is an X and a yellow triangle when we have Y. This will help us to visualize how these elements intervene in the process of modelling a plant.
Rule 1: ( X -> X[-FFF][+FFF]FX )
As we can see here, the first rule allows the plant to grow up one step vertically while releasing two lateral strings. After the first generation, we obtain two “X”s which will keep multiplying exponentially in each generation. We can say that this rule is responsible of the vertical growth of the plant while releasing some strings along that path. It’s the stem rule.
Rule 2: ( Y -> YFX[+Y][-Y] )
The second rule is mainly responsible of the horizontal expansion of the plant while growing up. We will isolate the second rule by ignoring the X element and removing it from the second rule. At each step, the Y element grows up by a little string and releases two rotated Y elements in opposite directions which will prepare the ramification to grow up in the next generation. As in the previous rule, each Y element will generate a new one in the next generation which lead to an exponential multiplication of the elements.
Adding up both of the rules, we get a plant that grows exponentially fast vertically and horizontally. We have to counterbalance the exponential growth of in each generation by scaling down the size of each new generation. As the size is multiplicated by 2 in each run, we will just use a scale factor of 0.5.
We get a plant that grows up in a balanced way vertically and horizontally.
Stochastic plant
All the plants generated by this L-system will look the same. When we want to generate a forest or a garden with this model, it will look very artificial.
It is time to make this model more complex and more realistic by adding some randomness to the generation of the branches. We will add a random scaling factor when drawing each line (here the scaling factor is between 0.7 and 1.2) and an additional random angle in each rotation (here a random angle between 0 and 30 degrees). We get the following plant — which by the way looks more realistic than the deterministic plant .
In this example, the random angle added to each rotation is chosen to be only a positive angle, which models another type of plants. Each time we generate a plant we get a new unique one as the branching angles are different in each run.
Even more stochastic plant:
In the previous example, we added randomness to the Turtle interpretation of our L-system, which produced a different plant each time. This added randomness to geometric aspects of the plant (branching angles and stem lengths), but the topology remained the same.
In his incredible book “The Algorithmic Beauty of Plants”, A. Lindenmayer — the inventor of L-systems- extended their definition by adding another kind of randomness : making the L-system stochastic itself even before interpreting it by Turtle graphics. This will allow us to generate plants with different topology.
This randomness is achieved by matching each letter of the L-system alphabet to a set of rules and their corresponding probabilities. This a simple example of a stochastic L-system:
Each rule can be selected with approximately the same probability of 1/3.
Now, we are going to introduce randomness to the L-system of the previous plants. We will associate a probabilistic ruleset to replace the ‘Y’ letter. Remember that it is the letter responsible of the horizontal expansion of the plant. The ‘Y’ rule will be replaced by a set of 3 rules with different probabilities to generate different kind of plants.
Here we get different types of plants: balanced plants and others with a bias in branching toward the right or the left side.
Conclusion:
We can see through these examples how powerful L-systems are in modelling the growth of plants and its branching structures. These systems can be improved to model more complex plants and trees by introducing parameters to the ruleset. In the next episode, we will extend the definition of our L-systems by introducing parameters.
Please feel free to use the code I developed to generate your own plants. I am eager to see how your plants looks like.
Github: https://github.com/hhassen/L-system
References:
The Algorithmic Beauty of Plants, Przemyslaw Prusinkiewicz & Aristid Lindenmayer
Procedural generation of tree models for use in computer graphics, Charlie Hewitt