xref: /csrg-svn/lib/libplot/bitgraph/arc.c (revision 19848)
1 #ifndef lint
2 static char sccsid[] = "@(#)arc.c	4.1 (Berkeley) 11/10/83";
3 #endif
4 
5 #include "bg.h"
6 
7 /* should include test for equality? */
8 #define side(x,y)	(a*(x)+b*(y)+c > 0.0 ? 1 : -1)
9 
10 /* The beginning and ending points must be distinct. */
11 arc(xc,yc,xbeg,ybeg,xend,yend)
12 int xc,yc,xbeg,ybeg,xend,yend;
13 {
14 	double r, radius, costheta, sintheta;
15 	double a, b, c, x, y, tempX;
16 	int right_side;
17 
18 	int screen_xc = scaleX(xc);
19 	int screen_yc = scaleY(yc);
20 
21 	/* It is more convienient to beg and end relative to center. */
22 	int screen_xbeg = scaleX(xbeg) - screen_xc;
23 	int screen_ybeg = scaleY(ybeg) - screen_yc;
24 
25 	int screen_xend = scaleX(xend) - screen_xc;
26 	int screen_yend = scaleY(yend) - screen_yc;
27 
28 	/* probably should check that arc is truely circular */
29 	r = sqrt( (double) (screen_xbeg*screen_xbeg + screen_ybeg*screen_ybeg) );
30 
31 	/*
32 	This method is reasonably efficient, clean, and clever.
33 	The easy part is generating the next point on the arc.  This is
34 	done by rotating the points by the angle theta.  Theta is chosen
35 	so that no rotation will cause more than one pixel of a move.
36 	This corresponds to a triangle having x side of r and y side of 1.
37 	The rotation is done (way) below inside the loop.
38 
39 	Note:  all calculations are done in screen coordinates.
40 	*/
41 	if (r <= 1.0) {
42 		/* radius is mapped to length < 1*/
43 		point(xc,yc);
44 		return;
45 		}
46 
47 	radius = sqrt(r*r + 1.0);
48 	sintheta = 1.0/radius;
49 	costheta = r/radius;
50 
51 	/*
52 	The hard part of drawing an arc is figuring out when to stop.
53 	This method works by drawing the line from the beginning point
54 	to the ending point.  This splits the plane in half, with the
55 	arc that we wish to draw on one side of the line.  If we evaluate
56 	side(x,y) = a*x + b*y + c, then all of the points on one side of the
57 	line will result in side being positive, and all the points on the
58 	other side of the line will result in side being negative.
59 
60 	We want to draw the arc in a counter-clockwise direction, so we
61 	must find out what the sign of "side" is for a point which is to the
62 	"right" of a line drawn from "beg" to "end".  A point which must lie
63 	on the right is [xbeg + (yend-ybeg), ybeg - (xend-xbeg)].  (This
64 	point is perpendicular to the line at "beg").
65 
66 	Thus, we compute side of the above point, and then compare the
67 	sign of side for each new point with the sign of the above point.
68 	When they are different, we terminate the loop.
69 	*/
70 
71 	a = (double) (screen_yend - screen_ybeg);
72 	b = (double) (screen_xend - screen_xbeg);
73 	c = (double) (screen_yend*screen_xbeg - screen_xend*screen_ybeg);
74 	right_side = side(screen_xbeg + (screen_yend-screen_ybeg),
75 			  screen_ybeg - (screen_xend-screen_xbeg) );
76 
77 	x = screen_xbeg;
78 	y = screen_ybeg;
79 	move(xbeg, ybeg);
80 	do {
81 		currentx = screen_xc + (int) (x + 0.5);
82 		currenty = screen_yc + (int) (y + 0.5);
83 		putchar( ESC );
84 		printf(":%d;%dd", currentx, currenty);
85 		tempX = x;
86 		x = x*costheta - y*sintheta;
87 		y = tempX*sintheta + y*costheta;
88 	} while( side(x,y) == right_side );
89 }
90