2 Value Turtles

▼ |  | Turtle Graphics

1 | Traditional Turtles
2 | Value Turtles

► | 2 | Value Turtles

2.1 | Examples

On this page:

turtles
turtles?
move
draw
turn
turn/ radians
set-pen-width
set-pen-color
merge
clean
turtles-width
turtles-height
turtles-pen-width
turtles-pen-color
turtle-state
restore-turtle-state
turtles-pict
2.1 Examples
radial-turtles
spaced-turtles
neato
regular-poly
regular-polys
spokes
spyro-gyra

Racket
top contents ← prev up next →

## 2 Value Turtles🔗ℹ

(require graphics/value-turtles) | package: htdp-lib

The value turtles are a variation on traditional turtles. Rather than having just a single window where each operation changes the state of that window, in the graphics/value-turtles library, the entire turtles window is treated as a value. This means that each of the primitive operations accepts, in addition to the usual arguments, a turtles-window value; instead of returning nothing, each returns a turtles-window value.

> > procedure
> > (turtles |  | width |  |  |  |
> > |  | height |  |  |  |
> > | [ | init-x |  |  |  |
> > |  | init-y |  |  |  |
> > |  | init-angle]) |  | → |  | turtles?
> width : real?
> height : real?
> init-x : real? = (/ width 2)
> init-y : real? = (/ height 2)
> init-angle : real? = 0

Creates a new turtles window with the given width and height. The remaining arguments specify position of the initial turtle and the direction in radians (where 0 is to the right). The turtle’s pen width is 1.

> procedure (turtles? v) → boolean?
> v : any/c

Determines if v is a turtles drawing.

> procedure (move n turtles) → turtles?
> n : real?
> turtles : turtles?

Moves the turtle n pixels, returning a new turtles window.

> procedure (draw n turtles) → turtles?
> n : real?
> turtles : turtles?

Moves the turtle n pixels and draws a line along the path, returning a new turtles window.

> procedure (turn theta turtles) → turtles?
> theta : real?
> turtles : turtles?

Turns the turtle theta degrees counter-clockwise, returning a new turtles window.

> procedure (turn/radians theta turtles) → turtles?
> theta : real?
> turtles : turtles?

Turns the turtle theta radians counter-clockwise, returning a new turtles window.

> procedure (set-pen-width turtles width) → turtles?
> turtles : turtles?
> width : (real-in 0 255)

Creates a new turtles that draws with the pen width width.

Added in version 1.5 of package htdp-lib.

> procedure (set-pen-color turtles color) → turtles?
> turtles : turtles?
> color : (or/c string? (is-a?/c color%))

Creates a new turtles that draws with the pen color color.

Added in version 1.6 of package htdp-lib.

> procedure (merge turtles1 turtles2) → turtles?
> turtles1 : turtles?
> turtles2 : turtles?

The split and tprompt forms provided by graphics/turtles aren’t needed for graphics/value-turtles, since the turtles window is a value.

Instead, the merge accepts two turtles windows and combines the state of the two turtles windows into a single window. The new window contains all of the turtles of the previous two windows, but only the line drawings of the first turtles argument.

> procedure (clean turtles) → turtles?
> turtles : turtles?

Produces a turtles with the drawing as in turtles, but with zero turtles.

> procedure (turtles-width turtles) → (and/c real? positive?)
> turtles : turtles?

Returns the width of turtles.

> procedure (turtles-height turtles) → (and/c real? positive?)
> turtles : turtles?

Returns the height of turtles.

> procedure (turtles-pen-width turtles) → (real-in 0 255)
> turtles : turtles?

Returns the current width of the pen that the turtles use to draw.

Added in version 1.5 of package htdp-lib.

> procedure (turtles-pen-color turtles) → (is-a?/c color%)
> turtles : turtles?

Returns the current color of the pen that the turtles use to draw.

Added in version 1.6 of package htdp-lib.

> > procedure
> > (turtle-state turtles) |  | → |  |
> > (listof (vector/c real? real? real?
> > #:immutable? #t
> > #:flat? #t))
> turtles : turtles?

Returns the position and heading of all of the turtles; the first element in each vector is the x coordinate, the second is the y coordinate and the third is the angle in degrees.

Added in version 1.5 of package htdp-lib.

> procedure (restore-turtle-state turtles state) → turtles?
> turtles : turtles?
> | state |  | : |  |
> (listof (vector/c real? real? real?
> #:immutable? #t
> #:flat? #t))

Keeps the drawing as in turtles, but puts the turtles positions and headings as specified in state.

Added in version 1.5 of package htdp-lib.

> procedure (turtles-pict turtles) → pict?
> turtles : turtles?

Constructs a pict that draws the same way that turtles would draw, except that it does not draw the frame around the turtles, nor does it draw the turtles themselves. Additionally, the size of the resulting is not the size of turtles, but instead sized exactly to the the lines that that are in the drawing in turtles.

Added in version 1.5 of package htdp-lib.

### 2.1 Examples🔗ℹ

(require graphics/value-turtles-examples)
| package: htdp-lib

The graphics/turtle-examples library’s source is meant to be read, but it also exports the following examples.

> procedure (radial-turtles n turtles) → turtles?
> n : exact-nonnegative-integer?
> turtles : turtles?

Places 2n turtles spaced evenly pointing radially outward.

> procedure (spaced-turtles n turtles) → turtles?
> n : exact-nonnegative-integer?
> turtles : turtles?

Places 2n turtles evenly spaced in a line and pointing in the same direction as the original turtle.

> procedure (neato turtles) → turtles?
> turtles : turtles?

As the name says...

> procedure (regular-poly sides radius turtles) → turtles?
> sides : exact-nonnegative-integer?
> radius : real?
> turtles : turtles?

Draws a regular poly centered at the turtle with sides sides and with radius radius.

> procedure (regular-polys n s turtles) → turtles?
> n : exact-nonnegative-integer?
> s : any/c
> turtles : turtles?

Draws n regular polys each with n sides centered at the turtle.

> procedure (spokes turtles) → turtles?
> turtles : turtles?

Draws some spokes, using radial-turtles and spaced-turtles.

> procedure (spyro-gyra turtles) → turtles?
> turtles : turtles?

Draws a spyrograph-reminiscent shape.
top contents ← prev up next →