読者です 読者をやめる 読者になる 読者になる

Daily Creative Coding

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

CMYのドット

/**
* CMY dots
*
* @author aa_debdeb
* @date 2017/01/11
*/

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

void mousePressed(){
  
  ArrayList<Obj> objs = new ArrayList<Obj>();
  
  color[] colors = {color(255, 0, 255),
                    color(0, 255, 255),
                    color(255, 255, 0)};
  for(int i = 0; i < 3; i++){
    PVector center = new PVector(random(width / 3.0, width / 3.0 * 2), random(height / 3.0, height / 3.0 * 2), 0);
    float rotAng = random(TWO_PI);
    for(float w = -300; w <= 300; w += 30){
      for(float h = -300; h <= 300; h += 30){
        float x = cos(rotAng) * w - sin(rotAng) * h;
        float y = sin(rotAng) * w + cos(rotAng) * h;
        float z = map(w + h, -600, 600, 100, 0);
        objs.add(new Obj(new PVector(x + center.x, y + center.y, z + center.z), 30, colors[i]));
      }
    }
  }
    
  background(255);
  while(!objs.isEmpty()){
    Obj deepObj = objs.get(0);
    for(Obj obj: objs){
      if(deepObj.position.z < obj.position.z){
        deepObj = obj;
      }
    }
    deepObj.render();
    objs.remove(deepObj);
  }
}

void draw(){

}

class Obj {
  
  PVector position;
  float size;
  color c;
  
  Obj(PVector position, float size, color c){
    this.position = position;
    this.size = size;
    this.c = c;
  }
  
  void render(){
    stroke(255);
    strokeWeight(2);
    fill(c);
    float s = map(position.z, 0, 100, size, 5);
    ellipse(position.x, position.y, s, s);
    
  }
  
}
f:id:aa_debdeb:20170106222642j:plain