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

Daily Creative Coding

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

線の下を動くもの

ランダム
/**
* movers under lines
*
* @author aa_debdeb
* @date 2017/01/18
*/

ArrayList<Mover> movers;

void setup(){
  size(640, 480);
  noFill();
  movers = new ArrayList<Mover>();
}

void draw(){
  background(26, 11, 8);

  boolean isPink = true;  
  for(float h = -50; h <= height + 50; h += 10){
    if(isPink){
      stroke(230, 26, 105);
    } else {
      stroke(113, 199, 209);
    }
    isPink = !isPink;
    beginShape();
    for(float w = -50; w <= width + 50; w += 10){
      float mw = w + random(-2, 2);
      float mh = h + random(-2, 2);
//      float mw = w;
//      float mh = h;
      for(Mover mover: movers){
        float d = mover.dist(w, h);
        if(d < 100){
          mw += map(d, 0, 100, 1, 0) * mover.vel.x * 3;
          mh += map(d, 0, 100, 1, 0) * mover.vel.y * 3;
        }
      }
      vertex(mw, mh);
    } 
    endShape();
  }
  
  ArrayList<Mover> nextMovers = new ArrayList<Mover>();
  for(Mover mover: movers){
    mover.update();
    if(!mover.canDelete()){
      nextMovers.add(mover);
    }
  }
  movers = nextMovers;
    
  if(random(1) < 1){
    movers.add(new Mover());
  }

}

class Mover {
  PVector loc, vel;
  
  Mover(){
    float locSize = 500;
    float locAng = random(TWO_PI);
    loc = new PVector(locSize * cos(locAng) + width / 2, locSize * sin(locAng) + height / 2);
    float velSize = 10;
    float velAng = random(TWO_PI);
    vel = new PVector(velSize * cos(velAng), velSize * sin(velAng));
  }
  
  void update(){
    loc.add(vel);
  }
  
  float dist(float x, float y){
    return sqrt(sq(loc.x - x) + sq(loc.y - y));
  }
  
  boolean canDelete(){
    return sqrt(sq(loc.x - width / 2) + sq(loc.y - height / 2)) > 600;
  }
}
f:id:aa_debdeb:20170112221808j:plain