Browse Source

fix for special case when segments are parallel to y axis

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@47355 15284696-431f-4ddb-bdfa-cd5b030d7da7
Markus Metz 14 years ago
parent
commit
3c389c6b1e
1 changed files with 150 additions and 41 deletions
  1. 150 41
      lib/vector/diglib/linecros.c

+ 150 - 41
lib/vector/diglib/linecros.c

@@ -76,26 +76,58 @@ dig_test_for_intersection(double ax1, double ay1,
 	return 0;
 
     /* segments are colinear. check for overlap */
-    if (ax1 > ax2) {
-	t = ax1;
-	ax1 = ax2;
-	ax2 = t;
-    }
-    if (bx1 > bx2) {
-	t = bx1;
-	bx1 = bx2;
-	bx2 = t;
+
+    if (ax1 != ax2) {
+	/* segments are not parallel to y axis, can use x values */
+    
+	if (ax1 > ax2) {
+	    t = ax1;
+	    ax1 = ax2;
+	    ax2 = t;
+	}
+	if (bx1 > bx2) {
+	    t = bx1;
+	    bx1 = bx2;
+	    bx2 = t;
+	}
+	if (ax1 > bx2)
+	    return 0;
+	if (ax2 < bx1)
+	    return 0;
+
+	/* there is overlap */
+
+	if (ax1 == bx2 || ax2 == bx1)
+	    return 1;		/* endpoints only */
+
+	return -1;			/* true overlap   */
     }
-    if (ax1 > bx2)
-	return 0;
-    if (ax2 < bx1)
-	return 0;
+    else {
+	/* segments are parallel to y axis, use y values */
+	if (ay1 > ay2) {
+	    t = ay1;
+	    ay1 = ay2;
+	    ay2 = t;
+	}
+	if (by1 > by2) {
+	    t = by1;
+	    by1 = by2;
+	    by2 = t;
+	}
+	if (ay1 > by2)
+	    return 0;
+	if (ay2 < by1)
+	    return 0;
 
-    /* there is overlap */
+	/* there is overlap */
 
-    if (ax1 == bx2 || ax2 == bx1)
-	return 1;		/* endpoints only */
-    return -1;			/* true overlap   */
+	if (ay1 == by2 || ay2 == by1)
+	    return 1;		/* endpoints only */
+
+	return -1;			/* true overlap   */
+    }
+    
+    return 0; /* should not be reached */
 }
 
 
@@ -128,32 +160,109 @@ dig_find_intersection(double ax1, double ay1,
     }
 
     /* segments are colinear. check for overlap */
-    if (ax1 > ax2) {
-	t = ax1;
-	ax1 = ax2;
-	ax2 = t;
-    }
-    if (bx1 > bx2) {
-	t = bx1;
-	bx1 = bx2;
-	bx2 = t;
-    }
-    if (ax1 > bx2)
-	return 0;
-    if (ax2 < bx1)
-	return 0;
 
-    /* there is overlap */
+    if (ax1 != ax2) {
+	/* segments are not parallel to y axis, can use x values */
+	if (ax1 > ax2) {
+	    /* need to swap both coords */
+	    t = ax1;
+	    ax1 = ax2;
+	    ax2 = t;
+
+	    t = ay1;
+	    ay1 = ay2;
+	    ay2 = t;
+	}
+	if (bx1 > bx2) {
+	    /* need to swap both coords */
+	    t = bx1;
+	    bx1 = bx2;
+	    bx2 = t;
+
+	    t = by1;
+	    by1 = by2;
+	    by2 = t;
+	}
+	if (ax1 > bx2)
+	    return 0;
+	if (ax2 < bx1)
+	    return 0;
+
+	/* there is overlap */
 
-    if (ax1 == bx2) {
-	*x = ax1;
-	*y = ay1;
-	return 1;		/* endpoints only */
+	if (ax1 == bx2) {
+	    *x = ax1;
+	    *y = ay1;
+	    return 1;		/* endpoints only */
+	}
+	if (ax2 == bx1) {
+	    *x = ax2;
+	    *y = ay2;
+	    return 1;		/* endpoints only */
+	}
+	
+	/* overlap, no single intersection point */
+	if (ax1 > bx1 && ax1 < bx2) {
+	    *x = ax1;
+	    *y = ay1;
+	}
+	else {
+	    *x = ax2;
+	    *y = ay2;
+	}
+	return -1;
     }
-    if (ax2 == bx1) {
-	*x = ax2;
-	*y = ay2;
-	return 1;		/* endpoints only */
+    else {
+	/* segments are parallel to y axis, use y values */
+	if (ay1 > ay2) {
+	    /* need to swap both coords */
+	    t = ay1;
+	    ay1 = ay2;
+	    ay2 = t;
+
+	    t = ax1;
+	    ax1 = ax2;
+	    ax2 = t;
+	}
+	if (by1 > by2) {
+	    /* need to swap both coords */
+	    t = by1;
+	    by1 = by2;
+	    by2 = t;
+
+	    t = by1;
+	    by1 = by2;
+	    by2 = t;
+	}
+	if (ay1 > by2)
+	    return 0;
+	if (ay2 < by1)
+	    return 0;
+
+	/* there is overlap */
+
+	if (ay1 == by2) {
+	    *x = ax1;
+	    *y = ay1;
+	    return 1;		/* endpoints only */
+	}
+	if (ay2 == by1) {
+	    *x = ax2;
+	    *y = ay2;
+	    return 1;		/* endpoints only */
+	}
+
+	/* overlap, no single intersection point */
+	if (ay1 > by1 && ay1 < by2) {
+	    *x = ax1;
+	    *y = ay1;
+	}
+	else {
+	    *x = ax2;
+	    *y = ay2;
+	}
+	return -1;
     }
-    return -1;			/* overlap, no single intersection point */
+
+    return 0; /* should not be reached */
 }