Daily Creative Coding

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

球を段々と構築する

/**
* construction of sphere
*
* @author aa_debdeb
* @date 2016/06/20
*/

float radious = 200;
int resolution = 30;
int num = 0;
int numStep = 1;
int maxNum = resolution * (resolution / 2);

void setup(){
  size(500, 500, P3D);
  fill(255);
  stroke(128);
  strokeWeight(1);
}

void draw(){
  background(0);
//  lights();
  translate(width / 2, height / 2, 0);
  rotateX(-PI / 4);
  float step = TWO_PI / resolution;
  int n = 0;
  for(int i = 0; i < resolution / 2; i++){
    for(int j = 0; j < resolution; j++){
      float r1 = step * i;
      float nr1 = step * (i + 1);
      float r2 = step * j;
      float nr2 = step * (j + 1);
      PVector p1 = new PVector(radious * sin(r1) * cos(r2), radious * cos(r1), radious * sin(r1) * sin(r2));
      PVector p2 = new PVector(radious * sin(nr1) * cos(r2), radious * cos(nr1), radious * sin(nr1) * sin(r2));
      PVector p3 = new PVector(radious * sin(nr1) * cos(nr2), radious * cos(nr1), radious * sin(nr1) * sin(nr2));
      PVector p4 = new PVector(radious * sin(r1) * cos(nr2), radious * cos(r1), radious * sin(r1) * sin(nr2));
      beginShape();
      vertex(p1.x, p1.y, p1.z);
      vertex(p2.x, p2.y, p2.z);
      vertex(p3.x, p3.y, p3.z);
      vertex(p4.x, p4.y, p4.z);
      endShape();
      n++;
      if(n > num){
        num += numStep;
        if(num == 0 || num == maxNum - 1){
          numStep = -numStep;
        }
        return;
      }
    }
  }
}