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

Daily Creative Coding

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

ジオデシック・ドーム

/**
* Geodesic Dome
*
* @author aa_debdeb
* @date 2016/08/17
*/

int state = 0;
float scale = 100;
float phi = (1 + sqrt(5)) / 2.0;
float radious = sqrt(sq(1) + sq(phi));
ArrayList<Triangle> triangles;

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

  PVector[] vertices = {new PVector(0, 1, phi),
                        new PVector(0, -1, phi),
                        new PVector(0, -1, -phi),
                        new PVector(0, 1, -phi),
                        new PVector(phi, 0, 1),
                        new PVector(-phi, 0, 1),
                        new PVector(-phi, 0, -1),
                        new PVector(phi, 0, -1),
                        new PVector(1, phi, 0),
                        new PVector(-1, phi, 0),
                        new PVector(-1, -phi, 0),
                        new PVector(1, -phi, 0)};
  triangles = new ArrayList<Triangle>();
  triangles.add(new Triangle(vertices[0], vertices[8], vertices[9]));
  triangles.add(new Triangle(vertices[0], vertices[9], vertices[5]));
  triangles.add(new Triangle(vertices[0], vertices[5], vertices[1]));
  triangles.add(new Triangle(vertices[0], vertices[1], vertices[4]));
  triangles.add(new Triangle(vertices[0], vertices[4], vertices[8]));
  triangles.add(new Triangle(vertices[1], vertices[5], vertices[10]));
  triangles.add(new Triangle(vertices[1], vertices[10], vertices[11]));
  triangles.add(new Triangle(vertices[1], vertices[11], vertices[4]));
  triangles.add(new Triangle(vertices[2], vertices[3], vertices[7]));
  triangles.add(new Triangle(vertices[2], vertices[7], vertices[11]));
  triangles.add(new Triangle(vertices[2], vertices[11], vertices[10]));
  triangles.add(new Triangle(vertices[2], vertices[10], vertices[6]));
  triangles.add(new Triangle(vertices[2], vertices[6], vertices[3]));
  triangles.add(new Triangle(vertices[3], vertices[6], vertices[9]));
  triangles.add(new Triangle(vertices[3], vertices[9], vertices[8]));
  triangles.add(new Triangle(vertices[3], vertices[8], vertices[7]));
  triangles.add(new Triangle(vertices[4], vertices[11], vertices[7]));
  triangles.add(new Triangle(vertices[4], vertices[7], vertices[8]));
  triangles.add(new Triangle(vertices[5], vertices[9], vertices[6]));
  triangles.add(new Triangle(vertices[5], vertices[6], vertices[10]));
 
  for(int i = 0; i < 3; i++){
    ArrayList<Triangle> nextTriangles = new ArrayList<Triangle>();
    for(Triangle t: triangles){
      nextTriangles.addAll(t.divide());
    }
    triangles = nextTriangles;
  }  
}

void mousePressed(){
  state++;
  if(state == 3){
    state = 0;
  }
}

void draw(){
  background(0);
  translate(width / 2, height / 2);
  rotateX(frameCount * 0.001);
  rotateY(frameCount * 0.002);
  rotateZ(frameCount * 0.003);
  for(Triangle t: triangles){
    t.display();
  }
}

class Triangle{
  
  PVector[] vertices;
  
  Triangle(PVector v0, PVector v1, PVector v2){
    vertices = new PVector[3];
    vertices[0] = v0;
    vertices[1] = v1;
    vertices[2] = v2;
  }
  
  void display(){
    switch(state){
      case 0:
        stroke(255);
        fill(128);
        beginShape();
        for(int i = 0; i < 3; i++){
          vertex(vertices[i].x * scale, vertices[i].y * scale, vertices[i].z * scale);
        }
        endShape(CLOSE);
        break;
      case 1:
        stroke(255);
        noFill();
        beginShape();
        for(int i = 0; i < 3; i++){
          vertex(vertices[i].x * scale, vertices[i].y * scale, vertices[i].z * scale);
        }
        endShape(CLOSE);
        break;
      case 2:
        noFill();
        stroke(255);
        for(int i = 0; i < 3; i++){
          point(vertices[i].x * scale, vertices[i].y * scale, vertices[i].z * scale);
        }      
        break;
    }
  }
  
  ArrayList<Triangle> divide(){
    PVector[] midpoints = new PVector[3];
    for(int i = 0; i < 3; i++){
      int j = i != 2 ? i + 1: 0;
      PVector m = PVector.lerp(vertices[i], vertices[j], 0.5);
      m.normalize();
      m.mult(radious);
      midpoints[i] = m;
    }
    ArrayList<Triangle> triangles = new ArrayList<Triangle>();
    
    triangles.add(new Triangle(vertices[0], midpoints[0], midpoints[2]));
    triangles.add(new Triangle(vertices[1], midpoints[1], midpoints[0]));
    triangles.add(new Triangle(vertices[2], midpoints[2], midpoints[1]));
    triangles.add(new Triangle(midpoints[0], midpoints[1], midpoints[2]));   
    return triangles;
  }
}
f:id:aa_debdeb:20160810101208j:plain