2013年4月17日 星期三

[對照]蝦爸的p5範例 5-2

[Processing Example]

 

在這個範例中先利用beginShape() + vertex()畫出polygon三角面,再利用迴圈旋轉複製四等分,成角錐狀,最後再透過遞迴完成複雜的大金字塔形。

[Processing code] ↓


void setup() {
  size(400, 400, P3D);
}

void draw() {
  world();
  scale(2);
  tritri(3);
}

void world() {
  camera(mouseX*2, -mouseY*2, 400, 0, 0, 0, 0, 1, 0);
  background(0);
  stroke(255, 0, 0);
  line(-1000, 0, 0, 1000, 0, 0);
  stroke(0, 255, 0);
  line(0, 1000, 0, 0, -1000, 0);
  stroke(0, 0, 255);
  line(0, 0, 1000, 0, 0, -1000);
}

void tritri(int ttime) {
  float tx, ty, tl;
  tx=0;
  ty=0;
  tl=100;

  if (ttime==0) {
    stroke(255);
    noFill();
    for (int i=0;i<4;i++) {
      pushMatrix();
      beginShape();
      rotateY(radians(i*90));
      translate(0, 0, tl);
      vertex(tx, ty-tl, 0-tl);
      vertex(tx+tl, ty+tl, 0);
      vertex(tx-tl, ty+tl, 0);
      endShape(CLOSE);
      popMatrix();
    }
    beginShape();   
    vertex(tx+tl, ty+tl, tl);
    vertex(tx-tl, ty+tl, tl);
    vertex(tx-tl, ty+tl, 0-tl); 
    vertex(tx+tl, ty+tl, 0-tl);   
    endShape(CLOSE);
  }
  else if (ttime>0) {
    scale(0.5);
    for (int i=0;i<5;i++) {
      switch(i) {
      case 0:
        pushMatrix();
        translate(0, -tl, 0);
        tritri(ttime-1);
        popMatrix();
        break;
      case 1:
        pushMatrix();
        translate(tl, tl, tl);
        tritri(ttime-1);
        popMatrix();
        break;
      case 2:
        pushMatrix();
        translate(-tl, tl, tl);
        tritri(ttime-1);
        popMatrix();
        break;
      case 3:
        pushMatrix();
        translate(-tl, tl, -tl);
        tritri(ttime-1);
        popMatrix();
        break;
      case 4:
        pushMatrix();
        translate(tl, tl, -tl);
        tritri(ttime-1);
        popMatrix();
        break;
      }
    }
  }
}


[Pd Example]



[Pd code] ↓


(main.pd)


(sub patch [pd-repeat])


(tritri.pd)

File Download