A simple tree generator

June 4, 2025
  • #blog
  • #l_systems

Since my last post, I've spent my evenings looking into and implementing some simple L-Systems. L-Systems are all about using something called a Formal Grammar to generate patterns of symbols to use as instructions for drawing geometric shapes. Depending on the grammar and the drawing mechanism, you might get geometry that looks a lot like a plant. Astrid Lindenmayer (the inventor of and "L" in L-Systems) created L-systems to describe plants and their growth in the first place: it's a perfect fit for a project about procedural plant generation!

So what does an L-System look like? The wikipedia article on it has all you need, but I am writing this primarily for my own learning experience, so a brief explanation follows.

First, there is the grammar of the system. It describes

  • a set of symbols (the alphabet of the grammar)
  • a starting string (also called the axiom)
  • a list of production rules to repeatedly apply to the starting string. These rules specify a symbol (or pattern of symbols) and a (larger) string of symbols to replace it with. For example, the rule (A → AB) means 'If you find the symbol A, replace it with the string AB.'

Following the grammar, there is the drawing procedure that interprets the string. A simple option here is to interpret the symbols in the alphabet as turtle graphics instructions.

For example, a simple binary tree L-System looks like this:

  • Alphabet: F G [ ]
  • Axiom: F
  • Rules: (F -> G[+F]-F) (G -> GG)

We then pick an angle a (degrees) and distance d (units) and interpret these symbols as

  • F and G: move forward d units
  • [: push the current coordinates and angle to the stack
  • ]: pop the stack (reverting to those the coordinates and angle)
  • +: turn right a degrees
  • -: turn left a degrees
  • Ignore any other symbols

I've written a simple SVG-based visualizer for some of the more common L-systems below. It includes controls to adjust the iteration count (the number of times to apply the grammar's production rules) and the turn angle. Playing around with these settings and seeing different patterns emerge is quite fun!

I'm able to spend an hour or so each evening on these posts. Putting together this one took almost a full month... and I absolutely loved it. I started by implementing a very naive binary tree system in C and Raylib, then re-implemented it a few times, iterating towards a stochastic, parametric grammar (which I'll get into in future posts!). I had so much fun just playing around with settings and generating different random trees that I decided I had to include an interactive visualizer in this post. Another few weeks of writing a generic L-system visualizer, and here we are!