home *** CD-ROM | disk | FTP | other *** search
/ gondwana.ecr.mu.oz.au/pub/ / Graphics.tar / Graphics / fermiVogle.tar.Z / fermiVogle.tar / devel / src / arcs.c next >
C/C++ Source or Header  |  1996-02-07  |  4KB  |  232 lines

  1. #include "vogle.h"
  2.  
  3. #ifdef    TC
  4.  
  5. extern    double    cos();
  6. extern    double    sin();
  7.  
  8. #else 
  9.  
  10. #include <math.h>
  11.  
  12. #endif
  13.  
  14. static int    nsegs = 32;
  15.  
  16. /*
  17.  * arcprecision
  18.  *
  19.  *    sets the number of segments in an arc or circle.
  20.  *    - obsolete function.
  21.  */
  22. void
  23. arcprecision(int noseg)
  24. {
  25.     nsegs = noseg;
  26. }
  27.  
  28. /*
  29.  * circleprecision
  30.  *
  31.  *    sets the number of segments in an arc or circle.
  32.  */
  33. void
  34. circleprecision(int noseg)
  35. {
  36.     nsegs = noseg;
  37. }
  38.  
  39. /*
  40.  * arc
  41.  *
  42.  * draw an arc at a given location.  Precision of arc (# line segments)
  43.  * is calculated from the value given to circleprecision.
  44.  *
  45.  */
  46. void
  47. arc(float x, float y, float radius, float startang, float endang)
  48. {
  49.     Token    *t;
  50.     float    cx, cy, dx, dy;
  51.     float    deltang, cosine, sine, angle;
  52.     int    sync, i, numsegs;
  53.  
  54.     if (!vdevice.initialised)
  55.         verror("arc: vogle not initialised");
  56.  
  57.     if ((sync = vdevice.sync))
  58.         vdevice.sync = 0;
  59.  
  60.     angle = startang * D2R;
  61.     numsegs = (endang - startang) / 360.0 * nsegs + 0.5;
  62.     deltang = (endang - startang) * D2R / numsegs;
  63.     cosine = cos((double)deltang);
  64.     sine = sin((double)deltang);
  65.  
  66.     if (vdevice.inobject) {
  67.         t = newtokens(8);
  68.         t[0].i = ARC;
  69.         t[1].f = x;
  70.         t[2].f = y;
  71.         t[3].f = radius * cos((double)angle);
  72.         t[4].f = radius * sin((double)angle);
  73.         t[5].f = cosine;
  74.         t[6].f = sine;
  75.         t[7].i = numsegs;
  76.         return;
  77.     }
  78.  
  79.     /* calculates initial point on arc */
  80.  
  81.     cx = x + radius * cos((double)angle);
  82.     cy = y + radius * sin((double)angle);
  83.     move2(cx, cy);
  84.  
  85.     for (i = 0; i < numsegs; i++)  {
  86.         dx = cx - x; 
  87.         dy = cy - y;
  88.         cx = x + dx * cosine - dy * sine;
  89.         cy = y + dx * sine + dy * cosine;
  90.         draw2(cx, cy);
  91.     }
  92.  
  93.     if (sync) {
  94.         vdevice.sync = 1;
  95.         (*vdevice.dev.Vsync)();
  96.     }
  97. }
  98.  
  99. /*
  100.  * sector
  101.  *
  102.  *    draw a sector in a given location. The number of line segments in the
  103.  * arc of the segment is the same as in arc.
  104.  */
  105. void
  106. sector(float x, float y, float radius, float startang, float endang)
  107. {
  108.     Token    *t;
  109.     float    cx, cy, dx, dy;
  110.     float    deltang, cosine, sine, angle;
  111.     int    sync, i, numsegs, inpoly;
  112.  
  113.     if (!vdevice.initialised)
  114.         verror("segment: vogle not initialised");
  115.  
  116.     angle = startang * D2R;
  117.     numsegs = (endang - startang) / 360.0 * nsegs + 0.5;
  118.     deltang = (endang - startang) * D2R / numsegs;
  119.     cosine = cos((double)deltang);
  120.     sine = sin((double)deltang);
  121.  
  122.     if (vdevice.inobject) {
  123.         t = newtokens(8);
  124.         t[0].i = SECTOR;
  125.         t[1].f = x;
  126.         t[2].f = y;
  127.         t[3].f = radius * cos((double)angle);
  128.         t[4].f = radius * sin((double)angle);
  129.         t[5].f = cosine;
  130.         t[6].f = sine;
  131.         t[7].i = numsegs;
  132.         return;
  133.     }
  134.  
  135.     if ((sync = vdevice.sync))
  136.         vdevice.sync = 0;
  137.  
  138.     inpoly = vdevice.inpolygon;
  139.  
  140.     if ((vdevice.attr->a.fill || vdevice.attr->a.hatch) && !inpoly)
  141.         makepoly();        /* want it filled */
  142.  
  143.     move2(x, y);
  144.             /* calculates initial point on arc */
  145.  
  146.     cx = x + radius * cos((double)angle);
  147.     cy = y + radius * sin((double)angle);
  148.  
  149.     draw2(cx, cy);
  150.  
  151.     for (i = 0; i < numsegs; i++)  {
  152.         dx = cx - x; 
  153.         dy = cy - y;
  154.         cx = x + dx * cosine - dy * sine;
  155.         cy = y + dx * sine + dy * cosine;
  156.         draw2(cx, cy);
  157.     }
  158.  
  159.     if ((vdevice.attr->a.fill || vdevice.attr->a.hatch) && !inpoly)
  160.         closepoly();
  161.     else {
  162.         if (sync)
  163.             vdevice.sync = 1;
  164.  
  165.         draw2(x, y);
  166.     }
  167. }
  168.  
  169. /*
  170.  * circle
  171.  *
  172.  * Draw a circle of given radius at given world coordinates. The number of
  173.  * segments in the circle is the same as that of an arc.
  174.  *
  175.  */
  176. void
  177. circle(float x, float y, float radius)
  178. {
  179.     Token    *t;
  180.     float    cx, cy, dx, dy;
  181.     float    angle, cosine, sine;
  182.     int    sync, i, inpoly;
  183.  
  184.     if (!vdevice.initialised)
  185.         verror("circle: vogle not initialised");
  186.  
  187.     angle = 2.0 * PI / nsegs;
  188.     cosine = cos((double)angle);
  189.     sine = sin((double)angle);
  190.  
  191.     if (vdevice.inobject) {
  192.         t = newtokens(7);
  193.         t[0].i = CIRCLE;
  194.         t[1].f = x;
  195.         t[2].f = y;
  196.         t[3].f = radius;
  197.         t[4].f = cosine;
  198.         t[5].f = sine;
  199.         t[6].i = nsegs;
  200.         return;
  201.     }
  202.  
  203.     if ((sync = vdevice.sync))
  204.         vdevice.sync = 0;
  205.  
  206.     cx = x + radius;
  207.     cy = y;
  208.  
  209.     inpoly = vdevice.inpolygon;
  210.  
  211.     if ((vdevice.attr->a.fill || vdevice.attr->a.hatch) && !inpoly)
  212.         makepoly();        /* want it filled */
  213.  
  214.     move2(cx, cy);
  215.     for (i = 0; i < nsegs - 1; i++) {
  216.         dx = cx - x; 
  217.         dy = cy - y;
  218.         cx = x + dx * cosine - dy * sine;
  219.         cy = y + dx * sine + dy * cosine;
  220.         draw2(cx, cy);
  221.     }
  222.  
  223.     if ((vdevice.attr->a.fill || vdevice.attr->a.hatch) && !inpoly)
  224.         closepoly();
  225.     else {
  226.         if (sync)
  227.             vdevice.sync = 1;
  228.  
  229.         draw2(x + radius, y);
  230.     }
  231. }
  232.