Koch.java
Below is the syntax highlighted version of Koch.java
from §2.7 Recursion.
/*************************************************************************
* Compilation: javac Koch.java
* Execution: java Koch N
* Dependencies: StdDraw.java
*
* Plot an order N Koch snowflake.
*
* % java Koch 5
*
*************************************************************************/
public class Koch {
// Koch curve of order n
public static void koch(int n, double size) {
if (n == 0) StdDraw.goForward(size);
else {
koch(n-1, size);
StdDraw.rotate(60);
koch(n-1, size);
StdDraw.rotate(-120);
koch(n-1, size);
StdDraw.rotate(60);
koch(n-1, size);
}
}
public static void main(String args[]) {
int N = Integer.parseInt(args[0]);
int width = 512;
int height = (int) (2 * width / Math.sqrt(3));
double size = width / Math.pow(3.0, N);
StdDraw.create(width, height);
// three Koch curves in the shape of an equilateral triangle
StdDraw.go(0, width * Math.sqrt(3) / 2);
StdDraw.penDown();
koch(N, size);
StdDraw.rotate(-120);
koch(N, size);
StdDraw.rotate(-120);
koch(N, size);
StdDraw.show();
}
}
Last updated: Sun Sep 26 14:06:13 EDT 2004
.
Copyright © 2004, Robert Sedgewick and Kevin Wayne.