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.
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:
,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.