Daily Creative Coding

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

不定形の輪っか

f:id:aa_debdeb:20161030094334j:plain

unshaped loops - OpenProcessing

/**
* unshaped loops
*
* @author aa_debdeb
* @date 2016/11/06
*/

float radius = 200;
ArrayList<Loop> loops;

void setup(){
  size(640, 640);
  colorMode(HSB, 360, 100, 100);
  loops = new ArrayList<Loop>();
  for(int i = 0; i < 6; i++){
    loops.add(new Loop());
  }
  background(0, 100, 0);
}

void draw(){
  fill(0, 100, 0, 30);
  noStroke();
  rect(0, 0, width, height);
  translate(width / 2, height / 2);
  for(Loop loop: loops){
    loop.display();
    loop.update();
  }
}


class Loop {
  
  ArrayList<Point> points;
  color c;
  
  Loop(){
    points = new ArrayList<Point>();
    for(float angle = 0; angle < 360; angle += 18){
      float radian = radians(angle);
      PVector loc = new PVector(radius * cos(radian), radius * sin(radian));
       points.add(new Point(loc));
    }
    c = color(random(1) < 0.5 ? random(320, 340): random(170, 190), 100, 100, 30);
  }
  
  void display(){
    noFill();
    stroke(c);
    strokeWeight(2);
    beginShape();
    curveVertex(points.get(0).loc.x, points.get(0).loc.y);
    for(Point p: points){
      curveVertex(p.loc.x, p.loc.y);
    }
    curveVertex(points.get(0).loc.x, points.get(0).loc.y);
    curveVertex(points.get(1).loc.x, points.get(1).loc.y);
    curveVertex(points.get(2).loc.x, points.get(2).loc.y);
    endShape();
  }
  
  void update(){
    for(Point p: points){
      p.update();
    }
  }
}

class Point {
  
  PVector loc, vel;
  
  Point(PVector _loc){
    loc = _loc;
    vel = PVector.mult(loc, -1);
    vel.normalize();
    vel.mult(random(0.5, 1));
  }
  
  void update(){
    loc.add(vel);
    if(loc.mag() > radius){
      float locAng = atan2(loc.y, loc.x);
      float velAng = atan2(vel.y, vel.x);
      float velMag = vel.mag();
      vel.x = velMag * cos(2 * locAng - velAng - PI);
      vel.y = velMag * sin(2 * locAng - velAng - PI);        }
  }
  
}