Documentation Center

  • Trial Software
  • Product Updates

plot::Turtle

"turtle graphics" (imperative drawings)

Use only in the MuPAD Notebook Interface.

This functionality does not run in MATLAB.

Syntax

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

Description

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

plot::Turtle defines a graphic by sending movement commands to an abstract robot. This robot starts heading up and standing at the origin, with its pen ready for drawing ("down") and the line color taken from the attribute LineColor.

The following commands are known to the robot:

  • Left(α)

    Turn left by the angle α (in radians).

  • Right(α)

    Turn right by the angle α (in radians).

  • Forward(d)

    Move forward distance d.

  • Up

    Lift the "pen", i.e., subsequent movement commands do not draw lines.

  • Down

    Lower the "pen", i.e., subsequent movement commands do draw lines.

  • Push

    Remember the current state (position, angle, line color).

  • Pop

    Restore the last remembered state and remove it from the list of remembered states.

  • Noop

    This command is ignored.

  • LineColor(c)

    Set the line color to the colorc.

The commands not taking an argument may also be entered with empty parentheses () after, e.g., Push().

A plot::Turtle-object can be manipulated dynamically by calling its methods left, right, forward, penUp, penDown, push, pop, and setLineColor, with the obvious connections to the commands above. These methods append a new command to the end of the list. Cf. Example 3.

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

Both angles and distances can be animated. Colors can not.

Attributes

AttributePurposeDefault Value
AffectViewingBoxinfluence of objects on the ViewingBox of a sceneTRUE
AntiAliasedantialiased lines and points?TRUE
Colorthe main colorRGB::Blue
CommandListturtle movement commands[]
Framesthe number of frames in an animation50
Legendmakes a legend entry 
LegendTextshort explanatory text for legend 
LegendEntryadd this object to the legend?FALSE
LineColorcolor of linesRGB::Blue
LineWidthwidth of lines0.35
LineStylesolid, dashed or dotted lines?Solid
LinesVisiblevisibility of linesTRUE
Namethe name of a plot object (for browser and legend) 
ParameterEndend value of the animation parameter 
ParameterNamename of the animation parameter 
ParameterBegininitial value of the animation parameter 
ParameterRangerange of the animation parameter 
PointSizethe size of points1.5
PointStylethe presentation style of pointsFilledCircles
PointsVisiblevisibility of mesh pointsFALSE
TimeEndend time of the animation10.0
TimeBeginstart time of the animation0.0
TimeRangethe real time span of an animation0.0 .. 10.0
Titleobject title 
TitleFontfont of object titles[" sans-serif ", 11]
TitlePositionposition of object titles 
TitleAlignmenthorizontal alignment of titles w.r.t. their coordinatesCenter
TitlePositionXposition of object titles, x component 
TitlePositionYposition of object titles, y component 
VisiblevisibilityTRUE
VisibleAfterobject visible after this time value 
VisibleBeforeobject visible until this time value 
VisibleFromToobject visible during this time range 
VisibleAfterEndobject visible after its animation time ended?TRUE
VisibleBeforeBeginobject visible before its animation time starts?TRUE

Examples

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)]))

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

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

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

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

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))

Likewise, the angle can be animated:

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

Example 3

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

t := plot::Turtle()

t::forward(1)

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

plot(t)

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(t)

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)

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)

Parameters

commands

A list of commands. See below for command definitions.

commands is equivalent to the attribute CommandList.

a

Animation parameter, specified as a = amin..amax, where amin is the initial parameter value, and amax is the final parameter value.

See Also

MuPAD Functions

MuPAD Graphical Primitives

Was this topic helpful?