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