Daily Creative Coding

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

たくさんの円を傾けながら重ねて円錐をつくる

/**
* penguins
*
* @author aa_debdeb
* @date 2016/07/18
*/

float maxR = 100;
float maxZ = 300;
float vStep = 0.01;
ArrayList<Tree> trees;

void setup(){
  size(640, 480, P3D);
  colorMode(HSB, 360, 100, 100);
  initialize();
}

void initialize(){
  trees = new ArrayList<Tree>();
  for(int i = 0; i < 4; i++){
    trees.add(new Tree());
  }
  float hue = random(360);
  float[] hueStep = {0, 30, 180, 210};
  for(int i = 0; i < 4; i++){
    float h = hue + hueStep[i];
    if(h >= 360){h -= 360;}
    trees.get(i).sc = color(h, 60, 100);
  }
  noFill();
  
}

void mousePressed(){
  initialize(); 
}

void draw(){
  background(0, 0, 100);
  translate(width / 2, height * 2.0 / 3, -200);
  rotateX(HALF_PI * 4.0 / 5);
  rotateZ(map(mouseX, 0, width, -PI * 2.0 / 3, PI * 2.0 / 3));
  for(Tree tree: trees){
    tree.display();
  }
}

class Tree{
  
  PVector pos;
  color sc;
  PVector noiseX, noiseY;
  
  Tree(){
    pos = new PVector(random(-200, 200), random(-200, 200));  
    noiseX = new PVector(random(10000), random(10000));
    noiseY = new PVector(random(10000), random(10000));
  }
  
  void display(){
    stroke(sc);
    pushMatrix();
    translate(pos.x, pos.y, 0);
    for(float v = 0; v <= 1.0; v += vStep){
      float r = map(sq(1.0 - v), 0, 1, 0, maxR);
      ellipse(0, 0, r * 2, r * 2);
      translate(0, 0, vStep * maxZ);
      rotateY(map(noise(v * 0.1 + noiseY.x, frameCount * 0.004 + noiseY.y), 0, 1, -PI / 32, PI / 32));
      rotateX(map(noise(v * 0.1 + noiseX.x, frameCount * 0.004 + noiseX.y), 0, 1, -PI / 32, PI / 32));

    }
    popMatrix();  
  }
  
}