Skip to Main Content Skip to Search
Product Documentation

plot::LsysLindenmayer systems

plot::Lsys creates Lindenmayer systems, i.e., string rewriting systems controlling turtle graphics.

→ Examples

Call:

plot::Lsys(alpha, start, trans, ..., <a = amin .. amax>, Options)

Parameters:

alpha

Angle (in radians) for turning commands. Animatable.
alpha is equivalent to the attribute RotationAngle.

start

String used as the starting rule.
start is equivalent to the attribute StartRule.

trans, ...: 

Iteration and Turtle command rules (see below).
trans, ... is equivalent to the attributes IterationRules, TurtleRules.

See Also:

plot, plot::copy, plot::Turtle

Details:

Example 1

As a very simple system, we consider the following iteration rule: “replace each line forward by the sequence “line forward, move forward without painting, line forward.””:

l := plot::Lsys(0, "F", "F" = "FfF"):

Note that we do not provide an iteration rule for "f". This means “leave f alone, do not change it.”

The start state is displayed by plotting the system after zero generations:

l::Generations := 0:
plot(l)

MuPAD graphics

Increasing the number of generations, we see the effect of our transformation rule:

l::Generations := 1:
plot(l)

MuPAD graphics

l::Generations := 2:
plot(l)

MuPAD graphics

l::Generations := 3:
plot(l)

MuPAD graphics

The following variant of this simple example produces approximations to the Cantor set:

l := plot::Lsys(0, "F", "F" = "FfF", "f" = "fff"):
plot(l)

MuPAD graphics

Example 2

To get more interesting examples, we include rotations into our rules:

l := plot::Lsys(PI/2, "F-F-F-F", "F" = "F-F+F+FF-F-F+F",
                Generations = 3)

plot::Lsys(PI/2, "F-F-F-F", IterationRules = ["F" = "F-F+F+FF-F-F+F"], TurtleRules = [], Generations = 3)

As you can see, plot::Lsys has detected that our rule is an iteration rule. We could have used this syntax directly when creating the object. We have not given turtle rules, so the defaults are used:

plot(l)

MuPAD graphics

Example 3

The Peano curve is a famous example of a space filling curve. In the limit process, increasing the number of iterations while decreasing the length of the forward steps, it actually fills the plane. There are different constructions known, the one shown here fills a square tilted by 45Symbol::deg:

peano := plot::Lsys(PI/2, "F", "F" = "F+F-F-F-F+F+F+F-F"):

The transformation rule says to replace each straight line with the following construction:

peano::Generations := 1:
plot(peano)

MuPAD graphics

After a few iterations, the lines already get very close to one another:

peano::Generations := 5:
plot(peano)

MuPAD graphics

Example 4

Many L-systems contain different types of lines: While they are drawn exactly the same, their transformation rules are different from one another. The following example shows an image similar to the Sierpinski triangle:

l := plot::Lsys(PI/3, "R", "L" = "R+L+R", "R" = "L-R-L",
                         "L" = Line, "R" = Line,
                Generations = 7):
plot(l)

MuPAD graphics

Example 5

The Push and Pop operations can be used to draw “arms” in an L-system:

plot(plot::Lsys(23*PI/180, "F", "F" = "FF-[-F+F+F]+[+F-F-F]",
                Generations = 4))

MuPAD graphics

Example 6

L-systems have been used to simulate plant growth. We show an example here that uses the symbols B, H, and G to change the color of lines:

l := plot::Lsys(PI/9, "BL", "L" = "BR[+HL]BR[-GL]+HL",
                "R" = "RR", "L" = Line, "R" = Line,
                "B" = RGB::Brown, "H" = RGB::ForestGreen,
                "G" = RGB::SpringGreen, Generations = 6):
plot(l)

MuPAD graphics

The attribute Generations can be animated. This way, we can actually make the “plant” “grow:”

plot(plot::Lsys(a*PI/45, "BL", "L" = "BR[+HL]BR[-GL]+HL", "R" = "RR",
                "L" = Line, "R" = Line, "B" = RGB::Brown,
                "H" = RGB::ForestGreen, "G" = RGB::SpringGreen,
                 Generations = a, a = 1 .. 6)):

MuPAD graphicsimage

Example 7

L-systems can display a couple of popular fractals. One example is the Koch snowflake, generated by replacing each straight line with a straight line, followed by a left turn of 60Symbol::deg, another straight line, a right turn of 120Symbol::deg, another straight line, another left turn of 60Symbol::deg and a final straight line:

koch := plot::Lsys(PI/3, "F--F--F", "F" = "F+F--F+F"):

The starting rule has been chosen to be an equilateral triangle:

koch::Generations := 0:
plot(koch)

MuPAD graphics

The first generation looks like this:

koch::Generations := 1:
plot(koch)

MuPAD graphics

The limit is pretty well approximated after five generations:

koch::Generations := 5:
plot(koch)

MuPAD graphics

Finally, we use plot::modify and the "StepLength" slot to show the first couple of iterations superimposed on one another:

colors := [RGB::Red, RGB::Green, RGB::Blue, RGB::Yellow, RGB::DimGrey]:
plot(plot::modify(koch, Generations = i,
                  StepLength = 3^(-i),
                  LineColor = colors[i+1]) $ i = 0..4)

MuPAD graphics

Example 8

Another well-known example of a fractal generated by an L-system is Heighway's Dragon curve. Informally, it is generated by “drawing a right angle and then replacing each right angle by a smaller right angle” (Gardner). It has been used in the book Jurassic Park by Michael Crichton and thereby got another nickname, the “Jurassic Park fractal:”

plot(plot::Lsys(PI/2, "L", "L" = "L+R+", "R" = "-L-R",
                "L" = Line, "R" = Line, Generations = 9))

MuPAD graphics

It is interesting to note that the iteration rules of this curve are equivalent to appending a mirrored copy of the curve to its end:

plot(plot::Lsys(PI/2, "L", "L" = "L+R+", "R" = "-L-R",
                "L" = Line, "R" = Line, Generations = a,
                a = 1..9))

MuPAD graphicsimage

Example 9

While the L-system of the previous example corresponds to the definition found in the literature, the images in at least one popular source show another system (while the definition given is the one from above), namely:

plot(plot::Lsys(PI/4, "X-F-Y", "X" = "X+F+Y", "Y" = "X-F-Y",
                "X" = Line, "Y" = Line, Generations = 9)):

MuPAD graphics

Example 10

An L-system may contain letters that are not meant to show in the final graphic, so they form some sort of “markers” for subsequent iteations. For this purpose, you may use the turtle rule Noop:

plot(plot::Lsys(PI/12,
                "X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X",
                "X" = "[F+F+F+F[---X-Y]+++++F++++++++F-F-F-F]",
                "Y" = "[F+F+F+F[---Y]+++++F++++++++F-F-F-F]",
                "X" = Noop, "Y" = Noop,
                Generations = 3))

MuPAD graphics

plot(plot::Lsys(PI/2, "FB",
                "A" = "FBFA+HFA+FB-FA", "B" = "FB+FA-FB-JFBFA",
                "F" = "", "H" = "-", "J" = "+",
                "A" = Noop, "B" = Noop, "H" = Noop, "J" = Noop))

MuPAD graphics

Example 11

Using this rule, we can use the following formulation of the popular Hilbert curve due to Ken Philip:

plot(plot::Lsys(PI/2, "x", "x" = "-yF+xFx+Fy-", "y" = "+xF-yFy-Fx+",
                "x" = Noop, "y" = Noop))

MuPAD graphics

To animate the creation process of the Hilbert curve, we adjust the length of the lines to the current number of iteration steps:

plot(plot::Lsys(PI/2, "x", "x" = "-yF+xFx+Fy-", "y" = "+xF-yFy-Fx+",
                "x" = Noop, "y" = Noop,
                Generations = i, StepLength = 1/(2^i-1),
                i = 1..6, Frames = 6))

MuPAD graphicsimage

Example 12

In some cases, systems will need small angles and long strings in order to specify the desired directions. Take for example the following system:

plot(plot::Lsys(7*PI/15, "F", "F"="F+F--F+F",
                Generations=4))

MuPAD graphics

The rotations to the right use an angle of (7*PI)/15, while that to the left (the sharp spike) is a turn of (14*PI)/15. It would look more natural, however, to have the turtle start to the right, i.e., at an angle of -PI/2. Since no multiple of (7*PI)/15 is equal to PI/2 modulo 2*PI, this requires that we use a smaller angle, adjusting our iteration rule:

plot(plot::Lsys(7*PI/30,"+++++++++++++++F",
                "F"="F++F----F++F", Generations=4))

MuPAD graphics

Background:

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS