PTurtle class

The PTurtle class (for "Processing Turtle") describes a virtual Turtle object that draws lines in a Processing window. The full code for that class is given below.

Create a Processing project TurtleTester with the following program in it. NOTE: This program must be the program in the first tab of the Processing project. Classes used by the main must be placed to the right of the main() program.

PTurtle t;  // creates a PTurtle that we can draw with

/**
 * The setup() function runs once when the program is first run.
 */
void setup()
{
  size(700,700);
  background(255);                      // white
  t = new PTurtle(width/2, height/2);   // middle of window
}

/**
 * The draw() method is run repeatedly, up to 30x a second, or
 * (in this case), until the noLoop() instructions is encountered.
 */
void draw()
{
  for (int i = 0; i < 2; i++)
  { 
    t.forward(200);
    t.left(90);
  }
  noLoop();
}

Once the the TurtleTester program has been entered into the project, open up a new tab in the Processing project where the code for the PTurtle class can be entered (see below).

Once the main program and the PTurtle class have been placed in the Processing "sketch," run the program to see what is produced. Make alterations to the main program to experiment with drawing different kinds of objects using the PTurtle class.

/**
 * Defines a Turtle class that can be used in the 
 * Processing environment. Based on code from Jamie Matthews, 
 * http://j4mie.org/blog/simple-turtle-for-processing/
 */

class PTurtle 
{
    // instance variables
    float x, y;         // Current position of the turtle
    float angle;        // Current heading of the turtle
                        // 0 = positive x-axis
    boolean penDown;    // The state of the pen

    /**
     * Constructs a Turtle object
     * @param x the initial x-coordinate of the Turtle
     * @param y the initial y-coordinate of the Turtle
     */
    PTurtle (float x, float y) 
    {
        this.x = x;
        this.y = y;
        angle = 0;    // ie, pointing along the positive x-axis
        penDown = true;
    }

    /**
     * Moves the turtle forward by the specified distance
     * @param distance the distance to move forward
     */
    void forward (float distance) 
    {
        float newX = x + cos(radians(angle)) * distance;
        float newY = y + sin(radians(angle)) * distance;
        // If the pen is down, draw a line to the new position
        if (penDown) line(x, y, newX, newY);
        // Update position
        x = newX;
        y = newY;
    }

    /**
     * Moves the turtle back by the specified distance
     * @param distance the distance to move backward
     */
    void back (float distance) 
    {
        forward(-distance);
    }

    /**
     * Turns the turtle to the left by the given angle.
     * @param angle the number of degrees to rotate left
     */
    void left (float angle) 
    {
        this.angle -= angle;
    }

    /**
     * Turns the turtle to the right by the given angle.
     * @param angle the number of degrees to rotate right
     */
    void right (float angle) 
    {
        this.angle += angle;
    }

    /**
     * Lifts the pen up so that the tail won't draw
     */
    void penUp() 
    {
        penDown = false;
    }

    /**
     * Sets the pen up so that the tail will draw
     */
    void penDown() 
    {
      penDown = true;
    }
}