home *** CD-ROM | disk | FTP | other *** search
/ gondwana.ecr.mu.oz.au/pub/ / Graphics.tar / Graphics / voglw.zip / arcs.c next >
C/C++ Source or Header  |  1997-02-13  |  7KB  |  400 lines

  1. #include "vogl.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(noseg)
  24.     int    noseg;
  25. {
  26.     nsegs = noseg;
  27. }
  28.  
  29. /*
  30.  * circleprecision
  31.  *
  32.  *    sets the number of segments in an arc or circle.
  33.  */
  34. void
  35. circleprecision(noseg)
  36.     int    noseg;
  37. {
  38.     nsegs = noseg;
  39. }
  40.  
  41. /*
  42.  * arc
  43.  *
  44.  * draw an arc at a given location.  Precision of arc (# line segments)
  45.  * is calculated from the value given to circleprecision.
  46.  *
  47.  */
  48. void
  49. arc(x, y, radius, sang, eang)
  50.     Coord    x, y, radius;
  51.     Angle    sang, eang;
  52. {
  53.     Token    *t;
  54.     float    cx, cy, dx, dy;
  55.     float    startang, endang, deltang, cosine, sine, angle;
  56.     int    i, numsegs, sync;
  57.  
  58.     if (!vdevice.initialised)
  59.         verror("arc: vogl not initialised");
  60.  
  61.  
  62.     startang = (float)sang / 10.0;
  63.     endang = (float)eang / 10.0;
  64.  
  65.     angle = startang * D2R;
  66.     numsegs = (endang - startang) / 360.0 * nsegs + 0.5;
  67.     deltang = (endang - startang) * D2R / numsegs;
  68.     cosine = cos((double)deltang);
  69.     sine = sin((double)deltang);
  70.  
  71.     if (vdevice.inobject) {
  72.         t = newtokens(8);
  73.         t[0].i = ARC;
  74.         t[1].f = x;
  75.         t[2].f = y;
  76.         t[3].f = radius * cos((double)angle);
  77.         t[4].f = radius * sin((double)angle);
  78.         t[5].f = cosine;
  79.         t[6].f = sine;
  80.         t[7].i = numsegs;
  81.         return;
  82.     }
  83.  
  84.     if (sync = vdevice.sync)
  85.         vdevice.sync = 0;
  86.  
  87.     /* calculates initial point on arc */
  88.  
  89.     cx = x + radius * cos((double)angle);
  90.     cy = y + radius * sin((double)angle);
  91.     move2(cx, cy);
  92.  
  93.     for (i = 0; i < numsegs; i++)  {
  94.         dx = cx - x; 
  95.         dy = cy - y;
  96.         cx = x + dx * cosine - dy * sine;
  97.         cy = y + dx * sine + dy * cosine;
  98.         draw2(cx, cy);
  99.     }
  100.  
  101.     if (sync) {
  102.         vdevice.sync = 1;
  103.         (*vdevice.dev.Vsync)();
  104.     }
  105. }
  106.  
  107. /*
  108.  * arcs
  109.  *
  110.  * draw an arc at a given location.  (Expressed as short integers) 
  111.  * Precision of arc (# line segments) is calculated from the value
  112.  * given to circleprecision.
  113.  *
  114.  */
  115. void
  116. arcs(x, y, radius, sang, eang)
  117.     Scoord    x, y, radius;
  118.     Angle    sang, eang;
  119. {
  120.     arc((Coord)x, (Coord)y, (Coord)radius, sang, eang);
  121. }
  122.  
  123. /*
  124.  * arci
  125.  *
  126.  * draw an arc at a given location.  (Expressed as integers) 
  127.  * Precision of arc (# line segments) is calculated from the value
  128.  * given to circleprecision.
  129.  *
  130.  */
  131. void
  132. arci(x, y, radius, sang, eang)
  133.     Icoord    x, y, radius;
  134.     Angle    sang, eang;
  135. {
  136.     arc((Coord)x, (Coord)y, (Coord)radius, sang, eang);
  137. }
  138.  
  139. /*
  140.  * arcf
  141.  *
  142.  *    draw a filled sector in a given location. The number of line
  143.  * segments in the arc of the segment is the same as in arc.
  144.  */
  145. void
  146. arcf(x, y, radius, sang, eang)
  147.     Coord    x, y, radius;
  148.     Angle    sang, eang;
  149. {
  150.     Token    *t;
  151.     float    cx, cy, dx, dy;
  152.     float    deltang, cosine, sine, angle;
  153.     int    i, numsegs;
  154.     float    startang, endang;
  155.  
  156.     if (!vdevice.initialised)
  157.         verror("arcf: vogl not initialised");
  158.  
  159.     startang = sang / 10.0;
  160.     endang = eang / 10.0;
  161.  
  162.     angle = startang * D2R;
  163.     numsegs = (endang - startang) / 360.0 * nsegs + 0.5;
  164.     deltang = (endang - startang) * D2R / numsegs;
  165.     cosine = cos((double)deltang);
  166.     sine = sin((double)deltang);
  167.  
  168.     if (vdevice.inobject) {
  169.         t = newtokens(8);
  170.         t[0].i = ARCF;
  171.         t[1].f = x;
  172.         t[2].f = y;
  173.         t[3].f = radius * cos((double)angle);
  174.         t[4].f = radius * sin((double)angle);
  175.         t[5].f = cosine;
  176.         t[6].f = sine;
  177.         t[7].i = numsegs;
  178.         return;
  179.     }
  180.  
  181.     pmv2(x, y);
  182.             /* calculates initial point on arc */
  183.  
  184.     cx = x + radius * cos((double)angle);
  185.     cy = y + radius * sin((double)angle);
  186.  
  187.     pdr2(cx, cy);
  188.  
  189.     for (i = 0; i < numsegs; i++)  {
  190.         dx = cx - x; 
  191.         dy = cy - y;
  192.         cx = x + dx * cosine - dy * sine;
  193.         cy = y + dx * sine + dy * cosine;
  194.         pdr2(cx, cy);
  195.     }
  196.  
  197.     pclos();
  198. }
  199.  
  200. /*
  201.  * arcfs
  202.  *
  203.  * draw a filled sector at a given location.  (Expressed as short integers) 
  204.  * Precision of arc (# line segments) is calculated from the value
  205.  * given to circleprecision.
  206.  *
  207.  */
  208. void
  209. arcfs(x, y, radius, sang, eang)
  210.     Scoord    x, y, radius;
  211.     Angle    sang, eang;
  212. {
  213.     arcf((Coord)x, (Coord)y, (Coord)radius, sang, eang);
  214. }
  215.  
  216. /*
  217.  * arcfi
  218.  *
  219.  * draw a filled sector at a given location.  (Expressed as integers) 
  220.  * Precision of arc (# line segments) is calculated from the value
  221.  * given to circleprecision.
  222.  *
  223.  */
  224. void
  225. arcfi(x, y, radius, sang, eang)
  226.     Icoord    x, y, radius;
  227.     Angle    sang, eang;
  228. {
  229.     arcf((Coord)x, (Coord)y, (Coord)radius, sang, eang);
  230. }
  231.  
  232.  
  233. /*
  234.  * circ
  235.  *
  236.  * Draw a circle of given radius at given world coordinates. The number of
  237.  * segments in the circle is the same as that of an arc.
  238.  *
  239.  */
  240. void
  241. circ(x, y, radius)
  242.     Coord    x, y, radius;
  243. {
  244.     Token    *t;
  245.     float    cx, cy, dx, dy;
  246.     float    angle, cosine, sine;
  247.     int    i, sync;
  248.  
  249.     if (!vdevice.initialised)
  250.         verror("circ: vogl not initialised");
  251.  
  252.     angle = 2.0 * PI / nsegs;
  253.     cosine = cos((double)angle);
  254.     sine = sin((double)angle);
  255.  
  256.     if (vdevice.inobject) {
  257.         t = newtokens(7);
  258.         t[0].i = CIRCLE;
  259.         t[1].f = x;
  260.         t[2].f = y;
  261.         t[3].f = radius;
  262.         t[4].f = cosine;
  263.         t[5].f = sine;
  264.         t[6].i = nsegs;
  265.         return;
  266.     }
  267.  
  268.     cx = x + radius;
  269.     cy = y;
  270.  
  271.     if (sync = vdevice.sync)
  272.         vdevice.sync = 0;
  273.  
  274.     move2(cx, cy);
  275.     for (i = 0; i < nsegs; i++) {
  276.         dx = cx - x; 
  277.         dy = cy - y;
  278.         cx = x + dx * cosine - dy * sine;
  279.         cy = y + dx * sine + dy * cosine;
  280.         draw2(cx, cy);
  281.     }
  282.  
  283.     if (sync) {
  284.         vdevice.sync = 1;
  285.         draw2(x + radius, y);
  286.     }
  287. }
  288.  
  289. /*
  290.  * circs
  291.  *
  292.  * Draw a circle of given radius at given world coordinates expressed as
  293.  * short integers. The number of segments in the circle is the same as that
  294.  * of an arc.
  295.  *
  296.  */
  297. void
  298. circs(x, y, radius)
  299.     Scoord    x, y, radius;
  300. {
  301.     circ((Coord)x, (Coord)y, (Coord)radius);
  302. }
  303.  
  304.  
  305. /*
  306.  * circi
  307.  *
  308.  * Draw a circle of given radius at given world coordinates expressed as
  309.  * integers. The number of segments in the circle is the same as that
  310.  * of an arc.
  311.  *
  312.  */
  313. void
  314. circi(x, y, radius)
  315.     Icoord    x, y, radius;
  316. {
  317.     circ((Coord)x, (Coord)y, (Coord)radius);
  318. }
  319.  
  320. /*
  321.  * circf
  322.  *
  323.  * Draw a filled circle of given radius at given world coordinates.
  324.  * The number of segments in the circle is the same as that of an arc.
  325.  *
  326.  */
  327. void
  328. circf(x, y, radius)
  329.     Coord    x, y, radius;
  330. {
  331.     Token    *t;
  332.     float    cx, cy, dx, dy;
  333.     float    angle, cosine, sine;
  334.     int    i;
  335.  
  336.     if (!vdevice.initialised)
  337.         verror("circf: vogl not initialised");
  338.  
  339.     angle = 2.0 * PI / nsegs;
  340.     cosine = cos((double)angle);
  341.     sine = sin((double)angle);
  342.  
  343.     if (vdevice.inobject) {
  344.         t = newtokens(7);
  345.         t[0].i = CIRCF;
  346.         t[1].f = x;
  347.         t[2].f = y;
  348.         t[3].f = radius;
  349.         t[4].f = cosine;
  350.         t[5].f = sine;
  351.         t[6].i = nsegs;
  352.         return;
  353.     }
  354.  
  355.     cx = x + radius;
  356.     cy = y;
  357.  
  358.     pmv2(cx, cy);
  359.     for (i = 0; i < nsegs; i++) {
  360.         dx = cx - x; 
  361.         dy = cy - y;
  362.         cx = x + dx * cosine - dy * sine;
  363.         cy = y + dx * sine + dy * cosine;
  364.         pdr2(cx, cy);
  365.     }
  366.  
  367.     pclos();
  368. }
  369.  
  370. /*
  371.  * circfs
  372.  *
  373.  * Draw a circle of given radius at given world coordinates expressed as
  374.  * short integers. The number of segments in the circle is the same as that
  375.  * of an arc.
  376.  *
  377.  */
  378. void
  379. circfs(x, y, radius)
  380.     Scoord    x, y, radius;
  381. {
  382.     circf((Coord)x, (Coord)y, (Coord)radius);
  383. }
  384.  
  385. /*
  386.  * circfi
  387.  *
  388.  * Draw a circle of given radius at given world coordinates expressed as
  389.  * integers. The number of segments in the circle is the same as that
  390.  * of an arc.
  391.  *
  392.  */
  393. void
  394. circfi(x, y, radius)
  395.     Icoord    x, y, radius;
  396. {
  397.     circf((Coord)x, (Coord)y, (Coord)radius);
  398. }
  399.  
  400.