Turtle Graphics with Java

Bern University of Teacher Education  
HomeStart Online-EditorPrintAndroid-TurtlegraphicsJava-Online

Recursion

Recursion is a problem solving method in which a problem reduced to the same, but a bit simpler problem. In Java a method is recursive if its declaration class the same method. To be sure that the recursive program does not end in an infinite loop, it is necessary to set breaking conditions.

Run this example (SquarePattern1.java)    
// SquarePattern1.java
import ch.aplu.turtle.*;

class SquarePattern1 extends Turtle
{
  SquarePattern1()
  {
    hideTurtle();
    figure(0064);
  }

  void figure(double x, double y, double s)
  {
    if (< 1)
    {
      return;
    }
    else
    {
      setPos(- s/2, y - s/2);
      square(s);
      figure(+ s, y + s, s/2);
    }
  }

  void square(double s)
  {
    for(int i = 0; i < 4; i++)
    {
      forward(s);
      right(90);
    }
  }

  public static void main(String[] args)
  {
    new SquarePattern1();
  }
}
 

Test this example in the Online-Editor


With some small addition other patterns can be drawn...

Run this example (SquarePattern2.java)

// SquarePattern2.java
import ch.aplu.turtle.*;

class SquarePattern2 extends Turtle
{
  SquarePattern2()
  {
    hideTurtle();
    speed(-1);
    figure(0064);
  }

  void figure(double x, double y, int s)
  {
    if (< 1)
    {
      return;
    }
    setPos(- s/2, y - s/2);
    square(s);
    figure(+ s, y + s, s/2);
    figure(- s, y + s, s/2);
  }

  void square(int s)
  {
    for(int i = 0; i < 4; i++)
    {
      forward(s);
      right(90);
    }
  }

  public static void main(String[] args)
  {
    new SquarePattern2();
  }
}
 

Test this example in the Online-Editor

Run this example (SquarePattern3.java)  
// SquarePattern3.java
import ch.aplu.turtle.*;

class SquarePattern3 extends Turtle
{
  SquarePattern3()
  {
    hideTurtle();
    speed(-1);
    figure(0064);
  }

  void figure(double x, double y, int s)
  {
    if (< 1)
    {
      return;
    }
    setPos(- s/2, y - s/2);
    square(s);
    fill(x, y);
    figure(+ s, y + s, s/2);
    figure(- s, y + s, s/2);
    figure(- s, y - s, s/2);
    figure(+ s, y - s, s/2);
  }

  void square(int s)
  {
    for(int i = 0; i < 4; i++)
    {
      forward(s);
      right(90);
    }

  }

  public static void main(String[] args)
  {
    new SquarePattern3();
  }
}
 

Test this example in the Online-Editor


Example Koch graph:

Run this example (Koch.java)  
// Koch.java
import ch.aplu.turtle.*;

class Koch extends Turtle
{
  Koch()
  {
    double length = 200;
    int generations = 4;
    hideTurtle();
    setPos( -1800);
    right(90);
    koch(length, generations);
  }

  void koch(double l, int n)
  {
    if (== 0)
    {
      forward(l);
      return;
    }
    koch(/ 3, n - 1);
    left(45);
    koch(/ 3, n - 1);
    right(90);
    koch(/ 3, n - 1);
    left(45);
    koch(/ 3, n - 1);
  }

  public static void main(String[] args)
  {
    new Koch();
  }
}
 

Test this example in the Online-Editor


More examples:

Run this example (Tree.java) Test this example in the Online-Editor

Run this example (Sierpi.java) Test this example in the Online-Editor

Run this example (Peano.java) Test this example in the Online-Editor

Run this example (Menger.java) Test this example in the Online-Editor

Run this example (Dragon.java) Test this example in the Online-Editor


Exercises