xref: /original-bsd/lib/libplot/gigi/arc.c (revision 1299e54c)
1f59b8431Sbostic /*-
2*1299e54cSbostic  * Copyright (c) 1980, 1993
3*1299e54cSbostic  *	The Regents of the University of California.  All rights reserved.
4f59b8431Sbostic  *
5f59b8431Sbostic  * %sccs.include.proprietary.c%
6ba410b69Sdist  */
7ba410b69Sdist 
87e7a0ed6Sralph #ifndef lint
9*1299e54cSbostic static char sccsid[] = "@(#)arc.c	8.1 (Berkeley) 06/04/93";
10f59b8431Sbostic #endif /* not lint */
117e7a0ed6Sralph 
127e7a0ed6Sralph #include "gigi.h"
137e7a0ed6Sralph 
147e7a0ed6Sralph /*
157e7a0ed6Sralph  * gigi requires knowing the anlge of arc.  To do this, the triangle formula
167e7a0ed6Sralph  *	c^2 = a^2 + b^2 - 2*a*b*cos(angle)
177e7a0ed6Sralph  * is used where "a" and "b" are the radius of the circle and "c" is the
187e7a0ed6Sralph  * distance between the beginning point and the end point.
197e7a0ed6Sralph  *
207e7a0ed6Sralph  * This gives us "angle" or angle - 180.  To find out which, draw a line from
217e7a0ed6Sralph  * beg to center.  This splits the plane in half.  All points on one side of the
227e7a0ed6Sralph  * plane will have the same sign when plugged into the equation for the line.
237e7a0ed6Sralph  * Pick a point on the "right side" of the line (see program below).  If "end"
247e7a0ed6Sralph  * has the same sign as this point does, then they are both on the same side
257e7a0ed6Sralph  * of the line and so angle is < 180.  Otherwise, angle > 180.
267e7a0ed6Sralph  */
277e7a0ed6Sralph 
287e7a0ed6Sralph #define side(x,y)	(a*(x)+b*(y)+c > 0.0 ? 1 : -1)
297e7a0ed6Sralph 
arc(xcent,ycent,xbeg,ybeg,xend,yend)307e7a0ed6Sralph arc(xcent,ycent,xbeg,ybeg,xend,yend)
317e7a0ed6Sralph int xcent,ycent,xbeg,ybeg,xend,yend;
327e7a0ed6Sralph {
337e7a0ed6Sralph 	double radius2, c2;
347e7a0ed6Sralph 	double a,b,c;
357e7a0ed6Sralph 	int angle;
367e7a0ed6Sralph 
377e7a0ed6Sralph 	/* Probably should check that this is really a circular arc.  */
387e7a0ed6Sralph 	radius2 = (xcent-xbeg)*(xcent-xbeg) + (ycent-ybeg)*(ycent-ybeg);
397e7a0ed6Sralph 	c2 = (xend-xbeg)*(xend-xbeg) + (yend-ybeg)*(yend-ybeg);
407e7a0ed6Sralph 	angle = (int) ( 180.0/PI * acos(1.0 - c2/(2.0*radius2)) + 0.5 );
417e7a0ed6Sralph 
427e7a0ed6Sralph 	a = (double) (ycent - ybeg);
437e7a0ed6Sralph 	b = (double) (xcent - xbeg);
447e7a0ed6Sralph 	c = (double) (ycent*xbeg - xcent*ybeg);
457e7a0ed6Sralph 	if (side(xbeg + (ycent-ybeg), ybeg - (xcent-xbeg)) != side(xend,yend))
467e7a0ed6Sralph 		angle += 180;
477e7a0ed6Sralph 
487e7a0ed6Sralph 	move(xcent, ycent);
497e7a0ed6Sralph 	printf("C(A%d c)[%d,%d]", angle, xbeg, ybeg);
507e7a0ed6Sralph }
51