import static util.SVGUtil.*;

public class Koch {
	private final static double SQRT3_6 = 0.28867513459481288225457439025098;

	private static void koch(int n, double x0, double y0, double x4, double y4) {
	    if (n==0) {
	        line(x0, y0, x4, y4);
	        return;
	    }  else {
	        double x1 = x0*2/3+x4/3, y1 = y0*2/3+y4/3;
	        double x2 = (x0+x4)/2 + (y4-y0) * SQRT3_6 , y2 = (y0+y4)/2 - (x4-x0) * SQRT3_6;
	        double x3 = x0/3+x4*2/3, y3 = y0/3+y4*2/3;
	        koch(n-1, x0, y0, x1, y1);
	        koch(n-1, x1, y1, x2, y2);
	        koch(n-1, x2, y2, x3, y3);
	        koch(n-1, x3, y3, x4, y4);
	    }
	}

	public static void main(String[] args) {
	    start();
	    stroke(0x008000);
	    strokeWeight(0.5);
	    koch(6, 40, 180, 254, 180);
	    finish();
	}
}
