topical media & game development

talk show tell print

graphic-processing-site-examples-3D-Form-Icosahedra-Icosahedron.pde / pde



  class Icosahedron extends Shape3D{
  
    // icosahedron
    PVector topPoint;
    PVector[] topPent = new PVector[5];
    PVector bottomPoint;
    PVector[] bottomPent = new PVector[5];
    float angle = 0, radius = 150;
    float triDist;
    float triHt;
    float a, b, c;
  
    // constructor
    Icosahedron(float radius){
      this.radius = radius;
      init();
    }
  
    Icosahedron(PVector v, float radius){
      super(v);
      this.radius = radius;
      init();
    }
  
    // calculate geometry
    void init(){
      c = dist(cos(0)*radius, sin(0)*radius, cos(radians(72))*radius,  sin(radians(72))*radius);
      b = radius;
      a = (float)(Math.sqrt(((c*c)-(b*b))));
  
      triHt = (float)(Math.sqrt((c*c)-((c/2)*(c/2))));
  
      for (int i=0; i<topPent.length; i++){
        topPent[i] = new PVector(cos(angle)*radius, sin(angle)*radius, triHt/2.0f);
        angle+=radians(72);
      }
      topPoint = new PVector(0, 0, triHt/2.0f+a);
      angle = 72.0f/2.0f;
      for (int i=0; i<topPent.length; i++){
        bottomPent[i] = new PVector(cos(angle)*radius, sin(angle)*radius, -triHt/2.0f);
        angle+=radians(72);
      }
      bottomPoint = new PVector(0, 0, -(triHt/2.0f+a));
    }
  
    // draws icosahedron 
    void create(){
      for (int i=0; i<topPent.length; i++){
        // icosahedron top
        beginShape();
        if (i<topPent.length-1){
          vertex(x+topPent[i].x, y+topPent[i].y, z+topPent[i].z);
          vertex(x+topPoint.x, y+topPoint.y, z+topPoint.z);
          vertex(x+topPent[i+1].x, y+topPent[i+1].y, z+topPent[i+1].z);
        } 
        else {
          vertex(x+topPent[i].x, y+topPent[i].y, z+topPent[i].z);
          vertex(x+topPoint.x, y+topPoint.y, z+topPoint.z);
          vertex(x+topPent[0].x, y+topPent[0].y, z+topPent[0].z);
        }
        endShape(CLOSE);
  
        // icosahedron bottom
        beginShape();
        if (i<bottomPent.length-1){
          vertex(x+bottomPent[i].x, y+bottomPent[i].y, z+bottomPent[i].z);
          vertex(x+bottomPoint.x, y+bottomPoint.y, z+bottomPoint.z);
          vertex(x+bottomPent[i+1].x, y+bottomPent[i+1].y, z+bottomPent[i+1].z);
        } 
        else {
          vertex(x+bottomPent[i].x, y+bottomPent[i].y, z+bottomPent[i].z);
          vertex(x+bottomPoint.x, y+bottomPoint.y, z+bottomPoint.z);
          vertex(x+bottomPent[0].x, y+bottomPent[0].y, z+bottomPent[0].z);
        }
        endShape(CLOSE);
      }
  
      // icosahedron body
      for (int i=0; i<topPent.length; i++){
        if (i<topPent.length-2){
          beginShape();
          vertex(x+topPent[i].x, y+topPent[i].y, z+topPent[i].z);
          vertex(x+bottomPent[i+1].x, y+bottomPent[i+1].y, z+bottomPent[i+1].z);
          vertex(x+bottomPent[i+2].x, y+bottomPent[i+2].y, z+bottomPent[i+2].z);
          endShape(CLOSE);
  
          beginShape();
          vertex(x+bottomPent[i+2].x, y+bottomPent[i+2].y, z+bottomPent[i+2].z);
          vertex(x+topPent[i].x, y+topPent[i].y, z+topPent[i].z);
          vertex(x+topPent[i+1].x, y+topPent[i+1].y, z+topPent[i+1].z);
          endShape(CLOSE);
        } 
        else if (i==topPent.length-2){
          beginShape();
          vertex(x+topPent[i].x, y+topPent[i].y, z+topPent[i].z);
          vertex(x+bottomPent[i+1].x, y+bottomPent[i+1].y, z+bottomPent[i+1].z);
          vertex(x+bottomPent[0].x, y+bottomPent[0].y, z+bottomPent[0].z);
          endShape(CLOSE);
  
          beginShape();
          vertex(x+bottomPent[0].x, y+bottomPent[0].y, z+bottomPent[0].z);
          vertex(x+topPent[i].x, y+topPent[i].y, z+topPent[i].z);
          vertex(x+topPent[i+1].x, y+topPent[i+1].y, z+topPent[i+1].z);
          endShape(CLOSE);
        }
        else if (i==topPent.length-1){
          beginShape();
          vertex(x+topPent[i].x, y+topPent[i].y, z+topPent[i].z);
          vertex(x+bottomPent[0].x, y+bottomPent[0].y, z+bottomPent[0].z);
          vertex(x+bottomPent[1].x, y+bottomPent[1].y, z+bottomPent[1].z);
          endShape(CLOSE);
  
          beginShape();
          vertex(x+bottomPent[1].x, y+bottomPent[1].y, z+bottomPent[1].z);
          vertex(x+topPent[i].x, y+topPent[i].y, z+topPent[i].z);
          vertex(x+topPent[0].x, y+topPent[0].y, z+topPent[0].z);
          endShape(CLOSE);
        }
      }
    }
  
    // overrided methods fom Shape3D
    void rotZ(float theta){
      float tx=0, ty=0, tz=0;
      // top point
      tx = cos(theta)*topPoint.x+sin(theta)*topPoint.y;
      ty = sin(theta)*topPoint.x-cos(theta)*topPoint.y;
      topPoint.x = tx;
      topPoint.y = ty;
  
      // bottom point
      tx = cos(theta)*bottomPoint.x+sin(theta)*bottomPoint.y;
      ty = sin(theta)*bottomPoint.x-cos(theta)*bottomPoint.y;
      bottomPoint.x = tx;
      bottomPoint.y = ty;
  
      // top and bottom pentagons
      for (int i=0; i<topPent.length; i++){
        tx = cos(theta)*topPent[i].x+sin(theta)*topPent[i].y;
        ty = sin(theta)*topPent[i].x-cos(theta)*topPent[i].y;
        topPent[i].x = tx;
        topPent[i].y = ty;
  
        tx = cos(theta)*bottomPent[i].x+sin(theta)*bottomPent[i].y;
        ty = sin(theta)*bottomPent[i].x-cos(theta)*bottomPent[i].y;
        bottomPent[i].x = tx;
        bottomPent[i].y = ty;
      }
    }
  
    void rotX(float theta){
    }
  
    void rotY(float theta){
    }
  
  }
  
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.