sketch_210219c

sketch_210219c.pde

import colorblind.ColorBlindness;

final int PAD = 2;
int num = 4;

void settings() {
  float f = 4.0;
  size((int)(148*f), (int)(210*f));
}

void setup() {
  noLoop();
  
  // ColorBlindness colorBlindness = new ColorBlindness(this);
  
  // colorBlindness.simulateAchromatopsia();
  // colorBlindness.simulateBlueConeMonochromacy();
  // colorBlindness.simulateDeuteranopia();
  // colorBlindness.simulateProtanopia();
  // colorBlindness.simulateTritanopia();
}

void draw() {
  // background(#5EC708);
  background(#469406);
  translate(width / 2.0, height / 2.0);

  PVector[] basis = makeBasis(3);
  PVector[][] lattice = makeLattice(basis);

  for (int i = 0; i < lattice.length; i++) {
    for (int j = 0; j < lattice[i].length; j++) {
      PVector p = lattice[i][j];

      pushMatrix();
      translate(p.x, p.y);
      
      // stroke(#C77412);
      stroke(#94560D);
      strokeWeight(0.8);
      noFill();
      
      drawShape(6);

      popMatrix();
    }
  }
  
  saveFrame("frames/image.png");
}


PVector[] makeBasis(int n) {
  float t = TWO_PI / n;
  PVector v1 = new PVector(cos(0.0*t), sin(0.0*t));
  PVector v2 = new PVector(cos(1.0*t), sin(1.0*t));
  return new PVector[] {v1, v2};
}

PVector[][] makeLattice(PVector[] basis) {

  float scalar = width / (float)num;
  PVector v1 = PVector.mult(basis[0], scalar);
  PVector v2 = PVector.mult(basis[1], scalar);
  PVector[][] lattice = new PVector[2*num+1][2*num+1];

  for (int i = -num; i < num + 1; i++) {
    for (int j = -num; j < num + 1; j++) {
      lattice[i+num][j+num] = PVector.add(PVector.mult(v1, j), PVector.mult(v2, i));
    }
  }

  return lattice;
}

void drawShape(int n) {
  float scalar = width / float(num);
  
  PVector[] points = new PVector[n];
  
  for (int k = 0; k < n; k++) {
    PVector p = PVector.fromAngle(TWO_PI / (float)n * (k + 0.5));
    // (scalar / 2.0) が多角形の外接円の半径になっていて,
    // 「中心から辺までの距離」が外接円の半径に一致するように
    // スケーリングする係数がcos(TWO_PI / float(n) / 2.0)
    p.mult((scalar / 2.0) / cos(TWO_PI / float(n) / 2.0));
    points[k] = p;
  }

  beginShape();
  for (int k = 0; k < n; k++) {
      vertex(points[k].x, points[k].y);
  }
  endShape(CLOSE);
  
  fill(#4EA607);
  beginShape();
  for (int k = 0; k < n; k += 2) {
    PVector p1 = points[k];
    PVector p2 = points[(k + 1) % n];
    
    // 三角形の重心
    PVector center = PVector.mult(PVector.add(p1, p2), 1.0 / 3.0);
    
    vertex(p1.x, p1.y);
    vertex(center.x, center.y);
    vertex(p2.x, p2.y);
    vertex(0.0, 0.0);
  }
  endShape();
  
  noFill();
  beginShape();
  for (int k = 0; k < n; k++) {
    PVector p1 = points[k];
    PVector p2 = points[(k + 1) % n];
    
    // 三角形の重心
    PVector center = PVector.mult(PVector.add(p1, p2), 1.0 / 3.0);
    
    line(0, 0, p1.x, p1.y);
    line(center.x, center.y, 0, 0);
    line(center.x, center.y, p1.x, p1.y);
    line(center.x, center.y, p2.x, p2.y);
  }
  endShape();
}