home *** CD-ROM | disk | FTP | other *** search
/ gondwana.ecr.mu.oz.au/pub/ / Graphics.tar / Graphics / prev.tar.gz / prev.tar / cylinder.c < prev    next >
C/C++ Source or Header  |  1991-03-11  |  3KB  |  179 lines

  1. #include <math.h>
  2. #include <stdio.h>
  3. #include <vogle.h>
  4. #include "art.h"
  5. #include "macro.h"
  6. #include "gram.h"
  7.  
  8.  
  9. extern mats    *mstackp;
  10. extern hlist    *fhlist;
  11. extern float    tolerance;
  12. extern int    lookatdone;
  13. extern int    longlines;
  14.  
  15. /*
  16.  * cylinit
  17.  *
  18.  *    initialise the function pointers and fields of a cylinder object
  19.  */
  20. cylinit(o, d)
  21.     object    *o;
  22.     details *d;
  23. {
  24.     int    first;
  25.     vector    c1, c2, rad;
  26.     details    *ld;
  27.     float    apex_x, apex_y, apex_z, apex_r;
  28.     float    base_x, base_y, base_z, base_r;
  29.     float    len0, len1,  len_x, len_y, len_z;
  30.     float    cosine, sine, dx1, dy1, dx2, dy2, delta;
  31.     float    cx1, cy1, cx2, cy2;
  32.     int    i;
  33.     Matrix    m;
  34.  
  35.     if (!lookatdone)
  36.         deflookat();
  37.  
  38.     first = 1;
  39.  
  40.     rad.x = rad.y = 1.0;
  41.  
  42.     while (d != (details *)NULL) {
  43.         switch (d->type) {
  44.         case CENTER:
  45.             if (first) {
  46.                 c1 = d->u.v;
  47.                 first = 0;
  48.             } else {
  49.                 c2 = d->u.v;
  50.             }
  51.             break;
  52.         case RADIUS:
  53.             rad.x = rad.y = d->u.f;
  54.             break;
  55.         case RADII:
  56.             rad.x = d->u.v.x;
  57.             rad.y = d->u.v.y;
  58.             break;
  59.         default:
  60.             warning("art: illegal field in cylinder ignored.\n");
  61.         }
  62.         ld = d;
  63.         d = d->nxt;
  64.         free(ld);
  65.     }
  66.  
  67.  
  68.     len_x = c1.x - c2.x;
  69.     len_y = c1.y - c2.y;
  70.     len_z = c1.z - c2.z;
  71.  
  72.     len0 = sqrt(len_x * len_x + len_y * len_y + len_z * len_z);
  73.  
  74.     len1 = sqrt(len_x * len_x + len_z * len_z);
  75.  
  76.     pushmatrix();
  77.  
  78.     calctransforms(mstackp);
  79.     multmatrix(mstackp->obj2ray);
  80.  
  81.     /*
  82.      * Draw the axis center line in YELLOW
  83.      */
  84.     color(YELLOW);
  85.     move(c1.x, c1.y, c1.z);
  86.     draw(c2.x, c2.y, c2.z);
  87.  
  88.     /*
  89.      * And the rest in GREEN...
  90.      */
  91.     color(GREEN);
  92.  
  93.     pushmatrix();
  94.  
  95.         translate(c2.x, c2.y, c2.z);
  96.  
  97.         identmatrix(m);
  98.  
  99.         if (len1 == 0.0) {
  100.             cosine = 0.0;
  101.             sine = 1.0;
  102.         } else {
  103.             cosine = len_z / len1;
  104.             sine = len_x / len1;
  105.         }
  106.  
  107.         /* rotate about y */
  108.         m[0][0] = cosine;
  109.         m[0][2] = -sine;
  110.         m[2][0] = sine;
  111.         m[2][2] = cosine;
  112.         multmatrix(m);
  113.  
  114.         identmatrix(m);
  115.  
  116.         if (len0 == 0.0) {
  117.             cosine = 0.0;
  118.             sine = 1.0;
  119.         } else {
  120.             cosine = len1 / len0;
  121.             sine = -len_y / len0;
  122.         }
  123.  
  124.         /* rotate about x */
  125.         m[1][1] = cosine;
  126.         m[1][2] = sine;
  127.         m[2][1] = -sine;
  128.         m[2][2] = cosine;
  129.         multmatrix(m);
  130.  
  131.         /*
  132.          * Draw the end circles...
  133.          */
  134.         pushmatrix();
  135.             scale(rad.x, rad.y, 1.0);
  136.  
  137.             circle (0.0, 0.0, 1.0);
  138.  
  139.             pushmatrix();
  140.                 translate(0.0, 0.0, len0);
  141.                 circle (0.0, 0.0, 1.0);
  142.             popmatrix();
  143.         popmatrix();
  144.  
  145.  
  146.  
  147.         /*
  148.          * Draw the logitudinal lines...
  149.          */
  150.         delta = 2 * PI / longlines;
  151.  
  152.         cosine = cos(delta);
  153.         sine = sin(delta);
  154.  
  155.         cx1 = rad.x;
  156.         cy1 = 0.0;
  157.         cx2 = rad.x;
  158.         cy2 = 0.0;
  159.  
  160.         move(cx1, cy1, 0.0);
  161.         draw(cx2, cy2, len0);
  162.  
  163.         for (i = 0; i < longlines; i++) {
  164.             dx1 = cx1;
  165.             dy1 = cy1;
  166.             cx1 = dx1 * cosine - dy1 * sine;
  167.             cy1 = dx1 * sine + dy1 * cosine;
  168.             dx2 = cx2;
  169.             dy2 = cy2;
  170.             cx2 = dx2 * cosine - dy2 * sine;
  171.             cy2 = dx2 * sine + dy2 * cosine;
  172.             move(cx1, cy1, 0.0);
  173.             draw(cx2, cy2, len0);
  174.         }
  175.     popmatrix();
  176.  
  177.     popmatrix();
  178. }
  179.