home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / graphics-0.17 / plot2ps / arc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-11  |  3.2 KB  |  135 lines

  1. /* This file is the arc routine, which is a standard part of the plot
  2.    library.  It draws an arc with the center at xc,yc, the beginning at
  3.    x0,y0 and the ending at x1,y1 */
  4.  
  5. #include "sys-defines.h"
  6. #include "libplot.h"
  7.  
  8. #ifdef NEED_DREM
  9. /* return the remainder of x/y */
  10. double drem (x, y)
  11.      double x, y;
  12. {
  13.   while (x>y/2.)
  14.     x -= y;
  15.   while (x<-y/2.)
  16.     x += y;
  17.   return x;
  18. }
  19. #endif
  20.  
  21. int
  22. arc (xc, yc, x0, y0, x1, y1)
  23.      int xc, yc, x0, y0, x1, y1;
  24. {
  25.   /* center: xc,yc
  26.      from: x0,y0
  27.      to: x1,y1 */
  28.   double dtheta, theta, theta_0, theta_1, cos_theta, sin_theta, x, y, radius;
  29.  
  30.   x = x0 - xc;            /* starting coordinate relatve to center */
  31.   y = y0 - yc;            /* of the circle. */
  32.   radius = sqrt (x*x + y*y);
  33.  
  34.   if (radius == 0.)        /* if the radius is zero, we draw a point. */
  35.     {
  36.       point (xc, yc);
  37.       return 0;
  38.     }
  39.  
  40.   if (x != 0)            /* find the starting angle */
  41.     {
  42.       theta_0 = atan2 (y, x);
  43.     }
  44.   else
  45.     {
  46.       if (y>0)
  47.     theta_0 = M_PI/2.;
  48.       else
  49.     theta_0 = M_PI/-2.;
  50.     }
  51.      
  52.       
  53.   x = x1 - xc;            /* ending coordinate relatve to center */
  54.   y = y1 - yc;            /* of the circle. */
  55.  
  56.   if (x != 0)            /* find the ending angle */
  57.     {
  58.       theta_1 = atan2 (y, x);
  59.     }
  60.   else
  61.     {
  62.       if (y>0)
  63.     theta_1 = M_PI/2.;
  64.       else
  65.     theta_1 = M_PI/-2.;
  66.     }
  67.   if (theta_1 <= theta_0)
  68.     theta_1 += 2. * M_PI;
  69.  
  70.   move (x0, y0);
  71.  
  72.   /*  we make dtheta proportional to the length of the arc so that
  73.       small arcs will look smooth. */
  74.   dtheta = (theta_1 - theta_0) /64.;
  75.  
  76.   for (theta = theta_0 + dtheta; theta < theta_1; theta += dtheta)
  77.     {
  78.       double xx, yy, xinc, yinc, distance_a, distance_b;
  79.       xx = radius*cos(theta);
  80.       yy = radius*sin(theta);
  81.  
  82.       /* choose the nearest pixel. */
  83.       if ( drem (theta, M_PI/2.) < M_PI/-4.)
  84.     {
  85.       xinc = 1.;
  86.       yinc = 0.;
  87.     }
  88.       else
  89.     {
  90.       xinc = 0.;
  91.       yinc = 1.;
  92.     }
  93.       distance_a = (int)(xx) * (int)(xx)
  94.     + (int)(yy) * (int)(yy) - radius * radius;
  95.       distance_b = (int)(xx+xinc) * (int)(xx+xinc)
  96.     + (int)(yy+yinc) * (int)(yy+yinc) - radius * radius;
  97.       if (distance_b < distance_a)
  98.     {
  99.       xx += xinc;
  100.       yy += yinc;
  101.     }
  102.       else
  103.     {
  104.       distance_b = (int)(xx-xinc) * (int)(xx-xinc)
  105.         + (int)(yy-yinc) * (int)(yy-yinc) - radius * radius;
  106.       if (distance_b < distance_a)
  107.         {
  108.           xx -= xinc;
  109.           yy -= yinc;
  110.         }
  111.     }
  112.       cont ((int) (xc + xx), (int) (yc + yy));
  113.     }
  114.   cont (x1, y1);
  115.   move (xc, yc);
  116.  
  117.   return 0;
  118. }
  119.  
  120. /* libtek, a library of functions for tektronics 4010 compatible devices.
  121.    Copyright (C) 1989 Free Software Foundation, Inc.
  122.  
  123. libtek is distributed in the hope that it will be useful, but WITHOUT ANY
  124. WARRANTY.  No author or distributor accepts responsibility to anyone for the
  125. consequences of using it or for whether it serves any particular purpose or
  126. works at all, unless he says so in writing.  Refer to the GNU General Public
  127. License for full details.
  128.  
  129. Everyone is granted permission to copy, modify and redistribute libtek, but
  130. only under the conditions described in the GNU General Public License.  A copy
  131. of this license is supposed to have been given to you along with libtek so
  132. you can know your rights and responsibilities.  It should be in a file named
  133. COPYING.  Among other things, the copyright notice and this notice must be
  134. preserved on all copies.  */
  135.