Daily Creative Coding

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

ペンローズの三角形(影付き)

/**
* Penrose Triangle with Shadow
*
* @author aa_debdeb
* @date 2016/06/28
*/

color bg, c1, c2, c3;
float e = 70;
float w = 75;

void setup(){
  size(500, 500);
  stroke(0);
  strokeWeight(1);
  colorMode(HSB, 360, 100, 100);
  mousePressed();
}

void mousePressed(){
  float hue = random(360);
  float sat = random(100);
  c1 = color(hue, sat, random(20, 100));
  c2 = color(hue, sat, random(20, 100));
  c3 = color(hue, sat, random(20, 100));
  bg = color(random(360), random(0, 20), random(20, 100));
}

void draw(){
  background(bg);
  translate(width / 2, height * 3 / 5);
  fill(c1);
  drawSide();
  rotate(TWO_PI / 3);
  fill(c2);
  drawSide();
  rotate(TWO_PI / 3);
  fill(c3);
  drawSide();
}

void drawSide(){
  float sin30, sin60, cos30, cos60;
  sin30 = cos60 = sin(PI / 6);
  cos30 = sin60 = cos(PI / 6);
  beginShape();
  float x1 = -e * cos60;
  float y1 = (e * cos60) / sqrt(3);
  vertex(x1, y1);
  float x2 = x1 - w;
  float y2 = y1;
  vertex(x2, y2);
  float x3 = x2 + (e + 3.0 * w) * cos60;
  float y3 = y2 - (e + 3.0 * w) * sin60;
  vertex(x3, y3);
  float x4 = x3 + (e + 4.0 * w) * sin30;
  float y4 = y3 + (e + 4.0 * w) * cos30;
  vertex(x4, y4);
  float x5 = x4 - w * cos60;
  float y5 = y4 + w * sin60;
  vertex(x5, y5);
  float x6 = x5 - (e + 3 * w) * cos60;
  float y6 = y5 - (e + 3 * w) * sin60;
  vertex(x6, y6);
  endShape(CLOSE);
}