xref: /csrg-svn/lib/libplot/hp7221/arc.c (revision 61393)
148518Sbostic /*-
2*61393Sbostic  * Copyright (c) 1980, 1993
3*61393Sbostic  *	The Regents of the University of California.  All rights reserved.
448518Sbostic  *
548518Sbostic  * %sccs.include.proprietary.c%
619978Sdist  */
719978Sdist 
815462Sralph #ifndef lint
9*61393Sbostic static char sccsid[] = "@(#)arc.c	8.1 (Berkeley) 06/04/93";
1048518Sbostic #endif /* not lint */
1115462Sralph 
1215462Sralph #include "hp7221.h"
1315462Sralph 
1415462Sralph /*
1515462Sralph  * 7221 requires knowing the anlge of arc.  To do this, the triangle formula
1615462Sralph  *	c^2 = a^2 + b^2 - 2*a*b*cos(angle)
1715462Sralph  * is used where "a" and "b" are the radius of the circle and "c" is the
1815462Sralph  * distance between the beginning point and the end point.
1915462Sralph  *
2015462Sralph  * This gives us "angle" or angle - 180.  To find out which, draw a line from
2115462Sralph  * beg to center.  This splits the plane in half.  All points on one side of the
2215462Sralph  * plane will have the same sign when plugged into the equation for the line.
2315462Sralph  * Pick a point on the "right side" of the line (see program below).  If "end"
2415462Sralph  * has the same sign as this point does, then they are both on the same side
2515462Sralph  * of the line and so angle is < 180.  Otherwise, angle > 180.
2615462Sralph  */
2715462Sralph 
2815462Sralph #define side(x,y)	(a*(x)+b*(y)+c > 0.0 ? 1 : -1)
2915462Sralph 
arc(xcent,ycent,xbeg,ybeg,xend,yend)3015462Sralph arc(xcent,ycent,xbeg,ybeg,xend,yend)
3115462Sralph int xcent,ycent,xbeg,ybeg,xend,yend;
3215462Sralph {
3315462Sralph 	double radius2, c2;
3415462Sralph 	double a,b,c;
3515462Sralph 	int angle;
3615462Sralph 
3715462Sralph 	/* Probably should check that this is really a circular arc.  */
3815462Sralph 	radius2 = (xcent-xbeg)*(xcent-xbeg) + (ycent-ybeg)*(ycent-ybeg);
3915462Sralph 	c2 = (xend-xbeg)*(xend-xbeg) + (yend-ybeg)*(yend-ybeg);
4015462Sralph 	angle = (int) ( 180.0/PI * acos(1.0 - c2/(2.0*radius2)) + 0.5 );
4115462Sralph 
4215462Sralph 	a = (double) (ycent - ybeg);
4315462Sralph 	b = (double) (xcent - xbeg);
4415462Sralph 	c = (double) (ycent*xbeg - xcent*ybeg);
4515462Sralph 	if (side(xbeg + (ycent-ybeg), ybeg - (xcent-xbeg)) != side(xend,yend))
4615462Sralph 		angle += 180;
4715462Sralph 
4815462Sralph 	move(xcent, ycent);
4915462Sralph 	/* Not quite implemented...
5015462Sralph 	printf("C(A%d c)[%d,%d]", angle, xbeg, ybeg);
5115462Sralph 	*/
5215462Sralph }
53