Recursive Line

Create a Processing project RecursiveLine that draws a geometric pattern using recursion.

The RecursiveLine program:

  1. Establishes a PTurtle object to use in this program
  2. Uses a Processing setup() function to set up a screen for drawing on
  3. Defines a recursive function drawAndTurn that has a line length (a float value) and a turn angle (a float value). Pseudocode for this function:

     void drawAndTurn(float lineLength, float angle)
     {
         // base case
         if line length <= 0:
             return from this recursive function
         else:
             move the turtle forward the given line length
             turn the turtle to the left the given angle
             call drawAndTurn recursively w/ shorter line, same angle
     }
  4. Uses a Processing draw() function to call drawAndTurn with some initial values. (We don't need draw() to run repeatedly--the recursive function will take care of creating all the lines.)

     // The main draw() loop. Ordinarily, this would be run 
     // over and over, but here, with a noLoop at the end, it
     // only runs once.
    
     void draw()
     {
       drawAndTurn(400, 37);    // call the recursive function
       noLoop();                // don't re-draw
     }

For reference, the PTurtle class description is given here.

/**
 * 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;
    }
}