From 51a4efd970cd9bd33356dd549c223ad095fa57f1 Mon Sep 17 00:00:00 2001 From: mattbatt Date: Mon, 19 Jun 2017 22:20:03 -0400 Subject: [PATCH 1/2] Incorrect usage of `abs_sub` `x.abs_sub(y)` is defined ([confusingly](https://github.com/rust-lang/rust/issues/30315)) as `max(x-y, 0)`, not `abs(x-y)`. This wasn't caught in tests since `t_x - t_y` in [src/algorithm/intersects.rs:48](https://github.com/georust/rust-geo/blob/master/src/algorithm/intersects.rs#L48) is positive in all of the given examples. --- src/algorithm/intersects.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/algorithm/intersects.rs b/src/algorithm/intersects.rs index 8e6e88a4e..bdbaf7d02 100644 --- a/src/algorithm/intersects.rs +++ b/src/algorithm/intersects.rs @@ -45,7 +45,7 @@ impl Intersects> for Line t <= T::one() }, (Some(t_x), Some(t_y)) => { // All other lines - t_x.abs_sub(t_y) <= T::epsilon() && + (t_x - t_y).abs() <= T::epsilon() && T::zero() <= t_x && t_x <= T::one() } @@ -442,6 +442,7 @@ mod test { #[test] fn point_intersects_line_test() { let p0 = Point::new(2., 4.); + let p1 = Point::new(0., 0.); // vertical line let line1 = Line::new(Point::new(2., 0.), Point::new(2., 5.)); // point on line, but outside line segment @@ -454,6 +455,7 @@ mod test { assert!(!p0.intersects(&line2)); assert!(line3.intersects(&p0)); assert!(p0.intersects(&line3)); + assert!(!p1.intersects(&line3)); } #[test] fn line_intersects_line_test() { From 6c3ea2f3ddff40fde99060a1a9fac5c71dd026d5 Mon Sep 17 00:00:00 2001 From: mattbatt Date: Tue, 20 Jun 2017 10:57:23 -0400 Subject: [PATCH 2/2] Clarify test cases --- src/algorithm/intersects.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/algorithm/intersects.rs b/src/algorithm/intersects.rs index bdbaf7d02..61d1ae8c1 100644 --- a/src/algorithm/intersects.rs +++ b/src/algorithm/intersects.rs @@ -442,20 +442,34 @@ mod test { #[test] fn point_intersects_line_test() { let p0 = Point::new(2., 4.); - let p1 = Point::new(0., 0.); // vertical line let line1 = Line::new(Point::new(2., 0.), Point::new(2., 5.)); // point on line, but outside line segment let line2 = Line::new(Point::new(0., 6.), Point::new(1.5, 4.5)); // point on line let line3 = Line::new(Point::new(0., 6.), Point::new(3., 3.)); + // point above line with positive slope + let line4 = Line::new(Point::new(1., 2.), Point::new(5., 3.)); + // point below line with positive slope + let line5 = Line::new(Point::new(1., 5.), Point::new(5., 6.)); + // point above line with negative slope + let line6 = Line::new(Point::new(1., 2.), Point::new(5., -3.)); + // point below line with negative slope + let line7 = Line::new(Point::new(1., 6.), Point::new(5., 5.)); assert!(line1.intersects(&p0)); assert!(p0.intersects(&line1)); assert!(!line2.intersects(&p0)); assert!(!p0.intersects(&line2)); assert!(line3.intersects(&p0)); assert!(p0.intersects(&line3)); - assert!(!p1.intersects(&line3)); + assert!(!line4.intersects(&p0)); + assert!(!p0.intersects(&line4)); + assert!(!line5.intersects(&p0)); + assert!(!p0.intersects(&line5)); + assert!(!line6.intersects(&p0)); + assert!(!p0.intersects(&line6)); + assert!(!line7.intersects(&p0)); + assert!(!p0.intersects(&line7)); } #[test] fn line_intersects_line_test() {