Koch.java


Below is the syntax highlighted version of Koch.java from §3.2 Creating Data Types.


/******************************************************************************
 *  Compilation:  javac Koch.java
 *  Execution:    java Koch n
 *  Dependencies: Turtle.java 
 *  
 *  Plot a Koch curve of order n.
 *
 *  % java Koch 5
 *
 ******************************************************************************/

public class Koch {

    // plot Koch curve of order n, with given step size
    public static void koch(int n, double step, Turtle turtle) {
        if (n == 0) {
            turtle.goForward(step);
            return;
        }
        koch(n-1, step, turtle);
        turtle.turnLeft(60.0);
        koch(n-1, step, turtle);
        turtle.turnLeft(-120.0);
        koch(n-1, step, turtle);
        turtle.turnLeft(60.0);
        koch(n-1, step, turtle);
    } 
 
    public static void main(String[] args) {
        int n = Integer.parseInt(args[0]);
        double step = 1.0 / Math.pow(3.0, n); 
        Turtle turtle = new Turtle(0.0, 0.0, 0.0); 
        koch(n, step, turtle); 
    } 
} 


Copyright © 2000–2017, Robert Sedgewick and Kevin Wayne.
Last updated: Fri Oct 20 14:12:12 EDT 2017.