12,299,109 members (49,179 online)
alternative version

39.8K views
31 bookmarked
Posted

# Simple Lindenmayer System

, 14 Jan 2006 CPOL
 Rate this:
A simple 2D Lindenmayer system drawing program

## Introduction

### A Simple Program to Demonstrate 2D Lindenmayer Systems

Lindenmayer systems are a type of string re-writing systems used to create fractals. They were developed to describe natural systems like trees. You start with simple rules and apply them over and over to the resulting string. Lindenmayer systems are a type of fractal as the rules are applied to themselves repeatedly to create the patterns.

For example:

```rule 1 a:b
rule 2 b:ba

Step 1 a
Step 2 b
Step 3 ba
Step 4 bab
Step 5 babba```

To actually draw something, the rules include commands:

```F = draw forward 1 segment
f = move forward one segment (no drawing)
+ = rotate plus degrees
- = rotate minus degrees
B = Draw backwards
b = move backwards
[ = push, remember current position

For example, a Kock curve can be drawn with this rule:

```F:F+F--F+FF
,an initial Angle of 90, initial string of F. ```

The push and pop commands are used to create branches, for example for a tree.

### How the Program Works

The program applies the rules to the initial string to create a long string of commands. Then the created string is used to draw the string by simply following the instructions in the string. Any characters in the string that are not commands are ignored.

The only interesting functions are `int CL_systemDoc::MakeCurveString() `found in the file l_systemDoc.cpp. This is the function that applies the rules to the initial string that creates the final string that will be used to draw the pattern. Unlike many fractal generators, this is an iterative process, not a recursive process. `int CL_systemView::DrawCurveStringRecurse `draws the string. Recursion is used to support the push and pop commands. If push and pop were not supported, then a simple `for `loop could scan the drawing string to create the final image.

A rule looks like: X:Ff+-X
The second character must be a colon ':'.
The first character gets replaced with the text after the colon.

The File/Curve Parms lets you change all aspects of the pattern.

• Rotate Angle: The amount that a = - rotates the direction of drawing by
• Iterations: The number of times to apply the rules to the initial string
• Length: The length, in pixels, of each line segment that gets drawn
• Initial Angle: The angle the first line segment is drawn at
• Initial State: The string that the rules are applied to

Enter a new string in the text box after the ADD button, then press the ADD button to add a new rule to the end.
Highlight a rule in the list-box, then press the DELETE button to delete the rule.

To draw a curve, simply click the mouse in the main window to pick the starting point.

Always start a new pattern with a number of iterations under 5. Some can take a LONG time once the iterations gets over 5 or 6. You will have to make the length shorter as the iteration goes up to fit the pattern on the screen.

## Share

Professional Programmer living in Beautiful Vancouver, BC, Canada.

## You may also be interested in...

 First Prev Next
 Could I use it as a reference? Member 880207011-Apr-12 10:15 Member 8802070 11-Apr-12 10:15
 push and pop added arussell14-Jan-06 13:01 arussell 14-Jan-06 13:01
 cool... Nitron24-Oct-05 5:14 Nitron 24-Oct-05 5:14
 Last Visit: 31-Dec-99 18:00     Last Update: 28-May-16 11:06 Refresh 1