xref: /csrg-svn/lib/libplot/gigi/arc.c (revision 15484)
1*15484Sralph #ifndef lint
2*15484Sralph static char sccsid[] = "@(#)arc.c	4.1 (Berkeley) 11/10/83";
3*15484Sralph #endif
4*15484Sralph 
5*15484Sralph #include "gigi.h"
6*15484Sralph 
7*15484Sralph /*
8*15484Sralph  * gigi requires knowing the anlge of arc.  To do this, the triangle formula
9*15484Sralph  *	c^2 = a^2 + b^2 - 2*a*b*cos(angle)
10*15484Sralph  * is used where "a" and "b" are the radius of the circle and "c" is the
11*15484Sralph  * distance between the beginning point and the end point.
12*15484Sralph  *
13*15484Sralph  * This gives us "angle" or angle - 180.  To find out which, draw a line from
14*15484Sralph  * beg to center.  This splits the plane in half.  All points on one side of the
15*15484Sralph  * plane will have the same sign when plugged into the equation for the line.
16*15484Sralph  * Pick a point on the "right side" of the line (see program below).  If "end"
17*15484Sralph  * has the same sign as this point does, then they are both on the same side
18*15484Sralph  * of the line and so angle is < 180.  Otherwise, angle > 180.
19*15484Sralph  */
20*15484Sralph 
21*15484Sralph #define side(x,y)	(a*(x)+b*(y)+c > 0.0 ? 1 : -1)
22*15484Sralph 
23*15484Sralph arc(xcent,ycent,xbeg,ybeg,xend,yend)
24*15484Sralph int xcent,ycent,xbeg,ybeg,xend,yend;
25*15484Sralph {
26*15484Sralph 	double radius2, c2;
27*15484Sralph 	double a,b,c;
28*15484Sralph 	int angle;
29*15484Sralph 
30*15484Sralph 	/* Probably should check that this is really a circular arc.  */
31*15484Sralph 	radius2 = (xcent-xbeg)*(xcent-xbeg) + (ycent-ybeg)*(ycent-ybeg);
32*15484Sralph 	c2 = (xend-xbeg)*(xend-xbeg) + (yend-ybeg)*(yend-ybeg);
33*15484Sralph 	angle = (int) ( 180.0/PI * acos(1.0 - c2/(2.0*radius2)) + 0.5 );
34*15484Sralph 
35*15484Sralph 	a = (double) (ycent - ybeg);
36*15484Sralph 	b = (double) (xcent - xbeg);
37*15484Sralph 	c = (double) (ycent*xbeg - xcent*ybeg);
38*15484Sralph 	if (side(xbeg + (ycent-ybeg), ybeg - (xcent-xbeg)) != side(xend,yend))
39*15484Sralph 		angle += 180;
40*15484Sralph 
41*15484Sralph 	move(xcent, ycent);
42*15484Sralph 	printf("C(A%d c)[%d,%d]", angle, xbeg, ybeg);
43*15484Sralph }
44