Daily Creative Coding

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

三角形のループ

/**
* triangle loop
*
* @author aa_debdeb
* @date 2016/12/27
*/

float MAX_RADIUS = 200;
int LOOP = 120;

color bg = color(219, 175, 184);
color c1 = color(153, 14, 42);
color c2 = color(220, 20, 60);

void setup(){
  size(500, 500);
  noStroke();
  fill(0);
}

void draw(){
  background(bg);
  translate(width / 2 , height / 2);
  float t = float(frameCount % LOOP) / LOOP;
  if(t < 0.5){
    pushMatrix();
    rotate(map(t, 0, 0.5, 0, TWO_PI / 6));
    float radius = map(calcQuad(map(t, 0, 0.5, 0, 1.0)), 0, 1, MAX_RADIUS, MAX_RADIUS / 2); 
    fill(c1);
    drawTriangle(radius);
    popMatrix();
  } else {  
    pushMatrix();
    rotate(TWO_PI / 6);
    fill(c1);
    drawTriangle(MAX_RADIUS / 2);
    popMatrix();
    float radius = map(calcQuad(map(t, 0.5, 1.0, 0, 1.0)), 0, 1, 0, MAX_RADIUS / 2);
    for(int i = 0; i < 3; i++){
      pushMatrix();
      translate(MAX_RADIUS / 2 * cos(TWO_PI / 3 * i - HALF_PI), MAX_RADIUS / 2 * sin(TWO_PI / 3 * i - HALF_PI));
      fill(lerpColor(c2, c1, map(t, 0.5, 1.0, 0, 1)));
      drawTriangle(radius);
      popMatrix();
    }
  }
}

void drawTriangle(float radius){
    beginShape();
    for(int i = 0; i < 3; i++){
      vertex(radius * cos(TWO_PI / 3 * i - HALF_PI), radius * sin(TWO_PI / 3 * i - HALF_PI));
    }
    endShape(CLOSE);
}

float calcQuad(float v){
  if(v < 0.5){
    return 2.0 * v * v;
  } else {
    return -2.0 * (v - 1.0) * (v - 1.0) + 1.0;
  }
}
f:id:aa_debdeb:20161223204610j:plain