Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix point-linestring intersection #500

Merged
merged 2 commits into from
Aug 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 30 additions & 9 deletions geo-types/src/private_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,36 @@ where
return true;
}
for line in line_string.lines() {
if ((line.start.y == line.end.y)
&& (line.start.y == point.y())
&& (point.x() > line.start.x.min(line.end.x))
&& (point.x() < line.start.x.max(line.end.x)))
|| ((line.start.x == line.end.x)
&& (line.start.x == point.x())
&& (point.y() > line.start.y.min(line.end.y))
&& (point.y() < line.start.y.max(line.end.y)))
{
// This is a duplicate of the line-contains-point logic in the "intersects" module
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah yes, great shame be on us all.

These algorithms exist in geo-types so we can have a nice rstar integration, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, although as we have now discovered they can easily get out of whack and cause problems, so I'd love to find some better way of providing the functionality, although the reason it's done like this is to avoid circular dependencies 😩

let tx = if line.dx() == T::zero() {
None
} else {
Some((point.x() - line.start.x) / line.dx())
};
let ty = if line.dy() == T::zero() {
None
} else {
Some((point.y() - line.start.y) / line.dy())
};
let contains = match (tx, ty) {
(None, None) => {
// Degenerate line
point.0 == line.start
}
(Some(t), None) => {
// Horizontal line
point.y() == line.start.y && T::zero() <= t && t <= T::one()
}
(None, Some(t)) => {
// Vertical line
point.x() == line.start.x && T::zero() <= t && t <= T::one()
}
(Some(t_x), Some(t_y)) => {
// All other lines
(t_x - t_y).abs() <= T::epsilon() && T::zero() <= t_x && t_x <= T::one()
}
};
if contains {
return true;
}
}
Expand Down
11 changes: 10 additions & 1 deletion geo/src/algorithm/contains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ where
T: Float,
{
fn contains(&self, p: &Point<T>) -> bool {
::geo_types::private_utils::line_string_contains_point(self, *p)
self.lines().any(|line| line.contains(p))
}
}

Expand Down Expand Up @@ -439,6 +439,15 @@ mod test {
use crate::algorithm::contains::Contains;
use crate::line_string;
use crate::{Coordinate, Line, LineString, MultiPolygon, Point, Polygon, Rect, Triangle};


#[test]
// see https://github.com/georust/geo/issues/452
fn linestring_contains_point() {
let line_string = LineString::from(vec![(0., 0.), (3., 3.)]);
let point_on_line = Point::new(1., 1.);
assert!(line_string.contains(&point_on_line));
}
#[test]
// V doesn't contain rect because two of its edges intersect with V's exterior boundary
fn polygon_does_not_contain_polygon() {
Expand Down