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

Daily Creative Coding

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

奥行きのある二次元

/**
* 2D with depth
*
* @author aa_debdeb
* @date 2016/09/11
*/

float MAX_DEPTH = 10000;
float BOTTOM_WIDTH = 5; 
ArrayList<Circle> circles;

void setup(){
  size(500, 500);
  circles = new ArrayList<Circle>();
}

void draw(){
  background(255, 255, 240);
  translate(width / 2, height / 2);
  stroke(51, 0, 0);
  strokeWeight(2);
  noFill();
  line(-width / 2, -height / 2, -BOTTOM_WIDTH, -BOTTOM_WIDTH);
  line(width / 2, -height / 2, BOTTOM_WIDTH, -BOTTOM_WIDTH);
  line(width / 2, height / 2, BOTTOM_WIDTH, BOTTOM_WIDTH);
  line(-width / 2, height / 2, -BOTTOM_WIDTH, BOTTOM_WIDTH); 
  rect(-BOTTOM_WIDTH, -BOTTOM_WIDTH, BOTTOM_WIDTH * 2, BOTTOM_WIDTH * 2);
  
  ArrayList<Circle> nextCircles = new ArrayList<Circle>();
  for(int i = circles.size() - 1; i >= 0; i--){
    Circle circle = circles.get(i);
    circle.display();
    circle.update();
    if(!circle.isPassed()){
      nextCircles.add(circle);
    }
  }
  circles = nextCircles;
  if(random(1) < 0.1){
    circles.add(new Circle());
  }
}

class Circle{

  PVector center;
  float speed;
  float diameter;
  color c;
  
  Circle(){
    center = new PVector(random(-width / 2, width / 2), random(-height / 2, height / 2), MAX_DEPTH);
    speed = random(10, 30);
    diameter = random(20, 50);
    c = color(random(100, 200), random(100, 200), random(255));
  }
  
  void update(){
    center.z -= speed;
  }
  
  void display(){
    stroke(c);
    noFill();
    float x = map(center.z, MAX_DEPTH, 0, 0, center.x);
    float y = map(center.z, MAX_DEPTH, 0, 0, center.y);
    float d = map(center.z, MAX_DEPTH, 0, 0, diameter);
    ellipse(x, y, d, d);
  }
  
  boolean isPassed(){
    return center.z < 0;
  }
  
}
f:id:aa_debdeb:20160909195501j:plain