Click here to Skip to main content
15,883,901 members
Articles / Desktop Programming / MFC

Simple Lindenmayer System

Rate me:
Please Sign up or sign in to vote.
4.49/5 (19 votes)
14 Jan 2006CPOL2 min read 54.5K   926   32   3
A simple 2D Lindenmayer system drawing program
Sample Image - l_system.jpg

Introduction

A Simple Program to Demonstrate 2D Lindenmayer Systems

A good source for more information is Google and these sites:

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
] = pop, return to last pushed 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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Canada Canada
Professional Programmer living in Beautiful Vancouver, BC, Canada.

Comments and Discussions

 
QuestionCould I use it as a reference? Pin
Member 880207011-Apr-12 10:15
Member 880207011-Apr-12 10:15 
Generalpush and pop added Pin
arussell14-Jan-06 13:01
professionalarussell14-Jan-06 13:01 
Generalcool... Pin
Nitron24-Oct-05 5:14
Nitron24-Oct-05 5:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.