float[] x, y; // Coordinates of the points on the curve int depth; // How deeply we've recurse void setup() { size(400,400); smooth(); background(255); initKoch(); drawSnowFlake(); } void drawSnowFlake() { pushMatrix(); translate(width/4,height/4); drawCurve(x,y); popMatrix(); pushMatrix(); translate(width*3/4,height/4); rotate(radians(120)); drawCurve(x,y); popMatrix(); pushMatrix(); translate(width/2, height/4+width/2*sin(radians(60))); rotate(radians(240)); drawCurve(x,y); popMatrix(); } void draw() {} // Each time the mouse is pressed, recurse one more level void mousePressed() { background(255); depth++; if (depth==6) initKoch(); // wrap around else expandKoch(); println(depth); drawSnowFlake(); } // Set up the initial curve -- a horizontal line void initKoch() { depth = 0; x = new float[2]; y = new float[2]; x[0] = 0; y[0] = 0; x[1] = width/2; y[1] = 0; } // Update the points by putting a triangular part jutting out // between each pair of points in the current set void expandKoch() { // new points int l2 = 1 + 4*(x.length-1); float[] x2 = new float[l2], y2 = new float[l2]; // index into new points int i2=0; for (int i=0; i