WPF, C#, Objective C and a little Math

L-Systems and Turtle Graphics in C# and WPF

I’m currently interested in L-Systems and how to generate content using them. WPF and .Net provides nice tools to create an application which I am going to use to my experiments. It is similar to Online experiments with L-Systems using JSXGraph but more “realtime”. Simple context-free, non-parametrized L-Systems are supported.

And this is how the result looks like:

When started, I defined ITurtle interface, which supports all needed function like Forward or TurnLeft. Now, moving the turtle along the Path could be implemented like:

var functions = new Dictionary<char, Action<ITurtle>>();
functions['F'] = t => t.Forward(true);
functions['f'] = t => t.Forward(false);
functions['-'] = t => t.TurnLeft();
functions['+'] = t => t.TurnRight();
functions['['] = t => t.Push();
functions[']'] = t => t.Pop();

foreach (char c in Path)

To display Turtle Graphics I created TurtlePathDisplay class which inherits FrameworkElement, and contains 2 dependency property – Path and TurtleParameters.

Every time when one of these 2 properties changed, TurtlePathDisplay updates its visual representation. It also modifies its size and render transformation so, that hosting Visual is visible. In application main window TurtlePathDisplay is placed inside Viewbox, so it final render is scaled to fit to current UI layout:

    Path="{Binding Path, 
      Source={StaticResource PathGenerator}, Mode=OneWay}"
    TurtleParameters="{Binding TurtleParameters, 
      Source={StaticResource PathGenerator}, Mode=OneWay}"/>

Here PathGenerator is an object which provides Path and TurtleParameters. But the most interesting role of this object is to generate Path based on user definition of some L-system. This is done with help of PathParameters class. This class inherits from TurtleParameters class and contains the complete definition of L-system and turtle parameters:

public class PathParameters : TurtleParameters
    // List of rules - used to transform words.
    public string[] Rules { get; set; }

    // List of symbols - used to transform
    // final word in the turtle command path.
    public string[] Symbols { get; set; }

    // Initial word.
    public string Axiom { get; set; }

    // Now many iteration shall be done.
    public int Level { get; set; }

public class TurtleParameters
    // Value to move forward.
    public double Distance { get; set; }

    // Turn angle (grads).
    public double TurnAngle { get; set; }

    // Start turtle angle (grads).
    public double StartAngle { get; set; }

    // Thickness of the first element.
    public double StartThickness { get; set; }

    // Thickness of the last element.
    public double EndThickness { get; set; }

    // If true, random colors will be used to draw lines.
    public bool UseRandomColors { get; set; }

    // Handler, which will be called after every turtle action.
    public Action<TurtleParameters> StepHandler { get; set; }

So L-system shall be defined by the User in the application UI in a form of PathParameters initialization, for example like this:

Axiom = "F",
Rules = new string[]
    "+: "
Symbols = new string[]
Level = 9,
Distance = 10,
TurnAngle = 15,
StepHandler = p => {p.Distance *= 1.02;}

This text is binded to PathGenerators ParametersSourceCode property, so every time it changes, PathGenerator compiles source code containing defined initialization, and builds new path using initialized PathParameter object. CSharpCodeProvider make it work really good and fast, so I even do not need to implements any background compilation and keep application respond almost realtime!

In case of any compilation error the application displays a list of errors, so it can be easily found if something goes wrong.

Additionally, the application contains some predefined definitions of L-System (mostly taken from the Internet) and an option to modify generated Path by hand.

This archive contains VS 2008 project and the executable of my small application.

Categorised as: L-Systems, Turtle Graphics, WPF

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>