// Sierpinski Gasket illustration // // The Sierpinski Gasket takes the form of a // square that is divided into 9 regions (3 // rows, 3 columns). The middle region is // blacked out. We repeat this divide-and- // blackout procedure on each of the // surrounding eight regions. If we continue // this an infinite number of times, we end // up with a "lacework" design that is more // space than solid. // Global Variables int recursionDepth = 5; // # of times to repeat int canvasWidth = 500; int canvasHeight = 500; int normalFillColor = 150; // vs. black for center regions void setup () { size(canvasWidth, canvasHeight); background(0); fill(normalFillColor); sierpinski(recursionDepth, 0, 0, canvasWidth/3, canvasHeight/3); println("Finished calling sierpinski"); } // sierpinski() -- display a Sierpinski Gasket // for a rectangle with the supplied dimensions void sierpinski (int depth, int topLeftX, int topLeftY, int w, int h) { println("Calling sierpinski with depth=" + depth); // Base case: If there are no more levels of // depth remaining, stop if (depth < 1) { return; } else { // Divide the rectangle into 9 regions: // // ------------- // | 1 | 2 | 3 | // |---|---|---| // | 4 | 5 | 6 | (5 will be blacked out) // |---|---|---| // | 7 | 8 | 9 | // ------------- // Calculate the width and height of each // of the 9 regions // int regionWidth = (w - topLeftX)/3; // int regionHeight = (h - topLeftY)/3; //println("regionWidth: " + regionWidth + " regionHeight: " + regionHeight); // if (regionWidth > 0 && regionHeight > 0) // { // For each region, draw a filled-in rectangle // and then sierpinski-ize it // Draw region 1 println("Drawing region 1, depth: " + depth); fill(150); rect(topLeftX, topLeftY, w, w); sierpinski(depth-1, topLeftX, topLeftY, w/3, w/3); // Draw region 2 println("Drawing region 2, depth: " + depth); fill(150); rect(topLeftX+w, topLeftY, w, h); sierpinski(depth-1, topLeftX+w, topLeftY, w/3, h/3); // Draw region 3 println("Drawing region 3, depth: " + depth); fill(150); rect(topLeftX+(2*w), topLeftY, w, h); sierpinski(depth-1, topLeftX+(2*w), topLeftY, w/3, h/3); // Draw region 4 println("Drawing region 4, depth: " + depth); fill(150); rect(topLeftX, topLeftY+h, w, h); sierpinski(depth-1, topLeftX, topLeftY+h, w/3, h/3); // Draw region 5 println("Drawing region 5, depth: " + depth); fill(0); rect(topLeftX+w, topLeftY+h, w, h); // Draw region 6 println("Drawing region 6, depth: " + depth); fill(150); rect(topLeftX+(2*w), topLeftY+h, w, h); sierpinski(depth-1, topLeftX+(2*w), topLeftY+h, w/3, h/3); // Draw region 7 println("Drawing region 7, depth: " + depth); fill(150); rect(topLeftX, topLeftY+(2*h), w, h); sierpinski(depth-1, topLeftX, topLeftY+(2*h), w/3, h/3); // Draw region 8 println("Drawing region 8, depth: " + depth); fill(150); rect(topLeftX+w, topLeftY+(2*h), w, h); sierpinski(depth-1, topLeftX+w, topLeftY+(2*h), w/3, h/3); // Draw region 9 println("Drawing region 9, depth: " + depth); fill(150); rect(topLeftX+(2*w), topLeftY+(2*h), w, h); sierpinski(depth-1, topLeftX+(2*w), topLeftY+(2*h), w/3, h/3); } }