Introduction

A L-system aka Lindenmayer system is a rewriting system and a type of formal grammar, that can be used to create and generate a bunch of things. I will not dig to deep in the theorical definition of L-Systems since wikipedia already does it very well.

What’s a L-System ?

Let V be an alphabet and V+ be the ensemble of valid words from V.
Let ω a starting axiom from V+.
Let S be a set of constants.
Let P be a ensemble of rewriting rules, that reproduce elements of V.

A L-Systems is the aggregate : L = { V, ω, S, P }

A rewriting rule (we will shorten that one in simply rule) is defined like this:
Axiom -> Production
Where Axiom is a element from V+ and Production is a valid sequence of elements from V+.

L-Systems from scratch

So let’s precise more concretly.

Let V = { F, A }
Let S = { }
Let P = {
F -> FA
A -> F
}
Let ω = F

Let n = 0 .. 5 be the iteration number.

n = 0 : F
n = 1 : FA
n = 2 : FAF
n = 3 : FAFFA
n = 4 : FAFFAFAF
n = 5 : FAFFAFAFFAFFA

At each iteration each Axiom that match a rule is replaced/rewritted by its production. This is this behavior allow us to express easly recusive patterns like fractals for example.

A practical Case

In this blog post we will mostly talk about using L-Systems for generate fractals and rendering stuff but the core of this system is a lot more generic than that and can be used for a lot of other uses cases.

We can easly interpret an L-Systems into graphics using technics such as turtle graphics. All we have to do is define a subset of V+ that will compose an ensemble of commands wich will be interpreted by the turtle. So let define them:

F : moving forward
+ : rotate right
- : rotate left

And only with this we can start drawing our firsts fractals !
(For convenience purposes I will define L-Systems as follow, starting from now)

ω = F
F -> F+F-F-F+F

n = 0 : F
n = 1 : F+F-F-F+F
n = 2 : 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

L-System fractal example