Daily Creative Coding

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

中心に集中する線

/**
* lines to center
*
* @author aa_debdeb
* @date 2016/08/28
*/

ArrayList<Particle> particles;

void setup(){
  size(500, 500);
  colorMode(HSB, 360, 100, 100);
  particles = new ArrayList<Particle>();
  strokeWeight(2);  
}

void draw(){
  background(0, 100, 0);
  translate(width / 2, height / 2);
  ArrayList<Particle> nextParticles = new ArrayList<Particle>();
  for(Particle p: particles){
    p.display();
    p.update();
    if(!p.isDisappeared()){
      nextParticles.add(p);
    }
  }
  particles = nextParticles;
  int num = int(random(5));
  for(int i = 0; i < num; i++){
    particles.add(new Particle());
  }
}

class Particle{
  
  float v;
  float angle;
  float maxLen;
  float vSpeed;
  color c;
  
  Particle(){
    v = 1.0;
    angle = random(TWO_PI);
    maxLen = random(20, 50);
    vSpeed = random(0.01, 0.03);
    c = color(random(100, 200), 50, 100, 200);
  }
  
  void display(){
    float len = map(-4 * sq(v - 0.5) + 1, 0, 1, 0, maxLen);
    float x1 = (v * 200 - len / 2) * cos(angle);
    float y1 = (v * 200 - len / 2) * sin(angle);
    float x2 = (v * 200 + len / 2) * cos(angle);
    float y2 = (v * 200 + len / 2) * sin(angle);
    stroke(c);
    line(x1, y1, x2, y2);
  }
  
  void update(){
    v -= vSpeed;
  }
  
  boolean isDisappeared(){
    return v <= 0;
  }
}
f:id:aa_debdeb:20160825221136j:plain