xref: /csrg-svn/lib/libplot/gigi/arc.c (revision 61384)
148508Sbostic /*-
2*61384Sbostic  * Copyright (c) 1980, 1993
3*61384Sbostic  *	The Regents of the University of California.  All rights reserved.
448508Sbostic  *
548508Sbostic  * %sccs.include.proprietary.c%
619974Sdist  */
719974Sdist 
815484Sralph #ifndef lint
9*61384Sbostic static char sccsid[] = "@(#)arc.c	8.1 (Berkeley) 06/04/93";
1048508Sbostic #endif /* not lint */
1115484Sralph 
1215484Sralph #include "gigi.h"
1315484Sralph 
1415484Sralph /*
1515484Sralph  * gigi requires knowing the anlge of arc.  To do this, the triangle formula
1615484Sralph  *	c^2 = a^2 + b^2 - 2*a*b*cos(angle)
1715484Sralph  * is used where "a" and "b" are the radius of the circle and "c" is the
1815484Sralph  * distance between the beginning point and the end point.
1915484Sralph  *
2015484Sralph  * This gives us "angle" or angle - 180.  To find out which, draw a line from
2115484Sralph  * beg to center.  This splits the plane in half.  All points on one side of the
2215484Sralph  * plane will have the same sign when plugged into the equation for the line.
2315484Sralph  * Pick a point on the "right side" of the line (see program below).  If "end"
2415484Sralph  * has the same sign as this point does, then they are both on the same side
2515484Sralph  * of the line and so angle is < 180.  Otherwise, angle > 180.
2615484Sralph  */
2715484Sralph 
2815484Sralph #define side(x,y)	(a*(x)+b*(y)+c > 0.0 ? 1 : -1)
2915484Sralph 
arc(xcent,ycent,xbeg,ybeg,xend,yend)3015484Sralph arc(xcent,ycent,xbeg,ybeg,xend,yend)
3115484Sralph int xcent,ycent,xbeg,ybeg,xend,yend;
3215484Sralph {
3315484Sralph 	double radius2, c2;
3415484Sralph 	double a,b,c;
3515484Sralph 	int angle;
3615484Sralph 
3715484Sralph 	/* Probably should check that this is really a circular arc.  */
3815484Sralph 	radius2 = (xcent-xbeg)*(xcent-xbeg) + (ycent-ybeg)*(ycent-ybeg);
3915484Sralph 	c2 = (xend-xbeg)*(xend-xbeg) + (yend-ybeg)*(yend-ybeg);
4015484Sralph 	angle = (int) ( 180.0/PI * acos(1.0 - c2/(2.0*radius2)) + 0.5 );
4115484Sralph 
4215484Sralph 	a = (double) (ycent - ybeg);
4315484Sralph 	b = (double) (xcent - xbeg);
4415484Sralph 	c = (double) (ycent*xbeg - xcent*ybeg);
4515484Sralph 	if (side(xbeg + (ycent-ybeg), ybeg - (xcent-xbeg)) != side(xend,yend))
4615484Sralph 		angle += 180;
4715484Sralph 
4815484Sralph 	move(xcent, ycent);
4915484Sralph 	printf("C(A%d c)[%d,%d]", angle, xbeg, ybeg);
5015484Sralph }
51