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

Daily Creative Coding

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

/**
* eddy
*
* @author aa_debdeb
* @date 2016/09/16
*/

float dir;
ArrayList<Particle> particles;

void setup(){
  size(640, 480);  
  colorMode(HSB, 360, 100, 100);
  mousePressed();
}

void mousePressed(){
  background(random(360), random(30), random(100));
  stroke(random(360), random(30), random(100));
  strokeWeight(1);
  particles = new ArrayList<Particle>();
  for(int i = 0; i < 500; i++){
    particles.add(new Particle());
  }
  dir = random(1) < 0.5 ? -1: 1;
}

void draw(){
  translate(width / 2, height / 2);
  for(Particle p: particles){
    p.display();
  }
}

class Particle{

  PVector loc;
  
  Particle(){
    float locAng = random(TWO_PI);
    float radious = sqrt(sq(width) + sq(height)) / 2;
    loc = new PVector(radious * cos(locAng), radious * sin(locAng));
  }
  
  void display(){
    PVector vel = PVector.mult(loc, -1);
    vel.normalize();
    vel.limit(0.5);
    PVector nLoc = PVector.add(loc, vel);
    vel.normalize();
    vel.limit(4);
    vel.rotate(dir * HALF_PI);
    nLoc.add(vel);
    line(loc.x, loc.y, nLoc.x, nLoc.y);
    loc = nLoc;
  }
  
}
f:id:aa_debdeb:20160911175441j:plain