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

Daily Creative Coding

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

歯車

決定論
/**
* gears
*
* @author aa_debdeb
* @date 2016/12/17
*/

float bigR = 100;
float smallR = 90;
float angleStep = 5;
float time = 0.0;

void setup(){
  size(500, 500);
}

void draw(){
  background(255);  
  translate(width / 2, height / 2);
  float dist = bigR - (bigR - smallR) / 2;
  drawGear(-dist, -dist, time);
  drawGear(dist, -dist, -time + radians(angleStep));
  drawGear(-dist, dist, -time + radians(angleStep));
  drawGear(dist, dist, time);
  time += map(sin(frameCount * 0.01), -1, 1, 0, PI / 64);
}

void drawGear(float x, float y, float rot){
  pushMatrix();
  translate(x, y);
  rotate(rot);
  noStroke();
  fill(0);
  beginShape();
  float angle = 0;
  boolean isBigger = true;
  while(angle < 360){
    float radian = radians(angle);
    float radius = isBigger? bigR: smallR;
    vertex(radius * cos(radian), radius * sin(radian));
    angle += angleStep;
    isBigger = !isBigger;
  }
  endShape(CLOSE);
  fill(255);
  ellipse(0, 0, 20, 20);
  popMatrix();
}
f:id:aa_debdeb:20161212222206j:plain