Skip to content

Commit

Permalink
Merge #120
Browse files Browse the repository at this point in the history
120: Incorrect usage of `abs_sub` r=frewsxcv

`x.abs_sub(y)` is defined ([confusingly](rust-lang/rust#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) happens to be positive in all of the given examples so I've added a new example where `t_x - t_y` is negative.
  • Loading branch information
bors[bot] committed Jun 22, 2017
2 parents 2df70f9 + 6c3ea2f commit 2bc82be
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/algorithm/intersects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl<T> Intersects<Point<T>> for Line<T>
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()
}
Expand Down Expand Up @@ -448,12 +448,28 @@ mod test {
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!(!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() {
Expand Down

0 comments on commit 2bc82be

Please sign in to comment.