/* Reference: https://makezine.com/2011/05/11/codebox-explore-recursion-with-processing/ */ int N = 2; // Number of recursive steps // Draw a Mondarian-inspired image using recursion void piet(int x0, int y0, int x1, int y1, int N) { if (N == 0) { // Base case -- draw a colorful rectangle with a thick black border int sw = 10; //this is the stroke width for the rectangle's border color c[] = { #ff0000, #0000ff, #ffff00, #ffffff}; //Mondarian color palatte fill(c[int(random(c.length))]); stroke(0); strokeWeight(sw); rect (x0, y0, x1-x0, y1-y0); } else { //Recursive step -- recursively break the current rectangle into 4 new random rectangles int i = int(random(x0, x1)); //Pick a random x coordinate inside the current rectangle int j = int(random(y0, y1)); //Pick a random y coordinate inside the current rectangle piet(x0, y0, i, j, N-1); // Recurse on upper left rectangle piet(i, y0, x1, j, N-1); // Recurse on upper right rectangle piet(x0, j, i, y1, N-1); // Recurse on lower left rectangle piet(i, j, x1, y1, N-1); // Recurse on lower right rectangle } save("mondorian.png"); } // keep draw() here to continue looping while waiting for keys void draw() { } //Draw a new image each time a key is pressed void keyPressed() { background(0); piet(1, 1, 500, 500, N); } //Draw the first image void setup() { size(500, 500); piet(1, 1, 500, 500, N); }