API Docs for: 0.99.4
Show:

Turtle Class

PSEUDO

Extends JXG.Turtle
Module: JXG

This element is used to provide a constructor for a turtle.

Constructor

Turtle

(
  • parents
  • attributes
)
JXG.Turtle

Parameters:

  • parents Array

    Start position and start direction of the turtle. Possible values are

    • [x, y, angle]
    • [[x, y], angle]
    • [x, y]
    • [[x, y]]
  • attributes Object

    Object containing properties for the element such as stroke-color and visibility. See {@link JXG.GeometryElement#setAttribute}

Returns:

JXG.Turtle:

Reference to the created turtle object.

Example:

var board = JXG.JSXGraph.initBoard('box', {boundingbox: [-300, 300, 300, -300]});
        var t = board.create('turtle');
        
        var side = function(size, level) {
            if (level == 0) {
                t.fd(size);
                return;
            }
            side(size/3, level-1);
            t.lt(60);
            side(size/3, level-1);
            t.rt(120);
            side(size/3, level-1);
            t.lt(60);
            side(size/3, level-1);
        };
        
        var snowflake = function (size, level) {
            var i;
            for (i = 0; i < 3; i++) {
                side(size, level);
                t.rt(120);
            };
        }
        
        t.clearScreen().hideTurtle();
        t.setPenSize(1).setPenColor("#000000");
        t.lt(30).setPos(0, -100);
        
        snowflake(250, 3);
        
var board = JXG.JSXGraph.initBoard('box', {boundingbox: [-300, 300, 300, -300]});
        var t = board.create('turtle');
        
        var branch = function(length, level) {
            if  (level == 0)
                return;
        
            t.fd(length).lt(45);
            branch(length / 2, level - 1);
            t.rt(90);
            branch(length / 2, level - 1);
            t.lt(45).bk(length);
        };
        
        var lbranch = function(length, angle, level) {
            t.fd(2 * length);
            node(length, angle, level);
            t.bk(2 * length);
        };
        
        var rbranch = function (length, angle, level) {
            t.fd(length);
            node(length, angle, level);
            t.bk(length);
        };
        
        var node = function (length, angle, level) {
            if (level == 0)
                return;
        
            t.lt(angle);
            lbranch(length, angle, level - 1);
            t.rt(2 * angle);
            rbranch(length, angle, level - 1);
            t.lt(angle);
        };
        
        t.clearScreen().hideTurtle();
        t.setPenSize(5).setPenColor("#008800");
        t.setPos(30, -150);
        lbranch(25, 20, 7);
        
var board = JXG.JSXGraph.initBoard('jxgbox',{boundingbox: [-250, 250, 250, -250]});
        var t = brd.create('turtle', [0, 0], {strokeOpacity:0.5});
        t.setPenSize(3);
        t.right(90);
        var alpha = 0;
        
        var run = function() {
           t.forward(2);
           if (Math.floor(alpha / 360) % 2 === 0) {
              t.left(1);        // turn left by 1 degree
           } else {
              t.right(1);       // turn right by 1 degree
           }
        
           alpha += 1;
        
           if (alpha < 1440) {  // stop after two rounds
               setTimeout(run, 20);
           }
        }
        
        run();
        

Methods

back

(
  • len
)
JXG.Turtle chainable

Move the turtle backwards.

Parameters:

  • len Number

    of backwards move in user coordinates

Returns:

JXG.Turtle:

pointer to the turtle object

bk

()

Alias for undefined

clean

() JXG.Turtle chainable

Removes the turtle curve from the board. The turtle stays in its position.

Returns:

JXG.Turtle:

pointer to the turtle object

clearScreen

() JXG.Turtle chainable

Removes the turtle completely and resets it to its initial position and direction.

Returns:

JXG.Turtle:

pointer to the turtle object

copyAttr

(
  • key
  • val
)
Object private

Set a future attribute of the turtle.

Parameters:

Returns:

Object:

pointer to the attributes object

cs

()

Alias for undefined

evalAt

(
  • t
  • co.
)
Number

The co-coordinate of the turtle curve at position t is returned.

Parameters:

Returns:

Number:

x/y-coordinate of turtle at position t or x/y-coordinate of the turtle position if t > position.

fd

()

Alias for undefined

forward

(
  • length
)
JXG.Turtle chainable

Move the turtle forward.

Parameters:

  • length Number

    of forward move in user coordinates

Returns:

JXG.Turtle:

pointer to the turtle object

hasPoint

(
  • x
  • y
)
Boolean

Checks whether (x,y) is near the curve.

Parameters:

  • x Number

    Coordinate in x direction, screen coordinates.

  • y Number

    Coordinate in y direction, screen coordinates.

Returns:

Boolean:

True if (x,y) is near the curve, False otherwise.

hideTurtle

() JXG.Turtle chainable

Sets the visibility of the turtle head to false

Returns:

JXG.Turtle:

pointer to the turtle object

home

() JXG.Turtle chainable

Moves the turtle to position [0,0].

Returns:

JXG.Turtle:

pointer to the turtle object

ht

()

Alias for undefined

init

(
  • x
  • y
  • dir
)
JXG.Turtle private chainable

Initialize a new turtle or reinitialize a turtle after JXG.Turtle#clearScreen.

Parameters:

  • x Number

    initial x coordinate in user view

  • y Number

    initial y coordinate in user view

  • dir Number

    initial direction in degrees

Returns:

JXG.Turtle:

pointer to the turtle object

left

(
  • angle
)
JXG.Turtle chainable

Rotate the turtle direction to the right.

Parameters:

  • angle Number

    of the rotation in degrees

Returns:

JXG.Turtle:

pointer to the turtle object

lookTo

(
  • target
)
JXG.Turtle chainable

Rotates the turtle into a new direction. There are two possibilities:

Parameters:

  • target Number | Array

    If a number is given, it is interpreted as the new direction to look to; If an array consisting of two Numbers is given targeted is used as a pair coordinates.

Returns:

JXG.Turtle:

pointer to the turtle object

lt

()

Alias for undefined

maxX

() Number

Gives the upper bound of the parameter if the the turtle is treated as parametric curve. May be overwritten in @see generateTerm.

Returns:

minX

() Number

Gives the lower bound of the parameter if the the turtle is treated as parametric curve.

Returns:

Number:

0

moveTo

(
  • target
)
JXG.Turtle chainable

Moves the turtle to a given coordinate pair. The direction is not changed.

Parameters:

  • target Array

    Coordinates of the point where the turtle looks to.

Returns:

JXG.Turtle:

pointer to the turtle object

pd

()

Alias for undefined

penDown

() JXG.Turtle chainable

Pen down, continues visible drawing

Returns:

JXG.Turtle:

pointer to the turtle object

penup

() JXG.Turtle chainable

Pen up, stops visible drawing

Returns:

JXG.Turtle:

pointer to the turtle object

pop

()

Alias for undefined

popTurtle

() JXG.Turtle chainable

Gets the last position of the turtle on the stack, sets the turtle to this position and removes this position from the stack.

Returns:

JXG.Turtle:

pointer to the turtle object

pu

()

Alias for undefined

push

()

Alias for undefined

pushTurtle

() JXG.Turtle chainable

Pushes the position of the turtle on the stack.

Returns:

JXG.Turtle:

pointer to the turtle object

right

(
  • angle
)
JXG.Turtle chainable

Rotate the turtle direction to the right

Parameters:

  • angle Number

    of the rotation in degrees

Returns:

JXG.Turtle:

pointer to the turtle object

rt

()

Alias for undefined

setAttribute

(
  • attributes
)
JXG.Turtle chainable

Sets properties of the turtle, see also JXG.GeometryElement#setAttribute. Sets the property for all curves of the turtle in the past and in the future.

Parameters:

  • attributes Object

    key:value pairs

Returns:

JXG.Turtle:

pointer to the turtle object

setHighlightPenColor

(
  • color
)
JXG.Turtle chainable

Sets the highlight pen color. Equivalent to setAttribute({highlightStrokeColor:color}) but affects only the future turtle.

Parameters:

Returns:

JXG.Turtle:

pointer to the turtle object

setPenColor

(
  • color
)
JXG.Turtle chainable

Sets the pen color. Equivalent to setAttribute({strokeColor:color}) but affects only the future turtle.

Parameters:

Returns:

JXG.Turtle:

pointer to the turtle object

setPenSize

(
  • size
)
JXG.Turtle chainable

Sets the pen size. Equivalent to setAttribute({strokeWidth:size}) but affects only the future turtle.

Parameters:

Returns:

JXG.Turtle:

pointer to the turtle object

setPos

(
  • x
  • y
)
JXG.Turtle chainable

Moves the turtle without drawing to a new position

Parameters:

Returns:

JXG.Turtle:

pointer to the turtle object

showTurtle

() JXG.Turtle chainable

Sets the visibility of the turtle head to true.

Returns:

JXG.Turtle:

pointer to the turtle object

st

()

Alias for undefined

X

(
  • t
)
Number

If t is not supplied the x-coordinate of the turtle is returned. Otherwise the x-coordinate of the turtle curve at position t is returned.

Parameters:

Returns:

Number:

x-coordinate of the turtle position or x-coordinate of turtle at position t

Y

(
  • t
)
Number

If t is not supplied the y-coordinate of the turtle is returned. Otherwise the y-coordinate of the turtle curve at position t is returned.

Parameters:

Returns:

Number:

y-coordinate of the turtle position or y-coordinate of turtle at position t

Z

() Number

Returns:

Number:

z-coordinate of the turtle position

Properties

arrow

JXG.Line private

Arrow as subsitute for a turtle image

curve

JXG.Curve

The actual turtle curve object

dir

Number private

Turtle direction

objects

Array

Array containing all JSXgraph elements belonging to the turtle

stack

Array private

Turtle stack

turtle

JXG.Point private

Invisible point at the top of the turtle

turtle2

JXG.Point private

Invisible point at the top of the arrow