pnavarrc / lindenmayer

L-systems

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

lindenmayer

An L-system or Lindenmayer sytem, is a type of formal grammar. It consists of a set of symbols, production rules (functions) and an initial value or axiom that can generate self-similar structures. They were developed in 1968 by Aristid Lindenmayer, a theoretical biologist and botanist to describe the behaviour of plant cells and to create models of plant development.

While the formal description of L-systems is a bit dry, an L-system is just a description of a set of symbols that can be used to generate strings, and functions that rewrite the strings based on a set of rules.

L-systems are defined by an alphabet, an axiom or initial value, and the production rules.

  • alphabet: The set of characters that will be used to generate the strings. There are two kinds, constants and variables.
    • constants: Constants are characters that are not replaced by the production rules.
    • variables: Variables are characters that are replaced by the production rules. If there is a variable that doesn't have a production rule, the identity function is applied, the character is replaced by itself.
  • axiom: The string that will be used to start the process.
  • production rules: A function that tells how to replace each one of the variables.

Example: Algae

The L-system that Lindenmayer used to model the growth of algae. Here, the alphabet only consists of the letters A and B.

  • alphabet
    • variables: A, B
    • constants: none
  • axiom: A
  • rules:
    • AAB
    • BA

This defines a mechanism to generate strings. Starting with the axiom A, apply the production rules, replacing each symbol according to its rule. This will generate the string AB. Applying the production rules sequentially results in complex, self-similar structures.

Iteration Value
n = 0 A
n = 1 AB
n = 2 ABA
n = 3 ABAAB
n = 4 ABAABABA
... ...

The sequence has some interesting properties. The number of characters in the string, for example, follows the Fibonacci sequence, skipping the first 1. Of course, we can define L-systems with more variables, constants and more complex rules, which generate more complex structures.

Graphics Generation

Strings generated by L-systems can be used as drawing instructions to create graphics. Let's take the fractal tree as an example.

Example: Fractal Tree

In this example, we have an alphabet with 0, 1, [ and ].

  • alphabet
    • variables: 0, 1
    • constants: [, ]
  • axiom: 0
  • rules:
    • 01[0]0
    • 111

Remember that the numbers and brackets here are just characters in a string, they don't have meaning by themselves. This L-system generates the following strings:

Iteration Value
n = 0 0
n = 1 1[0]0
n = 2 11[1[0]0]1[0]
n = 3 1111[11[1[0]0]1[0]0]11[1[0]0]1[0]0
... ...

We can assign each symbol a drawing instruction which makes a string a sequence of instructions. To generate a tree out of this L-system, we can use the following drawing instructions

  • 0: draw a line segment ending in a leaf
  • 1: draw a line segment
  • [: push position and angle, turn left angle 45 degrees
  • ]: pop position and angle, turn right 45 degrees

Sequences of drawing instructions generated with this system

Learn More

About

L-systems

License:MIT License