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