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

Daily Creative Coding

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

カラーパーティクルの流れ

パーティクル ランダム
/**
* Noisy Flow for Color Particles
*
* @author aa_debdeb
* @date 2016/03/05
*/

ArrayList<Particle> particles;
float radianNoiseX, radianNoiseY;
float rNoiseX, rNoiseY;
float gNoiseX, gNoiseY;
float bNoiseX, bNoiseY;

void setup(){
  size(640, 640);
  particles = new ArrayList<Particle>();
  for(int i = 0; i < 1000; i++){
    particles.add(new Particle());
  }
  radianNoiseX = random(5000);
  radianNoiseY = random(5000);
  rNoiseX = random(5000);
  rNoiseY = random(5000);
  gNoiseX = random(5000);
  gNoiseY = random(5000);
  bNoiseX = random(5000);
  bNoiseY = random(5000);
  
  background(0);
  strokeWeight(1);
}

void draw(){
  for(Particle particle: particles){
    // for java mode
//    float radian = map(noise(radianNoiseX + mouseX + particle.pos.x * 0.02, radianNoiseY + mouseY + particle.pos.y * 0.02), 0, 1, 0, TWO_PI * 5) + random(PI);
//    float r =  map(noise(rNoiseX + mouseX + particle.pos.x * 0.005, rNoiseY + mouseY + particle.pos.y * 0.005), 0, 1, 0, 255);
//    float g =  map(noise(gNoiseX + mouseX + particle.pos.x * 0.005, gNoiseY + mouseY + particle.pos.y * 0.005), 0, 1, 0, 255);
//    float b =  map(noise(bNoiseX + mouseX + particle.pos.x * 0.005, bNoiseY + mouseY + particle.pos.y * 0.005), 0, 1, 0, 255);
    // for JavaScript mode
    float radian = map(noise(radianNoiseX + mouseX + particle.pos.x * 0.02, radianNoiseY + mouseY + particle.pos.y * 0.02), 0.4, 0.6, 0, TWO_PI * 5) + random(PI);
    float r =  map(noise(rNoiseX + mouseX + particle.pos.x * 0.005, rNoiseY + mouseY + particle.pos.y * 0.005), 0.4, 0.6, 0, 255);
    float g =  map(noise(gNoiseX + mouseX + particle.pos.x * 0.005, gNoiseY + mouseY + particle.pos.y * 0.005), 0.4, 0.6, 0, 255);
    float b =  map(noise(bNoiseX + mouseX + particle.pos.x * 0.005, bNoiseY + mouseY + particle.pos.y * 0.005), 0.4, 0.6, 0, 255);
    color c = color(r, g, b, 20);
    stroke(c);

    particle.update(radian);
    particle.display();  
  }
}

void keyPressed(){
  background(0);
}

class Particle{
  
  PVector pos, pPos;
  float step;
  
  Particle(){
    pos = new PVector(random(width), random(height));
    step = map(random(1), 0, 1, 1, 1.5);
  }
  
  void update(float radian){
    pPos = pos;
    pos = PVector.add(pos, new PVector(step * cos(radian), step * sin(radian)));  
    if(pos.x < 0 || pos.x >= width || pos.y < 0 || pos.y >= height){
      pos = new PVector(random(width), random(height));
      pPos = new PVector(pos.x, pos.y);
    }
  }
  
  void display(){
    line(pPos.x, pPos.y, pos.x, pos.y);
  }
   
}