# Turtlegraphics for Smartphones and Tablets

Bern University of Teacher Education
HomeStart Online EditorPrintAndroid GamesJava-Online

## Recursions

Recursion is a solution method where a problem is lead back to the same, but facilitated problem. In Java a method is recursive if the same method is evoked in its declaration part. So that a recursive program doe not evoke infinitely, a cancellation condition ("anchoring") is needed. Turtlegraphics are optimal for the programming of recursions. Under the menu "Known recursions" further nice recursions can be found.
The first recursion draws a square pattern. If you activate the two outcommented lines, even more beaufiul patterns are obtained. Install App on smartphone or tablet

 // Tu12.java package app.tu12; import turtle.*; public class Tu12 extends Playground {   public void main()   {     ht();     square(0, 0, 64);   }   void square(double x, double y, double s)   {     if (s < 1)       return;           setPos(x - s/2, y - s/2);     for(int i = 0; i < 4; i++)     {       fd(s);       rt(90);     }     square(x + s, y + s, s/2);     square(x - s, y + s, s/2);     // square(x + s, y - s, s/2);     // square(x - s, y - s, s/2);   }   } Explanatation of the program code:

 square(0, 0, 64); The first square is drawn in the position (0, 0) with the side length of 64 square(x + s, y + s, s/2); Every further square is drawn in a new position with half the side lenght return The method turn back (anchoring)

The binary tree is probably one of the most known recursions. The basis figur has the form of a Y. Install App on smartphone or tablet

 // Tree.java package app.tree; import turtle.*; public class Tree extends Playground {     public public void main()   {     setY(-100);     tree(128);   }   void tree(int s)   {     if (s < 8)       return;     fd(s);     lt(45);     tree(s / 2);     rt(90);     tree(s / 2);     lt(45);     bk(s);   } } The flake is a combination of iteration and recursion. Install App on smartphone or tablet

 // Flock.java package app.flock; import turtle.*; public class Flock extends Playground {   public void main()   {     flock(100);   }   void flock(int s)   {     if (s < 5)       return;     for (int i = 0; i < 6; i++)     {       fd(s);       flock(s / 3);       bk(s);       rt(60);     }   } } 