Advertisement
calcpage

LACS07_Koch.java

Jun 7th, 2012
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 0.88 KB | None | 0 0
  1. /**
  2. Koch.java   MrG 2012.0607
  3. purpose:    graph a Koch curve
  4. required files: Koch.java           main class
  5.         Turtle.java         derived class
  6.         StdDraw.java            graphics class
  7. translator: java Koch.java
  8. interpreter:    java Koch N
  9. */
  10. public class Koch
  11. {
  12.     public static void koch(int n, double step, Turtle turtle)
  13.     {
  14.         if(n==0)
  15.         {
  16.             turtle.goForward(step);
  17.             return;
  18.         }
  19.         koch(n-1,step,turtle);
  20.         turtle.turnLeft(60);
  21.         koch(n-1,step,turtle);
  22.         turtle.turnLeft(-120);
  23.         koch(n-1,step,turtle);
  24.         turtle.turnLeft(60);
  25.         koch(n-1,step,turtle);
  26.     }
  27.  
  28.     public static void main(String[] args)
  29.     {
  30.         StdDraw.setXscale(0,1.5);
  31.         StdDraw.setYscale(0,1.5);
  32.         int N = Integer.parseInt(args[0]);
  33.         Turtle blaise = new Turtle(0,1,0);
  34.         koch(N,1/Math.pow(3,N),blaise);
  35.         blaise.turnLeft(-120);
  36.         koch(N,1/Math.pow(3,N),blaise);
  37.         blaise.turnLeft(-120);
  38.         koch(N,1/Math.pow(3,N),blaise);
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement