Advertisement
calcpage

LACS07a_Dragon.java

Jun 12th, 2012
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.22 KB | None | 0 0
  1. /*************************************************************************
  2.  *  Compilation:  javac Dragon.java
  3.  *  Execution:    java Dragon N
  4.  *
  5.  *  Draws dragon curve of order N.
  6.  *
  7.  *  % java Dragon 7
  8.  *
  9.  *  Limitations
  10.  *  -------------
  11.  *    -  N must be between 1 and 15
  12.  *
  13.  *************************************************************************/
  14.  
  15.  
  16. public class Dragon {
  17.     private Turtle turtle;
  18.  
  19.     public Dragon(int N) {
  20.      /**************************************************************************
  21.       *  The following constants are used to figure out where to start        
  22.       *  drawing the dragon curve.  left[i] = maximum number of steps taken
  23.       *  to the left in dragon(i).  right[i], up[i], down[i] are similar.
  24.       **************************************************************************/
  25.       int[] left  = { 0, 0, 0, 2, 4, 5, 5,  5,  5,  5, 10, 42, 74, 81,  85,  85 };
  26.       int[] right = { 1, 1, 1, 1, 1, 1, 2, 10, 18, 21, 21, 21, 21, 21,  57, 170 };
  27.       int[] up    = { 0, 1, 2, 2, 2, 2, 2,  2,  5, 21, 37, 42, 42, 42,  42,  42 };
  28.       int[] down  = { 0, 0, 0, 0, 1, 5, 9, 10, 10, 10, 10, 10, 23, 85, 149, 165 };
  29.  
  30.  
  31.       double size = Math.max(left[N] + right[N], up[N] + down[N]);
  32.       double x = (right[N] - left[N]) / 2.0;
  33.       double y = (up[N]    - down[N]) / 2.0;
  34.  
  35.       turtle = new Turtle(0.0, 0.0, 0.0);
  36.       turtle.setXscale(x - size/2, x + size/2);
  37.       turtle.setYscale(y - size/2, y + size/2);
  38.       dragon(N);
  39.     }
  40.  
  41.  
  42.    // dragon curve of order n
  43.    public void dragon(int n) {
  44.       if (n == 0) {
  45.          turtle.goForward(1.0);
  46.       }
  47.  
  48.       else {
  49.          dragon(n-1);
  50.          turtle.turnLeft(90);
  51.          nogard(n-1);
  52.       }
  53.    }
  54.    
  55.    // reverse dragon curve of order n
  56.    public void nogard(int n) {
  57.       if (n == 0) {
  58.          turtle.goForward(1.0);
  59.       }
  60.       else {
  61.          dragon(n-1);
  62.          turtle.turnLeft(-90);
  63.          nogard(n-1);
  64.       }
  65.    }
  66.  
  67.  
  68.  
  69.    public static void main(String[] args) {
  70.       int N = Integer.parseInt(args[0]);
  71.       if (N < 0 || N > 15) {
  72.          System.out.println("Try a number between 1 and 15 next time.");
  73.          System.exit(0);
  74.       }
  75.  
  76.       new Dragon(N);
  77.  
  78.  
  79.    }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement