#lindenmayer-systems #lsystem #lindenmayer #alphabet #dcc


An implementation of a Lindenmayer system together with some rendering tools

11 unstable releases

0.6.3 May 17, 2021
0.6.2 Feb 10, 2020
0.6.1 Nov 2, 2019

#323 in Data structures

Download history 13/week @ 2022-04-23 47/week @ 2022-04-30 57/week @ 2022-05-07 188/week @ 2022-05-14 36/week @ 2022-05-21 69/week @ 2022-05-28 67/week @ 2022-06-04 2/week @ 2022-06-11 3/week @ 2022-06-18 1/week @ 2022-06-25 13/week @ 2022-07-02 24/week @ 2022-07-09 11/week @ 2022-07-16 23/week @ 2022-07-23 23/week @ 2022-07-30

82 downloads per month

MIT/Apache and maybe AGPL-3.0-or-later



Build Status codecov dependency status

A crate for working with Lindenmayer systems.


An L-System consists of an alphabet of symbols that can be used to make strings, a collection of production rules that expand each symbol into a larger string of symbols, an initial axiom string from which to begin construction, and a mechanism for transforming the generated strings into geometric structures.

Algae example

Lindenmayer's original L-System for modelling the growth of Algae had variables A and B, axiom A, and production rules A -> AB, B -> A. Iterating this system produces the following output:

  1. A
  2. AB
  3. ABA
  4. ABAAB

Basic usage

Put the following in your Cargo.toml:

dcc-lsystem = "0.6"


An L-system is represented by an instance of LSystem. To create a barebones LSystem, the LSystemBuilder struct is useful. The following example shows an implementation of Lindenmayer's Algae system.

use dcc_lsystem::LSystemBuilder;

let mut builder = LSystemBuilder::new();

// Set up the two tokens we use for our system.
let a = builder.token("A");
let b = builder.token("B");

// Set up our axiom (i.e. initial state)

// Set the transformation rules
builder.transformation_rule(a, vec![a,b]); // A -> AB
builder.transformation_rule(b, vec![a]);   // B -> A

// Build our LSystem, which should have initial state A
let mut system = builder.finish();
assert_eq!(system.render(), "A");

// system.step() applies our production rules a single time
assert_eq!(system.render(), "AB");

assert_eq!(system.render(), "ABA");

// system.step_by() applies our production rule a number of times

Rendering L-systems

It is possible to render an L-system into an image or gif. Typically this is done using a turtle - each token in the L-system's state is associated with some movement or rotation (or perhaps something more complicated) of a turtle. The TurtleLSystemBuilder struct offers a convenient way of constructing such renderings.


The Koch curve can be generated using an L-system with 3 symbols: F, +, and -, where F corresponds to moving forwards, + denotes a left rotation by 90°, and - denotes a right rotation by 90°. The system has axiom F and transformation rule F => F+F-F-F+F. This is implemented in the following example.

use image::Rgb;

use dcc_lsystem::turtle::{TurtleLSystemBuilder, TurtleAction};
use dcc_lsystem::renderer::{ImageRendererOptions, Renderer};

let mut builder = TurtleLSystemBuilder::new();

    .token("F", TurtleAction::Forward(30)) // F => go forward 30 units
    .token("+", TurtleAction::Rotate(90))  // + => rotate left 90°
    .token("-", TurtleAction::Rotate(-90)) // - => rotate right 90°
    .rule("F => F + F - F - F + F");

let (mut system, renderer) = builder.finish();
system.step_by(5); // Iterate our L-system 5 times

let options = ImageRendererOptionsBuilder::new()
    .fill_color(Rgb([255u8, 255u8, 255u8]))
    .line_color(Rgb([0u8, 0u8, 100u8]))

    .render(&system, &options)
    .expect("Failed to save koch_curve.png");

The resulting image is shown in the Examples section below.


It is also possible to render a GIF using an L-system. The individual frames of the GIF correspond to partial renderings of the L-system's state.

use image::Rgb;

use dcc_lsystem::renderer::{Renderer, VideoRendererOptions};
use dcc_lsystem::turtle::{TurtleAction, TurtleLSystemBuilder};

fn main() {
    let mut builder = TurtleLSystemBuilder::new();

        .token("F", TurtleAction::Forward(30))
        .token("+", TurtleAction::Rotate(90))
        .token("-", TurtleAction::Rotate(-90))
        .rule("F => F + F - F - F + F");

    let (mut system, renderer) = builder.finish();

    let options = VideoRendererOptionsBuilder::new()
        .fill_color(Rgb([255u8, 255u8, 255u8]))
        .line_color(Rgb([0u8, 0u8, 100u8]))

        .render(&system, &options);

Turtle actions

Currently the following actions are available:

TurtleAction Description
Nothing The turtle does nothing.
Rotate(i32) Rotate the turtle through an angle.
Forward(i32) Move the turtle forwards.
Push Push the turtle's current heading and location onto the stack.
Pop Pop the turtle's heading and location off the stack.
StochasticRotate(Box<dyn Distribution>) Rotate the turtle through an angle specified by some probability distribution.
StochasticForward(Box<dyn Distribution>) Move the turtle forwards through a distance specified by some probability distribution.

The Distribution trait is given by:

pub trait Distribution: dyn_clone::DynClone {
    fn sample(&self) -> i32;

A possible implementation of a Uniform distribution (using the rand crate) is as follows:

use rand::Rng;

pub struct Uniform {
    lower: i32,
    upper: i32,

impl Uniform {
    pub fn new(lower: i32, upper: i32) -> Self {
        Self { lower, upper }

impl Distribution for Uniform {
    fn sample(&self) -> i32 {
        let mut rng = rand::thread_rng();


Examples are located in dcc-lsystem/examples.

Sierpinski Arrowhead

Sierpinski Arrowhead

Koch curve

Koch curve

Dragon curve

Dragon curve

Fractal plant

Fractal plant


Licensed under either of

at your option.


Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.


~108K SLoC