| Contents | Index |
Turtle graphics define a line drawing by a sequence of commands to an abstract robot.
Call:
plot::Turtle(commands, <a = amin .. amax>, Options)
Parameters:
|
commands: |
A list of commands. See below for command definitions. |
See Also:
Details:
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
.
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.
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.
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
-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)


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 |