Lexa

WPF, C#, Objective C and a little Math

Flowers with L-System

Today I added filled polygons (called Surfaces) in L-Systems, so now it is possible to define some polygonal stuff, i.e. flowers. Next example is based on flower system from the book (figure 1.26), but with some modifications:

Viewer application was extended – now it is possible to check every build iteration of selected L-System (called steps). So, for the picture above step #3 is selected.

The other new feature is LSystems.Turtle.StringParser class. This is just a helper class, which allows to build modules string from characters string. In case of D0L systems its very convenient to use just simple strings, this was the reason to introduce this class. For example, the following Hilbert 3d Curve:

was generated by the following system using StringParser:

class Hilbert3d : LSystems.Turtle.SystemDefinition, LSystems.IRewriteRules
{
    public class A {}
    public class B {}
    public class C {}
    public class D {}

    private LSystems.Turtle.StringParser parser =
    new LSystems.Turtle.StringParser()
    {
        CharToObject = c =>
        {
            switch (c)
            {
            case 'A': return new A();
            case 'B': return new B();
            case 'C': return new C();
            case 'D': return new D();
            default: return null;
            }
        }
    };

    [LSystems.Production]
    public object Produce(A a)
    {
        return parser.Produce("B-F+CFC+F-D&F^D-F+&&CFC+F+B//");
    }

    [LSystems.Production]
    public object Produce(B a)
    {
        return parser.Produce("A&F^CFB^F^D^^-F-D^|F^B|FC^F^A//");
    }

    [LSystems.Production]
    public object Produce(C c)
    {
        return parser.Produce("|D^|F^B-F+C^F^A&&FA&F^C+F+B^F^D//");
    }

    [LSystems.Production]
    public object Produce(D d)
    {
        return parser.Produce("|CFB-F+B|FA&F^A&&FB-F+B|FC//");
    }

    public object Axiom
    {
        get { return new A(); }
    }
    public int Depth
    {
        get { return 4; }
    }
    public LSystems.RewriteDirection RewriteDirection
    {
        get { return LSystems.RewriteDirection.LeftToRight; }
    }

    // Colors.
    double color = 0;
    double delta = 0.05;

    [LSystems.Turtle.Interpret]
    public void Draw(LSystems.Turtle.F f)
    {
        this.Turtle.SetThicknessAndColor(33, 1, color, 0);
        this.Turtle.Forward(100, true);

        color += delta;
        if (color > 1)
        {
            color = 1;
            delta = -delta;
        }
        else if (color < 0)
        {
            color = 0;
            delta = -delta;
        }
    }
}

StringParser class is able to recognize most basic turtle commands (turns, moves), all custom symbols can be added usingCharToObject function (see example above).

Updated source code can be found in the repository.


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>