Rec­u­r­s­i­v­e Col­o­u­r­e­d Squ­a­r­e­s

by Matthew Chensee
  • Show Sketch
/** @peep sketchcode **/
 
size(200, 200);
squareRecursion(0, 0, width, height);
 
void squareRecursion(float x, float y, float w, float h) {
  // Draw a rectangle
  noStroke();
  rect(x, y, w, h);
  float r = random(0, 255);
  float g = random(0, 255);
  float b = random(0, 255);
  float o = random(0, 255);
  fill(r, g, b, o);
 
 
  // Test to see if we should recurse...
  if ((random(100) < 80) && (w > 10 || h > 10)) {
    // Set a border to go around sub-rectangles
    float bx = 3;
    float by = 3;
    // Calculatue the width and height of sub-rectangles
    w = (w - 3*bx) / 2;
    h = (h - 3*by) / 2;
    // Call the same function to draw the sub-rectangles
    squareRecursion(x + bx, y + by, w, h);
    squareRecursion(x + bx + w + bx, y + by, w, h);
    squareRecursion(x + bx, y + by + h + by, w, h);
    squareRecursion(x + bx + w + bx, y + by + h + by, w, h);
  }
}

Comments

Nobody has said anything yet.