Daily Creative Coding

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

パーティクルで色のついた三角形

/**
* Colored Triangles by Particles
*
* @author aa_debdeb
* @date 2016/02/26
*/


ArrayList<Particle> particles;

void setup(){
  size(500, 500);
  frameRate(30);
  noStroke();
  
  particles = new ArrayList<Particle>();
  for(int i = 0; i < 100; i++){
    particles.add(new Particle());
  }  
}

void draw(){
  background(0);
  
  for(Particle p: particles){
    fill(p.col);
    ellipse(p.pos.x, p.pos.y, 3, 3);
    p.update();
  }
  
  for(int i = 0; i < particles.size() - 2; i++){
    Particle p1 = particles.get(i);
    for(int j = i + 1; j < particles.size() - 1; j++){
      Particle p2 = particles.get(j);
      for(int k = j + 1; k < particles.size(); k++){
        Particle p3 = particles.get(k);
        if(PVector.dist(p1.pos, p2.pos) <= 60 && PVector.dist(p2.pos, p3.pos) <= 60 && PVector.dist(p3.pos, p1.pos) <= 60){
          float r = (red(p1.col) + red(p2.col) + red(p3.col)) / 3;
          float g = (green(p1.col) + green(p2.col) + green(p3.col)) / 3;
          float b = (blue(p1.col) + blue(p2.col) + blue(p3.col)) / 3;
          fill(r, g, b, 100);
          triangle(p1.pos.x, p1.pos.y, p2.pos.x, p2.pos.y, p3.pos.x, p3.pos.y);
        }
      }
    }
  }
}

class Particle {
  
  PVector pos;
  PVector vel;
  color col;
  
  Particle(){
    pos = new PVector(random(width), random(height));
    float velAng = random(TWO_PI);
    vel = new PVector(3 * cos(velAng), 3 * sin(velAng));  
    col = color(random(255), random(255), random(255));
  }
  
  void update(){
    pos.add(vel);
    if(pos.x < 0 || pos.x >= width){
      vel.x *= -1;      
    }
    if(pos.y < 0 || pos.y >= height){
      vel.y *= -1;
    }
  }
  
}