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