xref: /csrg-svn/lib/libplot/imagen/arc.c (revision 24989)
1*24989Ssam /*
2*24989Ssam  * Copyright (c) 1985 Regents of the University of California.
3*24989Ssam  * All rights reserved.  The Berkeley software License Agreement
4*24989Ssam  * specifies the terms and conditions for redistribution.
5*24989Ssam  */
6*24989Ssam 
7*24989Ssam #ifndef lint
8*24989Ssam static char sccsid[] = "@(#)arc.c	5.1 (Berkeley) 09/21/85";
9*24989Ssam #endif not lint
10*24989Ssam 
11*24989Ssam int del = 20;
12*24989Ssam step(d){
13*24989Ssam 	del = d;
14*24989Ssam }
15*24989Ssam arc(x,y,x0,y0,x1,y1){
16*24989Ssam 	double pc;
17*24989Ssam 	double sqrt();
18*24989Ssam 	int flg,m,xc,yc,xs,ys,qs,qf;
19*24989Ssam 	float dx,dy,r;
20*24989Ssam 	char use;
21*24989Ssam 	dx = x-x0;
22*24989Ssam 	dy = y-y0;
23*24989Ssam 	r = dx*dx+dy*dy;
24*24989Ssam 	pc = r;
25*24989Ssam 	pc = sqrt(pc);
26*24989Ssam 	flg = pc/4;
27*24989Ssam 	if(flg == 0)step(1);
28*24989Ssam 	else if(flg < del)step(flg);
29*24989Ssam 	xc = xs = x0;
30*24989Ssam 	yc = ys = y0;
31*24989Ssam 	move(xs,ys);
32*24989Ssam 	if(x0 == x1 && y0 == y1)flg=0;
33*24989Ssam 	else flg=1;
34*24989Ssam 	qs = quad(x,y,x0,y0);
35*24989Ssam 	qf = quad(x,y,x1,y1);
36*24989Ssam 	if(abs(x-x1) < abs(y-y1)){
37*24989Ssam 		use = 'x';
38*24989Ssam 		if(qs == 2 || qs ==3)m = -1;
39*24989Ssam 		else m=1;
40*24989Ssam 	}
41*24989Ssam 	else {
42*24989Ssam 		use = 'y';
43*24989Ssam 		if(qs > 2)m= -1;
44*24989Ssam 		else m= 1;
45*24989Ssam 	}
46*24989Ssam 	while(1){
47*24989Ssam 		switch(use){
48*24989Ssam 		case 'x':
49*24989Ssam 			if(qs == 2 || qs == 3)yc -= del;
50*24989Ssam 			else yc += del;
51*24989Ssam 			dy = yc-y;
52*24989Ssam 			pc = r-dy*dy;
53*24989Ssam 			xc = m*sqrt(pc)+x;
54*24989Ssam 			if((x < xs && x >= xc) || ( x > xs && x <= xc) ||
55*24989Ssam 			    (y < ys && y >= yc) || ( y > ys && y <=  yc) )
56*24989Ssam 			{
57*24989Ssam 				if(++qs > 4)qs=1;
58*24989Ssam 				if(qs == 2 || qs == 3)m= -1;
59*24989Ssam 				else m=1;
60*24989Ssam 				flg=1;
61*24989Ssam 			}
62*24989Ssam 			cont(xc,yc);
63*24989Ssam 			xs = xc;
64*24989Ssam 			ys = yc;
65*24989Ssam 			if(qs == qf && flg == 1)
66*24989Ssam 				switch(qf){
67*24989Ssam 				case 3:
68*24989Ssam 				case 4:
69*24989Ssam 					if(xs >= x1)return;
70*24989Ssam 					continue;
71*24989Ssam 				case 1:
72*24989Ssam 				case 2:
73*24989Ssam 					if(xs <= x1)return;
74*24989Ssam 				}
75*24989Ssam 			continue;
76*24989Ssam 		case 'y':
77*24989Ssam 			if(qs > 2)xc += del;
78*24989Ssam 			else xc -= del;
79*24989Ssam 			dx = xc-x;
80*24989Ssam 			pc = r-dx*dx;
81*24989Ssam 			yc = m*sqrt(pc)+y;
82*24989Ssam 			if((x < xs && x >= xc) || ( x > xs && x <= xc ) ||
83*24989Ssam 			    (y < ys && y >= yc) || (y > ys && y <= yc) )
84*24989Ssam 			{
85*24989Ssam 				if(++qs > 4)qs=1;
86*24989Ssam 				if(qs > 2)m = -1;
87*24989Ssam 				else m = 1;
88*24989Ssam 				flg=1;
89*24989Ssam 			}
90*24989Ssam 			cont(xc,yc);
91*24989Ssam 			xs = xc;
92*24989Ssam 			ys = yc;
93*24989Ssam 			if(qs == qf && flg == 1)
94*24989Ssam 				switch(qs){
95*24989Ssam 				case 1:
96*24989Ssam 				case 4:
97*24989Ssam 					if(ys >= y1)return;
98*24989Ssam 					continue;
99*24989Ssam 				case 2:
100*24989Ssam 				case 3:
101*24989Ssam 					if(ys <= y1)return;
102*24989Ssam 				}
103*24989Ssam 		}
104*24989Ssam 	}
105*24989Ssam }
106*24989Ssam quad(x,y,xp,yp){
107*24989Ssam 	if(x < xp)
108*24989Ssam 		if(y <= yp)return(1);
109*24989Ssam 		else return(4);
110*24989Ssam 	else if(x > xp)
111*24989Ssam 		if(y < yp)return(2);
112*24989Ssam 		else return(3);
113*24989Ssam 	else if(y < yp)return(2);
114*24989Ssam 	else return(4);
115*24989Ssam }
116*24989Ssam abs(a){
117*24989Ssam 	if(a < 0)return(-a);
118*24989Ssam 	return(a);
119*24989Ssam }
120