Daily Creative Coding

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

都市を見下ろす

/**
* birds-eye view of city
*
* @author aa_debdeb
* @date 2016/07/14
*/

void setup(){
  size(500, 500, P3D);
  mousePressed();
}

void draw(){

}

void mousePressed(){
  float maxLocRadious = 500;
  float minSize = 5;
  float maxSize = 30;
  float noiseX = random(10000);
  float noiseY = random(10000);
  float noiseScale = 0.05;
  ArrayList<Block> blocks = new ArrayList<Block>();
  for(int i = 0; i < 100000; i++){
    float size = random(minSize, maxSize);
    float locRadious = map(sqrt(random(1)), 0, 1, 0, maxLocRadious);
    float locRadian = random(TWO_PI);
    PVector loc = new PVector(locRadious * cos(locRadian), locRadious * sin(locRadian), size / 2);
    if(noise(loc.x * noiseScale + noiseX, loc.y * noiseScale + noiseY) < 0.5){
      continue;
    }
    int stack = int(random(1, 5));
    float radian = random(HALF_PI);
    boolean isOverlapped = false;
    for(Block other: blocks){
      float d = PVector.dist(other.loc, loc);
      if(d < sqrt(2) * other.size / 2 + sqrt(2) * size / 2){
        isOverlapped = true;
        break;
      }
    }
    if(!isOverlapped){
      Block block = new Block(loc, radian, size, stack);
      blocks.add(block);
    }
  }
  
  background(255);
  noStroke();
  translate(width / 2,  height / 2, 0);
  rotateX(PI / 3);
  lights();  
  for(Block block: blocks){
    block.display();
  }
}

class Block{
  
  PVector loc;
  float radian, size;
  int stack;
  float gray;
  
  Block(PVector _loc, float _radian, float _size, int _stack){
    loc = _loc;
    radian = _radian;
    size = _size;
    stack = _stack;
    gray = random(150, 220);
  }
  
  void display(){
    fill(gray);
    for(int i = 0; i < stack; i++){
      pushMatrix();
      translate(loc.x, loc.y, loc.z + i * size);
      rotateZ(radian);
      box(size);
      popMatrix();
    }
  }
  
}