home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 3 / PDCD_3.iso / utilities / utilsm / mkdrawf / Goodies / c / dodec next >
Encoding:
Text File  |  1994-12-15  |  2.9 KB  |  126 lines

  1. /* make an dodecahedron
  2.  *
  3.  * The Clever Fact is: you can make a dodecahedron by starting with a cube
  4.  * and putting a "tent" on top of each face. The tent needs to have "height"
  5.  * (root5-1)/2, and "offset" the square of that.
  6.  */
  7.  
  8. #include <math.h>
  9. #include <stdio.h>
  10.  
  11. #define HEIGHT    0.61803398875
  12. #define OFFSET    0.38196601125    /* HEIGHT^2 */
  13. #define BIG    (1+HEIGHT)
  14. #define SMALL    (1-OFFSET)
  15.  
  16. #define FACES 12
  17.  
  18. #undef LABEL
  19.  
  20. typedef double point[3];
  21.  
  22. point v[20] = {
  23.     /* vertices of cube */
  24.   { -1,-1,-1 },    /* 0 */
  25.   { -1,-1, 1 },    /* 1 */
  26.   { -1, 1,-1 },    /* 2 */
  27.   { -1, 1, 1 },    /* 3 */
  28.   {  1,-1,-1 },    /* 4 */
  29.   {  1,-1, 1 },    /* 5 */
  30.   {  1, 1,-1 },    /* 6 */
  31.   {  1, 1, 1 },    /* 7 */
  32.   { -SMALL, 0, -BIG },    /* 8 */
  33.   {  SMALL, 0, -BIG },    /* 9 */
  34.   { -SMALL, 0,  BIG },    /* 10 */
  35.   {  SMALL, 0,  BIG },    /* 11 */
  36.   { 0, -BIG, -SMALL },    /* 12 */
  37.   { 0, -BIG,  SMALL },    /* 13 */
  38.   { 0,  BIG, -SMALL },    /* 14 */
  39.   { 0,  BIG,  SMALL },    /* 15 */
  40.   { -BIG, -SMALL, 0 },    /* 16 */
  41.   { -BIG,  SMALL, 0 },    /* 17 */
  42.   {  BIG, -SMALL, 0 },    /* 18 */
  43.   {  BIG,  SMALL, 0 },    /* 19 */
  44. };
  45.  
  46. /* this is an arbitrary order */
  47. int faces[FACES][5] = {
  48.   {0,12,13,1,16},
  49.   {4,12,13,5,18},
  50.   {0,16,17,2,8},
  51.   {6,9,4,18,19},
  52.   {0,8,9,4,12},
  53.   {2,14,6,9,8},
  54.   {1,13,5,11,10},
  55.   {3,10,11,7,15},
  56.   {1,10,3,17,16},
  57.   {5,18,19,7,11},
  58.   {2,17,3,15,14},
  59.   {6,14,15,7,19}
  60. };
  61.  
  62. /* output vertex v[n], converted to 2d */
  63. void wrvx(int n) {
  64.   printf("%.4lg %.4lg",400*v[n][0]/(v[n][2]+5)+200,400*v[n][1]/(v[n][2]+5)+200);
  65. }
  66.  
  67. /* output points for face faces[n] */
  68. void facepath(int n) {
  69.   int i;
  70.   printf("  Move "); wrvx(faces[n][0]);
  71.   for (i=1;i<5;++i) {
  72.     printf("\n  Line "); wrvx(faces[n][i]); }
  73.   printf("\n  Close\n");
  74. }
  75.  
  76. /* is face n visible from 0,0,-5 ? */
  77. int visible(int n) {
  78.   double x=0,y=0,z=0; int i;
  79.   for (i=0;i<5;++i) {
  80.     x+=v[faces[n][i]][0];
  81.     y+=v[faces[n][i]][1];
  82.     z+=v[faces[n][i]][2];
  83.   }
  84.   x/=5; y/=5; z/=5;
  85.   return (x*x+y*y+(z+5)*z<0);
  86. }
  87.  
  88. int main(int ac, char *av[]) {
  89.   int i;
  90.   double ax,ay,az;
  91.   if (ac!=4) {
  92.     fprintf(stderr,"Usage: dodec angleX angleY angleZ\n");
  93.     return 0;
  94.   }
  95.   sscanf(av[1],"%lf",&ax); ax*=3.141592653589793/180;
  96.   sscanf(av[2],"%lf",&ay); ay*=3.141592653589793/180;
  97.   sscanf(av[3],"%lf",&az); az*=3.141592653589793/180;
  98.   { double cx=cos(ax),sx=sin(ax),cy=cos(ay),sy=sin(ay),cz=cos(az),sz=sin(az);
  99.     double t,x,y,z;
  100.     for (i=0;i<20;++i) {
  101.       x=v[i][0]; y=v[i][1]; z=v[i][2];
  102.       t=x; x=x*cz-y*sz; y=t*sz+y*cz;
  103.       t=x; x=x*cy-z*sy; z=t*sy+z*cy;
  104.       t=y; y=y*cx-z*sx; z=t*sx+z*cx;
  105.       v[i][0]=x; v[i][1]=y; v[i][2]=z;
  106.     }
  107.   }
  108. #ifdef LABEL
  109.   printf("FontTable {\n  1 Trinity.Medium\n}\n");
  110.   for (i=0;i<20;++i) {
  111.     printf("\nText {\n  Style 1\n  Size 12 12\n  StartAt "); wrvx(i);
  112.     printf("\n  Text %d\n}\n",i);
  113.   }
  114. #endif
  115.   printf("\nPath {\n");
  116.   for (i=0;i<FACES;++i) {
  117.     if (visible(i)) facepath(i);
  118.   }
  119.   printf("}\n\nPath {\n  Style { Dash { 1 4 } }\n");
  120.   for (i=0;i<FACES;++i){
  121.     if (!visible(i)) facepath(i);
  122.   }
  123.   printf("}\n");
  124.   return 0;
  125. }
  126.