/**
 *  The SimpleTree class shows an example if using recursion where a method will call
 *  itself as part of accomplishing its task.
 */

import java.awt.*;
import javax.swing.*;
import com.otherwise.jurtle.Turtle;

public class SimpleTree extends Turtle
{

    /**
     *  Main entry point.
     */
    public void runTurtle()
    {
        setAutoUpdate( false );
        hideTurtle();

        Dimension size = getDisplaySize();

        penUp();
        setPosition( size.width / 2, size.height - 20 );
        penDown();

        // The type of tree that gets drawn depends upon the second and
        // third arguments.  Try values of the second argument between 0.5 and 0.85.
        // Try values of the third argument between 20 and 70.
        tree( 80, 0.60, 50 );
    }


    /**
     *  Recursively draws a tree the trunk length and branch length is 
     *  specified by "length".  The parameter angle specifies the angle between
     *  the branches.  The decreaseFactor parameter specifies the fraction the 
     *  trunk and branch length is decreased at each recursion.
     */
    private void tree( double length, double decreaseFactor, int angle )
    {
        if ( length >= 2 )
        {
            forward( length );
            left( angle );
            forward( length );
            tree( length * decreaseFactor, decreaseFactor, angle );
            backward( length );
            right( 2 * angle );
            forward( length );
            tree( length * decreaseFactor, decreaseFactor, angle );
            backward( length );
            left( angle );
            backward( length );
            if ( length > 30 )
                updateDisplay();
        }
    }


}
