[Vxl-users] Re: bug in vgl_lineseg_test_lineseg function

 [Vxl-users] Re: bug in vgl_lineseg_test_lineseg function From: Peter Vanroose - 2005-09-03 17:12:26 ```> the function returns `true', which means the two line segments > intersect while clearly these are two completely separate, > non-intersecting linesegments. I've corrected this bug; updated file vgl/vgl_lineseg_test.txx is now in CVS. Here is the patch: diff -u -r1.1 -r1.2 --- core/vgl/vgl_lineseg_test.txx 17 Nov 2003 21:05:12 -0000 1.1 +++ core/vgl/vgl_lineseg_test.txx 3 Sep 2005 14:32:41 -0000 1.2 @@ -18,9 +18,27 @@ return (a<=0 && b>=0) || (a>=0 && b<=0); } +// Returns true iif (x3,y3) lies inbetween (x1,y1) and (x2,y2); +// all three points are assumed to be collinear +template +static inline +bool inbetween(T x1, T y1, T x2, T y2, T x3, T y3) +{ + return (x1-x3)*(x2-x3)<=0 && (y1-y3)*(y2-y3)<=0; +} + template bool vgl_lineseg_test_lineseg(T x1, T y1, T x2, T y2, T x3, T y3, T x4, T y4) { return vgl_lineseg_test_line(x1, y1, x2, y2, x3, y3, x4, y4) && - vgl_lineseg_test_line(x3, y3, x4, y4, x1, y1, x2, y2); + vgl_lineseg_test_line(x3, y3, x4, y4, x1, y1, x2, y2) && + ( // the above two conditions are only sufficient + // for noncollinear line segments! - PVr + !collinear(vgl_point_2d(x1, y1), vgl_point_2d(x2, y2), + vgl_point_2d(x3, y3)) || + !collinear(vgl_point_2d(x1, y1), vgl_point_2d(x2, y2), + vgl_point_2d(x4, y4)) || + inbetween(x1, y1, x2, y2, x3, y3) || + inbetween(x1, y1, x2, y2, x4, y4) + ); } -- Peter. ```

 [Vxl-users] bug in vgl_lineseg_test_lineseg function From: Nhon H. Trinh - 2005-08-16 14:06:07 Attachments: Message as HTML ```Hello, I'd like to report a bug in function: //: true if the linesegment joining [1], [2] meets the linesegment joining [3], [4]. export template bool vgl_lineseg_test_lineseg(T x1, T y1, T x2, T y2, T x3, T y3, T x4, T y4) This function is declared in vgl/vgl_lineseg_test.h and is meant to determined whether two line segments ([1], [2]) and ([3], [4]) intersect. When the inputs are: x1 = 0; y1 = 1; x2 = 1; y2 = 1; x3 = 2; y3 = 1; x4 = 3; y4 = 1; the function returns `true', which means the two line segments intersect while clearly these are two completely separate, non-intersecting linesegments. This bug occurs whenever the two line segments lie on the same line. A fix for this bug would be greatly appreciated. Nhon Trinh ```
 [Vxl-users] Re: bug in vgl_lineseg_test_lineseg function From: Peter Vanroose - 2005-09-03 17:12:26 ```> the function returns `true', which means the two line segments > intersect while clearly these are two completely separate, > non-intersecting linesegments. I've corrected this bug; updated file vgl/vgl_lineseg_test.txx is now in CVS. Here is the patch: diff -u -r1.1 -r1.2 --- core/vgl/vgl_lineseg_test.txx 17 Nov 2003 21:05:12 -0000 1.1 +++ core/vgl/vgl_lineseg_test.txx 3 Sep 2005 14:32:41 -0000 1.2 @@ -18,9 +18,27 @@ return (a<=0 && b>=0) || (a>=0 && b<=0); } +// Returns true iif (x3,y3) lies inbetween (x1,y1) and (x2,y2); +// all three points are assumed to be collinear +template +static inline +bool inbetween(T x1, T y1, T x2, T y2, T x3, T y3) +{ + return (x1-x3)*(x2-x3)<=0 && (y1-y3)*(y2-y3)<=0; +} + template bool vgl_lineseg_test_lineseg(T x1, T y1, T x2, T y2, T x3, T y3, T x4, T y4) { return vgl_lineseg_test_line(x1, y1, x2, y2, x3, y3, x4, y4) && - vgl_lineseg_test_line(x3, y3, x4, y4, x1, y1, x2, y2); + vgl_lineseg_test_line(x3, y3, x4, y4, x1, y1, x2, y2) && + ( // the above two conditions are only sufficient + // for noncollinear line segments! - PVr + !collinear(vgl_point_2d(x1, y1), vgl_point_2d(x2, y2), + vgl_point_2d(x3, y3)) || + !collinear(vgl_point_2d(x1, y1), vgl_point_2d(x2, y2), + vgl_point_2d(x4, y4)) || + inbetween(x1, y1, x2, y2, x3, y3) || + inbetween(x1, y1, x2, y2, x4, y4) + ); } -- Peter. ```