Recursive Scape


  // Recursive Subdivision Scape - Jim Bumgardner
  
  void setup()
  {
    size(500,500);
    smooth();
  }
  
  void recurseRect(float x, float y, float w, float h)
  {
    float pw = min(random(min(w,h)),width/3);
    float ph = pw;
    float px = random(x,x+w-pw);
    float py = random(y,y+h-pw);
    rect(px+.5,py+.5,pw-1,pw-1);
  
    if (w < 3 || h < 3)
      return;
  
    if (h > w) {
      recurseRect( x,y,w,py-y);
      recurseRect( x,py,px-x,ph);
      recurseRect( px+pw,py,x+w-(px+pw),ph);
      recurseRect( x,py+ph,w,y+h-(py+ph));
  
    } else {
      recurseRect( x,y,px-x,h);
      recurseRect( px,y,pw,py-y);
      recurseRect( px,py+ph,pw,y+h-(py+ph));
      recurseRect( px+pw,y,x+w-(px+pw),h);
    }
  }
  
  void draw()
  {
    float r = millis()*.0001;
    int ri = int(r);
    r -= ri;
    fill(255,r*255);
    rect(0,0,width,height);
    fill(0,r*255);
    randomSeed(ri);
    recurseRect(0,0,width,height); // -width/2,-height/2,width*2,height*2);  
  }