L-Systems in C vs JavaScript

June 10, 2025
  • #blog
  • #l_systems

In my previous post, I embedded a simple L-system visualizer written in vanilla TypeScript and compiled down to ES2020 JavaScript. The combination of JavaScript's language features and the browser environment made this a fairly straightforward task.

Generating the strings is trivial:

First, define the production rules as a map of characters to strings

1const rules: Record<string, string> = {
2  'F': 'G[-F]+F',
3  'G': 'GG'
4};

Then, apply the rules in a .map operation to successive iterations, starting from the axiom:

1let str = "F";
2for (const i = 0; i < MAX_ITERATIONS; i++) {
3  str = str.split('').map(chr => rules[chr] ?? chr).join('');
4}

With the strings generated, producing an image was a matter of interpreting those strings as SVG Path commands with the help of a little vector math.

Approaching this problem in C is a much bigger task. Right off the bat, we don't have convenient abstractions like hash maps or automatic memory management available to us; we have to build just about everything from scratch. This might sound tedious, but as an educational experience, I found the whole process quite gratifying! Over the next few posts, I'll detail my learning journey through this implementation.