2009年8月31日 星期一

正多面體座標產生器



public class 正多面體 {
  private static double r = 50;
  
  public static void main(String argv[]) {
    正四面體();
    正六面體();
    正八面體();
    正十二面體();
    正二十面體();
  }

  public static void printPoints(String s, Point3D[] v) {
    System.out.println(s);
    for(int i = 0; i < v.length; ++i) {
      System.out.println("p" + (i+1) + " " + v[i].x + " " + v[i].y + " " + v[i].z);
    }
  }
  
  public static void printLines(String s, int[][] o) {
    System.out.println(s);
    if(o == null) return;
    int c = 0;
    for(int i = 0; i < o.length; ++i) {
      for(int j = 0; j < o[i].length - 1; ++j) {
        System.out.println("l" + (c+1) + " p" + (o[i][j]+1) + " p" + (o[i][j+1]+1));
        c++;
      }
      System.out.println("l" + (c+1) + " p" + (o[i][o[i].length-1]+1) + " p" + (o[i][0]+1));
      c++;
    }
  }
  
  private static Point3D minus(Point3D p) {
    // TODO Auto-generated method stub
    return new Point3D(-p.x, -p.y, -p.z);
  }
  
  private static void 正四面體() {
    // TODO Auto-generated method stub
    double sq2 = Math.sqrt(2.0), sq3 = Math.sqrt(3.0); 
    Point3D[] Vt = new Point3D[4];
    Vt[0] = new Point3D(0, r, 0); 
    Vt[1] = new Point3D(0, -r/3, r*2*sq2/3); 
    Vt[2] = new Point3D(r*sq2/sq3, -r/3, -r*sq2/3); 
    Vt[3] = new Point3D(-r*sq2/sq3, -r/3, -r*sq2/3); 
     
    int[][] ord = {{0, 1, 2}, {0, 2, 3}, {0, 3, 1}, {1, 3, 2}};
    printPoints("正四面體", Vt);
    printLines("正四面體", ord);
  }

  
  
  private static void 正六面體() {
    // TODO Auto-generated method stub
    double sq2 = Math.sqrt(2.0), sq3 = Math.sqrt(3.0); 
    Point3D[] Vt = new Point3D[8];
    Vt[0] = new Point3D(0, r, 0); 
    Vt[1] = new Point3D(0, r/3, r*2*sq2/3); 
    Vt[2] = new Point3D(r*sq2/sq3, r/3, -r*sq2/3); 
    Vt[3] = new Point3D(-r*sq2/sq3, r/3, -r*sq2/3); 
     
    for(int i = 4; i < Vt.length; i++) 
        Vt[i] = minus(Vt[i-4]); 

    int[][] ord = {{0, 1, 7, 2}, {0, 2, 5, 3}, {0, 3, 6, 1}, 
                   {4, 6, 3, 5}, {4, 7, 1, 6}, {4, 5, 2, 7}}; 

    printPoints("正六面體", Vt);
    printLines("正六面體", ord);
  }

  private static void 正八面體() {
    // TODO Auto-generated method stub
    Point3D[] Vt = new Point3D[6];
    Vt[0] = new Point3D(0, r, 0); 
    Vt[1] = new Point3D(0, 0, r); 
    Vt[2] = new Point3D(r, 0, 0); 
     
    for(int i = 3; i < Vt.length; i++) 
        Vt[i] = minus(Vt[i-3]); 
     
    int[][] ord = {{0, 1, 2}, {0, 2, 4}, {0, 4, 5}, {0, 5, 1}, 
                   {3, 5, 4}, {3, 1, 5}, {3, 2, 1}, {3, 4, 2}};
    printPoints("正八面體", Vt);
    printLines("正八面體", ord);

  }

  private static void 正十二面體() {
    // TODO Auto-generated method stub
    double sq3=Math.sqrt(3.0),sq5=Math.sqrt(5.0);
    double t1 = (sq5+1)/2, t2 = (sq5-1)/2; 

    Point3D[] Vt = new Point3D[20];
    Vt[0] = new Point3D(0,r,0); 
    Vt[1] = new Point3D(0,r*sq5/3,r*2/3); 
    Vt[2] = new Point3D(r*sq3/3,r*sq5/3,-r/3); 
    Vt[3] = new Point3D(-r*sq3/3,r*sq5/3,-r/3); 
    Vt[4] = new Point3D(r*sq3/3,r/3,r*sq5/3); 
    Vt[5] = new Point3D(r*t1*sq3/3,r/3,r*t2*t2/3); 
    Vt[6] = new Point3D(r*t2*sq3/3,r/3,-r*t1*t1/3); 
    Vt[7] = new Point3D(-r*t2*sq3/3,r/3,-r*t1*t1/3); 
    Vt[8] = new Point3D(-r*t1*sq3/3,r/3,r*t2*t2/3); 
    Vt[9] = new Point3D(-r*sq3/3,r/3,r*sq5/3); 
    for(int i = 10; i < Vt.length; i++) 
        Vt[i] = minus(Vt[i-10]);
    
    
    int[][]ord = 
            {{ 0, 1, 4, 5, 2},{ 0, 2, 6, 7, 3},{ 0, 3, 8, 9, 1}, 
            { 1, 9,16,17, 4},{ 2, 5,18,19, 6},{ 3, 7,14,15, 8}, 
            {10,12,15,14,11},{10,13,17,16,12},{10,11,19,18,13}, 
            {11,14, 7, 6,19},{12,16, 9, 8,15},{13,18, 5, 4,17}};
    printPoints("正十二面體", Vt);
    printLines("正十二面體", ord);

  }

  private static void 正二十面體() {
    // TODO Auto-generated method stub
     double sq5=Math.sqrt(5.0); 
     double t1 = (sq5+1)/2, t2 = (sq5-1)/2; 
     Point3D[] Vt = new Point3D[12];
     Vt[0] = new Point3D(0,r,0); 
     Vt[1] = new Point3D(0,r/sq5,r*2/sq5); 
     Vt[2] = new Point3D(r*Math.sqrt(t1/sq5),r/sq5,r*t2/sq5); 
     Vt[3] = new Point3D(r*Math.sqrt(t2/sq5),r/sq5,-r*t1/sq5); 
     Vt[4] = new Point3D(-r*Math.sqrt(t2/sq5),r/sq5,-r*t1/sq5); 
     Vt[5] = new Point3D(-r*Math.sqrt(t1/sq5),r/sq5,r*t2/sq5); 
     for(int i = 6; i < Vt.length; i++) 
        Vt[i] = minus(Vt[i-6]); 
     
     int[][]ord = 
              {{0, 1,2},{0, 2,3},{0, 3,4},{ 0, 4, 5},{ 0,5, 1}, 
               {1,10,2},{2,11,3},{3, 7,4},{ 4, 8, 5},{ 5,9, 1}, 
               {6, 8,7},{6, 9,8},{6,10,9},{ 6,11,10},{ 6,7,11}, 
               {7, 8,4},{8, 9,5},{9,10,1},{10,11, 2},{11,7, 3}};
     printPoints("正二十面體", Vt);
     printLines("正二十面體", ord);

  }
  
  static class Point3D {
    public double x, y, z;
    public Point3D() {
      this.x = 0;
      this.y = 0;
      this.z = 0;
    }
    public Point3D(double x, double y, double z) {
      this.x = x;
      this.y = y;
      this.z = z;
    }
  }
}

沒有留言: