1*18194Sjaap #ifndef lint
2*18194Sjaap static char *sccsid ="inter.c	(CWI)	1.1	85/03/01";
3*18194Sjaap #endif
4*18194Sjaap #include "ideal.h"
5*18194Sjaap #include "y.tab.h"
6*18194Sjaap 
llinter(x1,y1,x2,y2,z1,w1,z2,w2,alpha,beta,collinear)7*18194Sjaap boolean llinter (x1,y1, x2,y2, z1,w1, z2,w2, alpha, beta, collinear)
8*18194Sjaap 	/* if this function returns TRUE,
9*18194Sjaap 	/* then alpha[(x1,y1),(x2,y2)] = beta[(z1,w1),(z2,w2)] */
10*18194Sjaap float x1,y1, x2,y2, z1,w1, z2,w2;
11*18194Sjaap float *alpha;
12*18194Sjaap float *beta;
13*18194Sjaap boolean *collinear;
14*18194Sjaap {
15*18194Sjaap 	float A1, B1, C1, A2, B2, C2, D, x, y;
16*18194Sjaap 	dprintf "(%f,%f) -- (%f,%f)\n", x1,y1, x2,y2);
17*18194Sjaap 	dprintf "(%f,%f) -- (%f,%f)\n", z1,w1, z2,w2);
18*18194Sjaap 	A1 = y1 - y2;
19*18194Sjaap 	B1 = x2 - x1;
20*18194Sjaap 	C1 = -B1*y1 - A1*x1;
21*18194Sjaap 	A2 = w1 - w2;
22*18194Sjaap 	B2 = z2 - z1;
23*18194Sjaap 	C2 = -B2*w1 - A2*z1;
24*18194Sjaap 	D = A1*B2 - A2*B1;
25*18194Sjaap 	if (fabs(D) < EPSILON) {
26*18194Sjaap 		*collinear = arecollinear(x1,y1,x2,y2,z1,w1);
27*18194Sjaap 		dprintf "%s\n", (*collinear)?"coincident":"disjoint");
28*18194Sjaap 		return (FALSE);
29*18194Sjaap 	}
30*18194Sjaap 	*collinear = FALSE;
31*18194Sjaap 	x = (B1*C2 - B2*C1)/D;
32*18194Sjaap 	y = (A2*C1 - A1*C2)/D;
33*18194Sjaap 	if (fabs(x2 - x1) > EPSILON) {
34*18194Sjaap 		*alpha = (x - x1)/(x2 - x1);
35*18194Sjaap 	} else if (fabs(y2 - y1) > EPSILON) {
36*18194Sjaap 		*alpha = (y - y1)/(y2 - y1);
37*18194Sjaap 	} else fprintf (stderr, "ideal: llinter: can't happen\n");
38*18194Sjaap 	if (fabs(z2 - z1) > EPSILON) {
39*18194Sjaap 		*beta = (x - z1)/(z2 - z1);
40*18194Sjaap 	} else if (fabs(w2 - w1) > EPSILON) {
41*18194Sjaap 		*beta = (y - w1)/(w2 - w1);
42*18194Sjaap 	} else fprintf (stderr, "ideal: llinter: can't happen\n");
43*18194Sjaap 	dprintf "intersection alpha = %f; beta = %f\n", *alpha, *beta);
44*18194Sjaap 	return (TRUE);
45*18194Sjaap } /* llinter */
46*18194Sjaap 
lcinter(x1,y1,x2,y2,x0,y0,r,alpha1,theta1,alpha2,theta2)47*18194Sjaap boolean lcinter (x1,y1, x2,y2, x0,y0, r, alpha1,theta1, alpha2,theta2)
48*18194Sjaap 	/* if this function returns TRUE,
49*18194Sjaap 	/* then alpha1[(x1,y1),(x2,y2)] = (x0,y0) + r*cis(theta1)
50*18194Sjaap 	/* and alpha2[(x1,y1),(x2,y2)] = (x0,y0) + r*cis(theta2) */
51*18194Sjaap float x1,y1, x2,y2, x0,y0, r;
52*18194Sjaap float *alpha1;
53*18194Sjaap float *theta1;
54*18194Sjaap float *alpha2;
55*18194Sjaap float *theta2;
56*18194Sjaap {
57*18194Sjaap 	float dx1, dx2, dy1, dy2;
58*18194Sjaap 	float A, B, C, D;
59*18194Sjaap 
60*18194Sjaap 	dprintf "intersection parameters:\n");
61*18194Sjaap 	dprintf "%f, %f -- %f, %f\n", x1, y1, x2, y2);
62*18194Sjaap 	dprintf "%f, %f (%f)\n", x0, y0, r);
63*18194Sjaap 	r = fabs(r);
64*18194Sjaap 	dx1 = x1 - x0;
65*18194Sjaap 	dx2 = x2 - x1;
66*18194Sjaap 	dy1 = y1 - y0;
67*18194Sjaap 	dy2 = y2 - y1;
68*18194Sjaap 	A = dx2*dx2 + dy2*dy2;
69*18194Sjaap 	dprintf "A=%f\n", A);
70*18194Sjaap 	if (A < EPSILON) {
71*18194Sjaap 		if (fabs (hypot (dx1, dy1) - r) < EPSILON) {
72*18194Sjaap 			*alpha1 = *alpha2 = 0.0;
73*18194Sjaap 			*theta1 = atan2 (dy1, dx1);
74*18194Sjaap 			*theta2 = *theta1 = rprin (*theta1);
75*18194Sjaap 			dprintf "alpha1 = alpha2 = %f theta1 = theta2 = %f\n",
76*18194Sjaap 				*alpha1, *theta1);
77*18194Sjaap 			return (TRUE);
78*18194Sjaap 		}
79*18194Sjaap 		else
80*18194Sjaap 			return (FALSE);
81*18194Sjaap 	}
82*18194Sjaap 	B = 2*(dx1*dx2 + dy1*dy2);
83*18194Sjaap 	C = dx1*dx1 + dy1*dy1 - r*r;
84*18194Sjaap 	D = B*B - 4*A*C;
85*18194Sjaap 	dprintf "B=%f C=%f D=%f\n", B, C, D);
86*18194Sjaap 	if (D < -EPSILON)
87*18194Sjaap 		return (FALSE);
88*18194Sjaap 	if (fabs(D) < EPSILON)
89*18194Sjaap 		D = 0.0;
90*18194Sjaap 	D = sqrt(D);
91*18194Sjaap 	*alpha1 = (-B + D)/(2.0*A);
92*18194Sjaap 	*theta1 = rprin (atan2 (dy1 + *alpha1*dy2, dx1 + *alpha1*dx2));
93*18194Sjaap 	*alpha2 = (-B - D)/(2.0*A);
94*18194Sjaap 	*theta2 = rprin (atan2 (dy1 + *alpha2*dy2, dx1 + *alpha2*dx2));
95*18194Sjaap 	dprintf "intersection alpha1 = %f, theta1 = %f\n", *alpha1, *theta1);
96*18194Sjaap 	dprintf "intersection alpha2 = %f, theta2 = %f\n", *alpha2, *theta2);
97*18194Sjaap 	return (TRUE);
98*18194Sjaap }
99*18194Sjaap 
ccinter(x0,y0,r0,x1,y1,r1,theta1,phi1,theta2,phi2)100*18194Sjaap boolean ccinter (x0,y0,r0, x1,y1,r1, theta1,phi1, theta2,phi2)
101*18194Sjaap 	/* if this function returns TRUE,
102*18194Sjaap 	/* then (x0,y0) + r0*cis(theta1) = (x1,y1) + r1*cis(phi1)
103*18194Sjaap 	/* and (x0,y0) + r0*cis(theta2) = (x1,y1) + r1*cis(phi2) */
104*18194Sjaap float x0,y0,r0;
105*18194Sjaap float x1,y1,r1;
106*18194Sjaap float *theta1;
107*18194Sjaap float *phi1;
108*18194Sjaap float *theta2;
109*18194Sjaap float *phi2;
110*18194Sjaap {
111*18194Sjaap 	float xcoeff, ycoeff, const;
112*18194Sjaap 	float u1, v1, u2, v2;
113*18194Sjaap 	boolean lncrc;
114*18194Sjaap 
115*18194Sjaap 	dprintf "intersection parameters\n");
116*18194Sjaap 	dprintf "%f %f (%f)\n", x0, y0, r0);
117*18194Sjaap 	dprintf "%f %f (%f)\n", x1, y1, r1);
118*18194Sjaap 
119*18194Sjaap 	r0 = fabs(r0);
120*18194Sjaap 	r1 = fabs(r1);
121*18194Sjaap 	xcoeff = 2*(x1 - x0);
122*18194Sjaap 	ycoeff = 2*(y1 - y0);
123*18194Sjaap 	const = r0*r0 - x0*x0 - y0*y0 - r1*r1 + x1*x1 + y1*y1;
124*18194Sjaap 	if (fabs(xcoeff) < EPSILON && fabs(ycoeff) < EPSILON)
125*18194Sjaap 		return (FALSE);
126*18194Sjaap 	if (fabs(xcoeff) < EPSILON) {
127*18194Sjaap 		u1 = 0.0;
128*18194Sjaap 		u2 = 1.0;
129*18194Sjaap 		v1 = v2 = const/ycoeff;
130*18194Sjaap 	} else if (fabs(ycoeff) < EPSILON) {
131*18194Sjaap 		v1 = 0.0;
132*18194Sjaap 		v2 = 1.0;
133*18194Sjaap 		u1 = u2 = const/xcoeff;
134*18194Sjaap 	} else if (fabs(const) < EPSILON) {
135*18194Sjaap 		u1 = 0.0;
136*18194Sjaap 		v1 = 0.0;
137*18194Sjaap 		u2 = 1.0;
138*18194Sjaap 		v2 = (const - 1.0/xcoeff)/ycoeff;
139*18194Sjaap 	} else {
140*18194Sjaap 		u1 = 0.0;
141*18194Sjaap 		v1 = const/ycoeff;
142*18194Sjaap 		u2 = const/xcoeff;
143*18194Sjaap 		v2 = 0.0;
144*18194Sjaap 	}
145*18194Sjaap 	lncrc = lcinter (u1,v1, u2,v2, x1,y1,r1, theta1,phi1, theta2,phi2);
146*18194Sjaap 	if (lncrc) {
147*18194Sjaap 		*phi1 = rprin (*phi1);
148*18194Sjaap 		*phi2 = rprin (*phi2);
149*18194Sjaap 		*theta1 = atan2 (y1 + r1*sin(*phi1) - y0, x1 + r1*cos(*phi1) - x0);
150*18194Sjaap 		*theta2 = atan2 (y1 + r1*sin(*phi2) - y0, x1 + r1*cos(*phi2) - x0);
151*18194Sjaap 		*theta1 = rprin (*theta1);
152*18194Sjaap 		*theta2 = rprin (*theta2);
153*18194Sjaap 		dprintf "intersection theta1 = %f phi1 = %f\n", *theta1, *phi1);
154*18194Sjaap 		dprintf "intersection theta2 = %f phi2 = %f\n", *theta2, *phi2);
155*18194Sjaap 		return (TRUE);
156*18194Sjaap 	} else
157*18194Sjaap 		return (FALSE);
158*18194Sjaap }
159