Daily Creative Coding

元「30 min. Processing」。毎日、Creative Codingします。

ブロックを再帰的に分割する

/**
* recursive segmentation
*
* @author aa_debdeb
* @date 2016/04/01
*/

void setup(){
  size(640, 480);
  noStroke();
  mousePressed();
}

void draw(){
  
}

void mousePressed(){
  ArrayList<Block> queue = new ArrayList<Block>();
  queue.add(new Block(0, 0, width, height, 0));
  while(!queue.isEmpty()){
    Block b = queue.remove(0);
    if(b.depth < 8 && (random(1) < pow(1.0 - b.depth * 0.1, 1.0 / 3))){
      queue.add(new Block(b.x, b.y, b.w / 2, b.h / 2, b.depth + 1));
      queue.add(new Block(b.x + b.w / 2, b.y, b.w / 2, b.h / 2, b.depth + 1));
      queue.add(new Block(b.x, b.y + b.h / 2, b.w / 2, b.h / 2, b.depth + 1));
      queue.add(new Block(b.x + b.w / 2, b.y + b.h / 2, b.w / 2, b.h / 2, b.depth + 1));      
    } else {
      fill(random(255), random(255), random(255));
      rect(b.x, b.y, b.w, b.h);
    }
  }
}

class Block{
  float x, y, w, h;
  int depth;
  
  Block(float x, float y, float w, float h, int depth){
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
    this.depth = depth;
  }
}