Levy.java


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


/*************************************************************************
 *  Compilation:  javac Levy.java
 *  Execution:    java Levy N
 *  Dependencies: StdDraw.java
 *  
 *  Plot an order N Levy tapestry.
 *
 *  % java Levy 5
 *
 *************************************************************************/


public class Levy {
    private Turtle turtle;
    private double size;

    public Levy(int N) {
        int SIZE = 512;
        turtle = new Turtle(178, 178, 0);
        turtle.setXscale(0, SIZE);
        turtle.setYscale(0, SIZE);
        size =  156.0 / Math.pow(Math.sqrt(2), N);
        levy(N);
        turtle.turnLeft(90);
        levy(N);
        turtle.turnLeft(90);
        levy(N);
        turtle.turnLeft(90);
        levy(N);
        turtle.turnLeft(90);
    }

   public void levy(int n) {
       if (n == 0) turtle.goForward(size);
       else {
          turtle.turnLeft(-45);
          levy(n-1);
          turtle.turnLeft(90);
          levy(n-1);
          turtle.turnLeft(-45);
       }
   }

    public static void main(String[] args) {
        int N = Integer.parseInt(args[0]);
        new Levy(N);
    }

}


Copyright © 2000–2010, Robert Sedgewick and Kevin Wayne.
Last updated: Wed Feb 9 09:07:43 EST 2011.