Daily Creative Coding

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

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

/**
* movers for drawing lines 2
*
* @author aa_debdeb
* @date 2016/06/01
*/

ArrayList<Mover> movers1;
ArrayList<Mover> movers2;

void setup(){
  size(600, 600);
  movers1 = new ArrayList<Mover>();
  for(int i = 0; i < 10; i++){
    movers1.add(new Mover(0));
  }  
  movers2 = new ArrayList<Mover>();
  for(int i = 0; i < 10; i++){
    movers2.add(new Mover(width));
  }
  background(255);
  colorMode(HSB, 360, 100, 100);
}

void draw(){
  for(Mover m1: movers1){
    for(Mover m2: movers2){
      float sat1 = map(m1.pos.y, 0, height, 100, 0);
      float sat2 = map(m2.pos.y, 0, height, 100, 0);
      float sat = (sat1 + sat2) / 2.0;
      float hue = (m1.hue + m2.hue) / 2.0;
      stroke(hue, sat, 100, 30);
      line(m1.pos.x, m1.pos.y, m2.pos.x, m2.pos.y);
    }
  }  
  for(Mover m: movers1){
    float sat = map(m.pos.y, 0, height, 100, 0);
    stroke(m.hue, (sat + 100) / 2.0, 100, 30);
    line(m.pos.x, m.pos.y, width, 0);
    stroke(m.hue, sat / 2.0, 100, 30);
    line(m.pos.x, m.pos.y, width, height);
  }
  for(Mover m: movers2){
    float sat = map(m.pos.y, 0, height, 100, 0);
    stroke(m.hue, (sat + 100) / 2.0, 100, 30);
    line(m.pos.x, m.pos.y, width, 0);
    stroke(m.hue, sat / 2.0, 100, 30);
    line(m.pos.x, m.pos.y, width, height);
  }
  for(Mover m: movers1){
    m.update();
  }
  for(Mover m: movers2){
    m.update();
  }
}

class Mover{

  PVector pos, vel;
  float hue;
  
  Mover(float x){
    pos = new PVector(x, random(height));
    vel = new PVector(0, random(-2, 2));
    hue = random(300, 360);
  }
  
  void update(){
    pos.add(vel);
    if(pos.y < 0){
      vel.y *= -1;
      pos.y += vel.y;
    }
    if(pos.y >= width){
      vel.y *= -1;
      pos.y += vel.y;
    }
  }
}