Skip to Main Content Skip to Search
Product Documentation

plot::Turtle“turtle graphics” (imperative drawings)

Turtle graphics define a line drawing by a sequence of commands to an abstract robot.

→ Examples

Call:

plot::Turtle(commands, <a = amin .. amax>, Options)

Parameters:

commands

A list of commands. See below for command definitions.
commands is equivalent to the attribute CommandList.

See Also:

plot, plot::copy, plot::Lsys

Details:

For long command sequences, it is highly recommended to give the commands directly using the syntax above or by setting the CommandList attribute directly.

Example 1

A square can be drawn by four times moving forward, each time turning right 90°:

plot(plot::Turtle([Forward(1), Right(PI/2),
                   Forward(1), Right(PI/2),
                   Forward(1), Right(PI/2),
                   Forward(1), Right(PI/2)]))

MuPAD graphics

Using the $ operator, this command list can be written much shorter:

plot(plot::Turtle([(Forward(1), Right(PI/2))$4]))

MuPAD graphics

In the same fashion, we can draw any regular n-sided polygon:

n := 7:
plot(plot::Turtle([(Forward(1), Right(2*PI/n)) $ n]))

MuPAD graphics

Example 2

The distance to move may contain an animation parameter:

plot(plot::Turtle([Forward(1+a), Right(PI/2),
                   Forward(1-2*a), Right(PI/2),
                   Forward(1+3*a), Right(PI/2),
                   Forward(1-4*a), Right(PI/2),
                   Forward(1+5*a)], a=0..2))

MuPAD graphicsimage

Likewise, the angle can be animated:

plot(plot::Turtle([(Forward(1), Right(a))$10],
                  a = 0.25..2.5))

MuPAD graphicsimage

Example 3

It is also possible to successively append commands to the list:

t := plot::Turtle()

plot::Turtle([])

t::forward(1)

plot::Turtle([Forward(1)])

for i from 1 to 9 do
  t::left(3*PI/5);
  t::forward(1);
end_for

`plot::Turtle([Forward(1), Left((3*PI)/...5), Forward(1)])`

plot(t)

MuPAD graphics

Example 4

As an extension to the original turtle model, the line color may be changed while plotting:

t := plot::Turtle():
t::setLineColor(RGB::Red):
t::forward(1):
p := float(PI/5):
for i from 1 to 9 do
  t::left(108*PI/180);
  t::setLineColor([cos(i*p), sin(i*p), 0.0]);
  t::forward(1);
end_for;

`plot::Turtle([LineColor([1.0, 0.0, 0.0...]), Forward(1)])`

plot(t)

MuPAD graphics

Note that the color within one line segment is constant.

Example 5

Another extension to the turtle model is that plot::Turtle supports a stack of saved states, enabling the robot to return to previous positions:

t := plot::Turtle():
t::forward(5):
for i from -3 to 4 do
  t::push();
    t::left(PI/18*i);
    t::forward(3);
  t::pop();
end_for:
plot(t)

MuPAD graphics

Example 6

Using small steps, it is possible to create appealing curves with plot::Turtle:

t := plot::Turtle(LineColor = RGB::Green):
t::forward(2):
for dir in [-1, 1] do
  t::push();
    t::left(dir*PI/30);
    for i from 1 to 10 do
      t::forward(0.2);
      t::left(dir*PI/30);
    end_for;
    t::left(dir*2/3*PI);
    for i from 1 to 10 do
      t::forward(0.2);
      t::left(dir*PI/30);
    end_for;
  t::pop()
end_for:
t::forward(3):
t::setLineColor(RGB::Red):
for dir from -5 to 5 do
  t::push();
    t::left(dir*2*PI/11);
    for i from 1 to 10 do
      t::forward(0.1);
      t::left(PI/30);
    end_for;
    t::left(2*PI/3);
    for i from 1 to 10 do
      t::forward(0.1);
      t::left(PI/30);
    end_for;
  t::pop()
end_for:
plot(t)

MuPAD graphics

  


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