Example 1: algae Lindenmayer's original L-system for modelling the growth of algae. :
variables : A B :
constants : none :
axiom : A :
rules : (A → AB), (B → A) which produces: :
n = 0 : A :
n = 1 : AB :
n = 2 : ABA :
n = 3 : ABAAB :
n = 4 : ABAABABA :
n = 5 : ABAABABAABAAB :
n = 6 : ABAABABAABAABABAABABA :
n = 7 : ABAABABAABAABABAABABAABAABABAABAAB
Example 1: algae, explained n=0: A start (axiom/initiator) / \ n=1: A B the initial single A spawned into AB by rule (A → AB), rule (B → A) couldn't be applied /| \ n=2: A B A former string AB with all rules applied, A spawned into AB again, former B turned into A / | | | \ n=3: A B A A B note all A's producing a copy of themselves in the first place, then a B, which turns ... / | | | \ | \ \ n=4: A B A A B A B A ... into an A one generation later, starting to spawn/repeat/recurse then The result is the sequence of
Fibonacci words. If one counts the length of each string, the
Fibonacci sequence of numbers is obtained (skipping the first 1, due to the choice of axiom): : 1 2 3 5 8 13 21 34 55 89 ... If it is not desired to skip the first 1, axiom
B can be used. That would place a
B node before the topmost node (
A) of the graph above. For each string, if one counts the
k-th position from the left end of the string, the value is determined by whether a multiple of the
golden ratio falls within the interval (k-1, k). The ratio of A to B likewise converges to the golden mean. This example yields the same result (in terms of the length of each string, not the sequence of
As and
Bs) if the rule (
A →
AB) is replaced with (
A →
BA), except that the strings are mirrored. This sequence is a
locally catenative sequence because G(n)=G(n-1)G(n-2), where G(n) is the
n-th generation.
Example 2: fractal (binary) tree •
variables : 0, 1 •
constants: "[", "]" •
axiom : 0 •
rules : (1 → 11), (0 → 1[0]0) The shape is built by
recursively feeding the axiom through the production rules. Each character of the input string is checked against the rule list to determine which character or string to replace it with in the output string. In this example, a '1' in the input string becomes '11' in the output string, while '[' remains the same. Applying this to the axiom of '0', one gets: It can be seen that this string quickly grows in size and complexity. This string can be drawn as an image by using
turtle graphics, where each symbol is assigned a graphical operation for the turtle to perform. For example, in the sample above, the turtle may be given the following instructions: • 0: draw a
line segment ending in a leaf • 1: draw a line segment • [: push position and angle, turn left 45 degrees • ]: pop position and angle, turn right 45 degrees The push and pop refer to a
LIFO stack (more technical grammar would have separate symbols for "push position" and "turn left"). When the turtle interpretation encounters a '[', the current position and angle are saved, and are then restored when the interpretation encounters a ']'. If multiple values have been "pushed," then a "pop" restores the most recently saved values. Applying the graphical rules listed above to the earlier recursion, one gets:
Example 3: Cantor set :
variables : A B :
constants : none :
start : A {starting character string} :
rules : (A → ABA), (B → BBB) Let
A mean "draw forward" and
B mean "move forward". This produces the famous
Cantor's fractal set on a real straight line
R.
Example 4: Koch curve A variant of the
Koch curve which uses only right angles. :
variables : F :
constants : + − :
start : F :
rules : (F → F+F−F−F+F) Here, F means "draw forward", + means "turn left 90°", and − means "turn right 90°" (see
turtle graphics). :
n = 0: :: F :: :
n = 1: :: F+F−F−F+F :: :
n = 2: :: F+F−F−F+F+F+F−F−F+F−F+F−F−F+F−F+F−F−F+F+F+F−F−F+F :: :
n = 3: :: F+F−F−F+F+F+F−F−F+F−F+F−F−F+F−F+F−F−F+F+F+F−F−F+F+ ::F+F−F−F+F+F+F−F−F+F−F+F−F−F+F−F+F−F−F+F+F+F−F−F+F− :: F+F−F−F+F+F+F−F−F+F−F+F−F−F+F−F+F−F−F+F+F+F−F−F+F− :: F+F−F−F+F+F+F−F−F+F−F+F−F−F+F−F+F−F−F+F+F+F−F−F+F+ :: F+F−F−F+F+F+F−F−F+F−F+F−F−F+F−F+F−F−F+F+F+F−F−F+F ::
Example 5: Sierpinski triangle The
Sierpinski triangle drawn using an L-system. :
variables : F G :
constants : + − :
start : F−G−G :
rules : (F → F−G+F+G−F), (G → GG) :
angle : 120° Here, F and G both mean "draw forward", + means "turn left by angle", and − means "turn right by angle". File:Sierpinski Triangle (from L-System, 2 iterations).png|n = 2 File:Sierpinski Triangle (from L-System, 4 iterations).png|n = 4 File:Sierpinski Triangle (from L-System, 6 iterations).png|n = 6 It is also possible to approximate the
Sierpinski triangle using a
Sierpiński arrowhead curve L-system. :
variables : A B :
constants : + − :
start : A :
rules : (A → B−A−B), (B → A+B+A) :
angle : 60° Here, A and B both mean "draw forward", + means "turn left by angle", and − means "turn right by angle" (see
turtle graphics).
Example 6: dragon curve The
dragon curve drawn using an L-system. :
variables : F G :
constants : + − :
start : F :
rules : (F → F+G), (G → F-G) :
angle : 90° Here, F and G both mean "draw forward", + means "turn left by angle", and − means "turn right by angle".
Example 7: fractal plant :
variables : X F :
constants : + − [ ] :
start : -X :
rules : (X → F+X]-X]-F[-FX]+X), (F → FF) :
angle : 25° First one needs to initialize an empty stack. This follows the LIFO (Last in, First Out) method to add and remove elements. Here, F means "draw forward", − means "turn right 25°", and + means "turn left 25°". X does not correspond to any drawing action and is used to control the evolution of the curve. The square bracket "[" corresponds to saving the current values for position and angle, so the position and angle are pushed to the top of the stack, when the "]" token is encountered, the stack is popped and the position and angle are reset. Every "[" comes before every "]" token. ==Variations==