Title: | Turtle Graphics |
---|---|
Description: | An implementation of turtle graphics <http://en.wikipedia.org/wiki/Turtle_graphics>. Turtle graphics comes from Papert's language Logo and has been used to teach concepts of computer programming. |
Authors: | Anna Cena [aut] , Marek Gagolewski [aut, cre] , Barbara Zogala-Siudem [aut], Marcin Kosinski [aut], Natalia Potocka [aut] |
Maintainer: | Marek Gagolewski <[email protected]> |
License: | GPL (>= 3) |
Version: | 1.0-8 |
Built: | 2024-11-01 11:16:01 UTC |
Source: | https://github.com/gagolews/turtlegraphics |
Learn computer programming while having a jolly time
Move the Turtle with commands that are relative to its own position, e.g., "move forward 100 pixels" or "turn right 30 degrees". From these building blocks you can build more complex shapes like circles, fractals, etc. Combined with R control flow, functions, and recursion, the idea of Turtle graphics allows students to get familiar with computer programming in a very accessible and pleasant way.
Anna Cena [aut]
Marek Gagolewski [aut]
Marcin Kosinski [aut]
Natalia Potocka [aut]
Barbara Zogala-Siudem [aut, cre]
Other TurtleGraphics: turtle_do
,
turtle_getpos
, turtle_goto
,
turtle_init
, turtle_move
,
turtle_param
, turtle_reset
,
turtle_show
, turtle_status
,
turtle_turn
, turtle_up
turtle_do
evaluates an R expression
with the Turtle temporarily hidden (for performance reasons).
turtle_do(expr)
turtle_do(expr)
expr |
expression to evaluate |
The terrarium must be initialized prior to using
these functions, see turtle_init
.
In order to decrease the evaluation time of expr
,
it is evaluated with Turtle temporarily hidden.
Basically it means that if a Turtle image is visible (see
turtle_show
and turtle_hide
) turtle_do
removes it, evaluates expr
and redraws it on the function exit.
Other TurtleGraphics: TurtleGraphics-package
,
turtle_getpos
, turtle_goto
,
turtle_init
, turtle_move
,
turtle_param
, turtle_reset
,
turtle_show
, turtle_status
,
turtle_turn
, turtle_up
turtle_init() turtle_do({ for (i in 1:4) { turtle_forward(50) turtle_right(90) } })
turtle_init() turtle_do({ for (i in 1:4) { turtle_forward(50) turtle_right(90) } })
turtle_getpos
returns the Turtle's current position on the plane.
turtle_getangle
returns the Turtle's current direction,
in degrees. An angle of 0 represents a north-facing Turtle.
turtle_getpos() turtle_getangle()
turtle_getpos() turtle_getangle()
The terrarium must be initialized prior to using these functions,
see turtle_init
.
Both functions return a (named) numeric vector.
turtle_getpos
returns a vector of length two which
specifies the x
and y
coordinates.
The turtle_getangle
returns the angle.
Other TurtleGraphics: TurtleGraphics-package
,
turtle_do
, turtle_goto
,
turtle_init
, turtle_move
,
turtle_param
, turtle_reset
,
turtle_show
, turtle_status
,
turtle_turn
, turtle_up
turtle_init() turtle_getpos()["x"] # x coordinate turtle_getpos()["y"] # y coordinate
turtle_init() turtle_getpos()["x"] # x coordinate turtle_getpos()["y"] # y coordinate
turtle_goto
and turtle_setpos
move the Turtle to a
given location without changing its direction.
turtle_setangle
rotates the Turtle to a given (absolute) angle,
where 0 denotes a north-facing Turtle.
turtle_goto(x, y) turtle_setpos(x, y) turtle_setangle(angle)
turtle_goto(x, y) turtle_setpos(x, y) turtle_setangle(angle)
x , y
|
numeric; coordinates specifying new Turtle's location. |
angle |
numeric; rotation angle in degrees. |
The terrarium must be initialized prior to using
these functions, see turtle_init
.
If the given location (x, y) lies outside the terrarium,
the behavior of these functions depends
on the mode
argument in turtle_init
.
turtle_goto
may draw the path between the current Turtle's
position and the new location. Its behavior depends on the current
plot settings,
see turtle_up
, turtle_down
. In case of
turtle_setpos
, however, the path drawing is always disabled.
Other TurtleGraphics: TurtleGraphics-package
,
turtle_do
, turtle_getpos
,
turtle_init
, turtle_move
,
turtle_param
, turtle_reset
,
turtle_show
, turtle_status
,
turtle_turn
, turtle_up
This function creates a new empty plot with the Turtle centered on the board and facing to the north.
turtle_init(width = 100, height = 100, mode = c("error", "clip", "cycle"))
turtle_init(width = 100, height = 100, mode = c("error", "clip", "cycle"))
width |
numeric; plot width. |
height |
numeric; plot height. |
mode |
character string; one of |
The mode
argument determines what happens if the Turtle
tries to move outside the terrarium.
clip
allows it to do that, but the drawing will be clipped
to the predefined plot region.
error
throws an error.
cycle
makes the Turtle appear on the other side of the board.
After the turtle_init()
function has been called
you can e.g. move the Turtle with the turtle_forward
function, turn its direction with turtle_right
or set display parameters of the Turtle's trace,
see turtle_param
.
Other TurtleGraphics: TurtleGraphics-package
,
turtle_do
, turtle_getpos
,
turtle_goto
, turtle_move
,
turtle_param
, turtle_reset
,
turtle_show
, turtle_status
,
turtle_turn
, turtle_up
turtle_forward
moves the Turtle in forward direction and
turtle_backward
moves the Turtle back.
turtle_move(distance, direction = c("forward", "backward")) turtle_forward(distance) turtle_backward(distance)
turtle_move(distance, direction = c("forward", "backward")) turtle_forward(distance) turtle_backward(distance)
distance |
single numeric value; specifies the distance to make. Negative distance results in moving in the opposite direction. |
direction |
character string; moving direction.
One of |
The Turtle must be initialized prior to using
these functions, see turtle_init
.
These functions make use of the Turtle's display options specified by
the turtle_param
function
(or if not, use the default options set by turtle_init
).
Note that if turtle_up
or turtle_down
was called, the Turtle's trace will be or not be drawn, respectively.
If you are willing to call these functions in an R loop,
you may want to hide the Turtle temporarily (see turtle_hide
and turtle_do
)
before making actual moves. This will increase the drawing performance
significantly.
Other TurtleGraphics: TurtleGraphics-package
,
turtle_do
, turtle_getpos
,
turtle_goto
, turtle_init
,
turtle_param
, turtle_reset
,
turtle_show
, turtle_status
,
turtle_turn
, turtle_up
turtle_init() turtle_left(30) turtle_forward(2) turtle_up() turtle_forward(1) turtle_down() turtle_right(60) turtle_forward(9)
turtle_init() turtle_left(30) turtle_forward(2) turtle_up() turtle_forward(1) turtle_down() turtle_right(60) turtle_forward(9)
Sets the display options for the Turtle's trace. It is possible to change its color, line type and line width.
turtle_param(col = NULL, lwd = NULL, lty = NULL) turtle_col(col) turtle_lwd(lwd) turtle_lty(lty)
turtle_param(col = NULL, lwd = NULL, lty = NULL) turtle_col(col) turtle_lwd(lwd) turtle_lty(lty)
col |
numeric or character; trace color, see e.g. |
lwd |
numeric; trace line width, see |
lty |
numeric; trace line type, see |
The Turtle must be initialized prior to using
this function, see turtle_init
.
Other TurtleGraphics: TurtleGraphics-package
,
turtle_do
, turtle_getpos
,
turtle_goto
, turtle_init
,
turtle_move
, turtle_reset
,
turtle_show
, turtle_status
,
turtle_turn
, turtle_up
turtle_init() turtle_forward(5) turtle_up() turtle_forward(3) turtle_down() turtle_left(90) turtle_forward(5) turtle_param(col = "red", lwd = 2, lty = 2) turtle_forward(5)
turtle_init() turtle_forward(5) turtle_up() turtle_forward(3) turtle_down() turtle_left(90) turtle_forward(5) turtle_param(col = "red", lwd = 2, lty = 2) turtle_forward(5)
This function resets the Turtle's position, direction, and graphical options.
turtle_reset()
turtle_reset()
The Turtle must be initialized prior to using
this function, see turtle_init
.
After a call to this function, the Turtle will be placed in the terrarium's center and it will be directed to the north.
The drawing remains unchanged.
Other TurtleGraphics: TurtleGraphics-package
,
turtle_do
, turtle_getpos
,
turtle_goto
, turtle_init
,
turtle_move
, turtle_param
,
turtle_show
, turtle_status
,
turtle_turn
, turtle_up
turtle_init() turtle_forward(4) turtle_param(col="red", lty=2, lwd=3) turtle_reset() turtle_left(45) turtle_forward(3)
turtle_init() turtle_forward(4) turtle_param(col="red", lty=2, lwd=3) turtle_reset() turtle_left(45) turtle_forward(3)
These functions enable or disable displaying the Turtle's image on the screen.
turtle_show() turtle_hide()
turtle_show() turtle_hide()
The Turtle must be initialized prior to using
this function, see turtle_init
.
It is recommended to hide the Turtle when
performing multiple Turtle moves, for efficiency reasons,
see also turtle_do
.
Other TurtleGraphics: TurtleGraphics-package
,
turtle_do
, turtle_getpos
,
turtle_goto
, turtle_init
,
turtle_move
, turtle_param
,
turtle_reset
, turtle_status
,
turtle_turn
, turtle_up
turtle_init() turtle_forward(4) turtle_hide() turtle_left(30) turtle_forward(3)
turtle_init() turtle_forward(4) turtle_hide() turtle_left(30) turtle_forward(3)
This function gives information about the current Turtle's position, direction, and on display options.
turtle_status()
turtle_status()
The Turtle must be initialized prior to using
this function, see turtle_init
.
Returns a list with three elements.
Other TurtleGraphics: TurtleGraphics-package
,
turtle_do
, turtle_getpos
,
turtle_goto
, turtle_init
,
turtle_move
, turtle_param
,
turtle_reset
, turtle_show
,
turtle_turn
, turtle_up
Turn the Turtle in the given direction by the given angle.
turtle_turn(angle, direction = c("left", "right")) turtle_left(angle) turtle_right(angle)
turtle_turn(angle, direction = c("left", "right")) turtle_left(angle) turtle_right(angle)
angle |
single numeric value; rotation angle in degrees. A negative value turns the Turtle in the opposite direction than the given one. |
direction |
character string; direction of the turn.
Possible values are |
The Turtle must be initialized prior to using
this function, see turtle_init
.
Other TurtleGraphics: TurtleGraphics-package
,
turtle_do
, turtle_getpos
,
turtle_goto
, turtle_init
,
turtle_move
, turtle_param
,
turtle_reset
, turtle_show
,
turtle_status
, turtle_up
turtle_init() turtle_left(30) # equivalent to turtle_turn(30, "left") turtle_right(40) turtle_turn(30, sample(c("left", "right"), 1)) # random turn
turtle_init() turtle_left(30) # equivalent to turtle_turn(30, "left") turtle_right(40) turtle_turn(30, sample(c("left", "right"), 1)) # random turn
When the Turtle moves, it may or may not leave a visible trace. These functions control such a behavior.
turtle_up() turtle_down()
turtle_up() turtle_down()
The Turtle must be initialized prior to using
this function, see turtle_init
.
Other TurtleGraphics: TurtleGraphics-package
,
turtle_do
, turtle_getpos
,
turtle_goto
, turtle_init
,
turtle_move
, turtle_param
,
turtle_reset
, turtle_show
,
turtle_status
, turtle_turn