Daily Creative Coding

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

同じ色相でブロックを再帰的に分割する

/**
* same hue
*
* @author aa_debdeb
* @date 2016/04/03
*/

void setup(){
  size(640, 480);
  noStroke();
  colorMode(HSB, 360, 100, 100);
  mousePressed();
}

void draw(){
  
}

void mousePressed(){
  float hue = random(360);
  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 {
      float sat = random(30, 100);
      float bri = random(30, 100);
      fill(hue, sat, bri);
      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;
  }
}