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

Daily Creative Coding

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

円の中のパーティクル間を線で結ぶ

パーティクル
/**
* line drawers in circle
* 
* @author aa_debdeb
* @date 2016/06/11
*/

float maxRadious = 200;
float minVel = 0.5;
float maxVel = 1.0;

ArrayList<Particle> particles;;

void setup(){
  size(500, 500);
  frameRate(180);
  background(220);
  colorMode(HSB, 360, 100, 100);
  
  particles = new ArrayList<Particle>();
  for(int i = 0; i < 30; i++){
    particles.add(new Particle());
  }
}

void draw(){
  translate(width / 2, height / 2);
  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(0, saturation, 100, 5);
      line(p1.pos.x, p1.pos.y, p2.pos.x, p2.pos.y);
    }
  }
  for(Particle p: particles){
    p.update();
  }
}

class Particle{
  
  PVector pos, vel;
  float saturation;
  
  Particle(){
    float posAng = random(TWO_PI);
    float posSize = random(maxRadious);
    pos = new PVector(posSize * cos(posAng), posSize * sin(posAng));
    float velAng = random(TWO_PI);
    float velSize = random(minVel, maxVel);
    vel = new PVector(velSize * cos(velAng), velSize * sin(velAng));
    saturation = random(100);
  }
  
  void update(){
    pos.add(vel);
    if(pos.mag() > maxRadious){
      pos.limit(maxRadious);
      float posAng = atan2(pos.y, pos.x);
      float velAng = atan2(vel.y, vel.x);
      float velSize = vel.mag();
      vel.x = velSize * cos(2 * posAng - velAng - PI);
      vel.y = velSize * sin(2 * posAng - velAng - PI);
    }
  }
}