[comp.graphics] Arc algorithm needed

TEITTINEN@cc.helsinki.fi (09/05/89)

I need a fast algorithm to draw arcs. The given values are centerpoint
(x,y) of the circle/ellipse, x- and y-radius, starting angle and ending
angle. C function preferred, but any language or plain algorithm
appreciated.

Thanks in advance
-- 
EARN: teittinen@finuh                        ! "Studying is the only way
Internet: teittinen@cc.helsinki.fi           !  to do nothing without
Marko Teittinen, student of computer science !  anyone blaming you" -me

mccaugh@s.cs.uiuc.edu (09/10/89)

#include <math.h>

 void DrawArc(a,b, x0,y0, theta1,theta2) /* draw arc from theta1 to theta2 */
        float a,b, x0,y0, theta1,theta2;     /* with all angles in radians */
                           /* a = x-radius, b = y-radius, (x0,y0) = center */
 {
#define  Degree  0.0174532

  float  a2,b2, ab, theta,tan_theta, x,y, Pi_2, Pi_3_2;

 a2 = a*a;   b2 = b*b;   ab = a*b;   Pi_2 = 0.5*Pi();   Pi_3_2 = 3.0*Pi_2;
 for (theta=theta1; theta<=theta2; theta+=Degree) /* increment = 1 degree */
     {
      if(theta==Pi_2)
        {
         x = x0;
         y = y0+b;
        }
      else if(theta==Pi_3_2)
             {
              x = x0;
              y = y0-b;
             }
           else {
                 tan_theta = tan(theta);
                 x  =  x0 + ab/sqrt(b2 + a2*tan_theta*tan_theta);
                 y  =  y0 + (x-x0)*tan_theta;
                }
      PutPixel(round(x),round(y),color);
     }
}

 I sincerely hope this helps.

 Scott McCaughrin
 Dept. of Computer Science
 University of Illinois

mccaugh@s.cs.uiuc.edu (09/10/89)

Incidentally, if you have access to Turbo-C, I believe that it has a
graphic primitive called 'ellipse' which takes these same parameters
(see declaration in: graphics.h); it also has 'putpixel' with the same
declaration as I have assumed. 
Furthermore, the function Pi() is supplied (in: math.h) by M_PI (to
18 places) and M_PI_2 replaces my Pi_2. But you would still need Pi_3_2.

Scott McCaughrin