Daily Creative Coding

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

パーティクルの間に線を描く

/**
* movers for drawing lines
*
* @author aa_debdeb
* @date 2016/05/31
*/

ArrayList<Particle> particles;

void setup(){
  size(640, 480);
  frameRate(360);
  particles = new ArrayList<Particle>();
  for(int i = 0; i < 10; i++){
    particles.add(new Particle());
  }
  background(255);
  colorMode(HSB, 360, 100, 100);
}

void draw(){
  for(int i = 0; i < particles.size() - 1; i++){
    Particle p1 = particles.get(i);
    for(int j = i + 1; j < particles.size(); j++){
      Particle p2 = particles.get(j);
      float saturation = (p1.saturation + p2.saturation) / 2.0;
      stroke(340, saturation, 100, 10);
      line(p1.pos.x, p1.pos.y, p2.pos.x, p2.pos.y);
    }  
  }
  for(Particle p: particles){
    stroke(340, p.saturation, 100, 10);
    line(0, 0, p.pos.x, p.pos.y);
    line(width, 0, p.pos.x, p.pos.y);
    line(0, height, p.pos.x, p.pos.y);
    line(width, height, p.pos.x, p.pos.y);
  }
  for(Particle p: particles){
    p.update();
  }
} 

class Particle{
  
  PVector pos, vel;
  float saturation;
  
  Particle(){
    pos = new PVector(random(width), random(height));
    vel = new PVector(random(-1, 1), random(-1, 1));
    saturation = random(100);
  }
  
  void update(){
    pos.add(vel);
    if(pos.x < 0){
      vel.x *= -1;
      pos.x += vel.x;
    }
    if(pos.x >= width){
      vel.x *= -1;
      pos.x += vel.x;
    }
    if(pos.y < 0){
      vel.y *= -1;
      pos.y += vel.y;
    }
    if(pos.y >= height){
      vel.y *= -1;
      pos.y += vel.y;
    }
  }
  
}