home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
vis-ftp.cs.umass.edu
/
vis-ftp.cs.umass.edu.tar
/
vis-ftp.cs.umass.edu
/
pub
/
Software
/
ASCENDER
/
ascendMar8.tar
/
UMass
/
BuildingFinder
/
Staging
/
intersect.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-05-09
|
5KB
|
277 lines
/*
*
* IntersectLines
*
* Compute the Intersection of two lines using the Parametric
* representations.
*
*
* _ _ _ _ _ _
* | (x2 - x1) (x3 - x4) | | t | | x3 - x1 |
* | (y2 - y1) (y3 - y4) | | s | = | y3 - y1 |
* - - - - - -
*
*/
#include "../polygons.h"
#define T 2
#define L 3
#define N -1
#define LOC_PERCENTAGE 0.8
#define PIBY2 1.570796
#define BIG_THETA 0.2
#define MIN_ANGLE_THRESH 1.2
#define SMALL(x,y) ((x < y) ? x : y)
#define nearEnd(x,val) ((fabs(x) >= val) || (fabs(1.0 - x) >= val))
#define onLine(t,val) ((t >= (1.0 - val)) && (t <= val))
int conflictIntersection(int x1, int y1, int x2, int y2)
/*
* Given endpoints of two lines, is there an intersection that occurs
* between the line and any other lines in the line list.
*
*
*/
{
double_2 *lines_form_corner();
line *lptr;
float val;
line lrec;
double parms[2];
double_2 Ipoint;
double_2 *apoint;
lrec.x1 = (double) x1; lrec.y1 = (double) y1;
lrec.x2 = (double) x2; lrec.y2 = (double) y2;
lptr = line_list;
while (lptr != NULL) {
intersect_point(lrec,*lptr, &Ipoint);
computeParms(Ipoint,lrec,*lptr,parms);
if (onLine(parms[0],LOC_PERCENTAGE) &&
onLine(parms[1],LOC_PERCENTAGE) && angleBetween(lrec,*lptr) >
MIN_ANGLE_THRESH ) {
return(TRUE);
}
lptr = lptr->next;
}
return(FALSE);
}
/***
float IntersectLines(line l1, line l2, double Parm[2])
{
float **A;
float *B;
int *index;
float d;
int j;
Point p;
A = matrix(1,2,1,2);
B = vector(1,2);
index = ivector(1,2);
fprintf(stderr,"Intersect lines: (%f %f, %f %f) -- (%f %f, %f %f)\n",
l1.x1,l1.y1,l1.x2,l1.y2,l2.x1,l2.y1,l2.x2,l2.y2);
A[1][1] = (float)(l1.x2 - l1.x1);
A[1][2] = (float)(l2.x1 - l2.x2);
A[2][1] = (float)(l1.y2 - l1.y1);
A[2][2] = (float)(l2.y1 - l2.y2);
B[1] = (l2.x1 - l1.x1);
B[2] = (l2.y1 - l1.y1);
ludcmp(A, 2, index, &d);
for (j=1; j <= 2; j++)
d *= A[j][j];
if (d == 0) {
Parm[0] = -1.0;
Parm[1] = -1.0;
return(-1.0);
}
lubksb(A, 2, index, B);
Parm[0] = B[1];
Parm[1] = B[2];
free_matrix(A,1.0,2.0,1.0,2.0);
free_vector(B,1.0,2.0);
return(Parm[0]);
}
***/
/*
double
angleBetween(line l1, line l2)
*
* Compute the angle between two lines as:
*
{
double theta1, theta2;
theta1 = LineSegTheta(l1.x1,l1.y1,l1.x2,l1.y2);
theta2 = LineSegTheta(l2.x1,l2.y1,l2.x2,l2.y2);
return( fabs(theta1 - theta2));
}
*/
double lineTheta(double StartCol, double StartRow, double EndCol, double EndRow)
{
double dx,dy; /* Buffers */
double Orientation;
dx = EndCol - StartCol;
dy = EndRow - StartRow;
if (dx == 0) return(PIBY2); /* Line is vertical */
if (dy == 0) return(0); /* Line is Horizontal */
Orientation = atan2( dy, dx );
/*
Make it so line orientation is always between 0 and pi.
*/
if (Orientation < 0 )
Orientation = Orientation + PI;
if (Orientation > PI)
Orientation = Orientation - PI;
return(Orientation);
}
int IntersectType(double t, double s)
{
if (nearEnd(t,LOC_PERCENTAGE) && nearEnd(s,LOC_PERCENTAGE))
return(L);
if (nearEnd(t,LOC_PERCENTAGE) && onLine(s,LOC_PERCENTAGE))
return(T);
if (onLine(t,LOC_PERCENTAGE) && nearEnd(s,LOC_PERCENTAGE))
return(T);
if (onLine(t,LOC_PERCENTAGE) && onLine(s,LOC_PERCENTAGE))
return(X);
return(N);
}
/*
*
* Intersect
*
*/
void
intersect_point(line l1, line l2, double_2 *result)
{
Point vec1, vec2;
double scale;
double a1, b1, c1, a2, b2, c2;
double div;
a1 = l1.y1 - l1.y2;
b1 = l1.x2 - l1.x1;
c1 = (l1.x1 * l1.y2) - (l1.x2 * l1.y1);
scale = sqrt ( SQ(a1) + SQ(b1) );
if (scale == 0) {
result->d1 = -2.0;
result->d2 = -2.0;
return;
}
vec1.x = a1 / scale; vec1.y = b1 /scale; vec1.z = c1 /scale;
a2 = l2.y1 - l2.y2;
b2 = l2.x2 - l2.x1;
c2 = (l2.x1 * l2.y2) - (l2.x2 * l2.y1);
scale = sqrt ( SQ(a2) + SQ(b2) );
if (scale == 0.0) {
result->d1 = -2.0;
result->d2 = -2.0;
return;
}
vec1.x = a2 / scale; vec1.y = b2 /scale; vec1.z = c2 /scale;
div = (a1 * b2) - (a2 * b1);
if (div == 0.0) {
result->d1 = -2.0;
result->d2 = -2.0;
return;
}
result->d1 = ((b1 * c2) - (b2 * c1)) / div;
result->d2 = ((c1 * a2) - (c2 * a1)) / div;
}
void
computeParms(double_2 vert, line l1, line l2, double parms[2])
{
Point vec1, vec2;
vec1.x = l1.x2 - l1.x1;
vec1.y = l1.y2 - l1.y1;
vec2.x = vert.d1 - l1.x1;
vec2.y = vert.d2 - l1.y1;
if (vec1.x == 0.0 && vec1.y == 0.0) /* Zero length !! */
parms[0] = -1.0;
else
parms[0] = (vec1.x*vec2.x + vec1.y*vec2.y) /
SQ(distance(0.0,0.0,vec1.x,vec1.y));
vec1.x = l2.x2 - l2.x1;
vec1.y = l2.y2 - l2.y1;
vec2.x = vert.d1 - l2.x1;
vec2.y = vert.d2 - l2.y1;
if (vec2.x == 0.0 && vec2.y == 0.0) /* Zero length !! */
parms[1] = -1.0;
else
parms[1] = (vec1.x*vec2.x + vec1.y*vec2.y) /
SQ(distance(0.0,0.0,vec1.x,vec1.y));
}