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

Handle line segments intersecting at their endpoints. The algorithm I #378

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
29 changes: 27 additions & 2 deletions geom/src/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ impl Line {
self.pt1().dist_to(self.pt2())
}

/// If two line segments intersect -- including endpoints -- return the point where they hit.
/// Undefined if the two lines have more than one intersection point!
/// If two line segments intersect at exactly one point, including endpoints, return that
/// point.
// TODO Also return the distance along self
pub fn intersection(&self, other: &Line) -> Option<Pt2D> {
// From http://bryceboe.com/2006/10/23/line-segment-intersection-algorithm/
Expand All @@ -70,6 +70,19 @@ impl Line {
|| is_counter_clockwise(self.pt1(), self.pt2(), other.pt1())
== is_counter_clockwise(self.pt1(), self.pt2(), other.pt2())
{
// This algorithm doesn't handle endpoints matching, so do that here.
if self.pt1() == other.pt1() {
return Some(self.pt1());
}
if self.pt1() == other.pt2() {
return Some(self.pt1());
}
if self.pt2() == other.pt1() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

equality is reflexive, so this one check is redundant ( the subsequent p2 == p2 check should stay though)

return Some(self.pt2());
}
if self.pt2() == other.pt2() {
return Some(self.pt2());
}
return None;
}

Expand Down Expand Up @@ -281,3 +294,15 @@ impl fmt::Display for InfiniteLine {
write!(f, ")")
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn intersection() {
let l1 = Line::must_new(Pt2D::new(0.0, 0.0), Pt2D::new(1.0, 1.0));
let l2 = Line::must_new(Pt2D::new(2.0, 2.0), Pt2D::new(1.0, 1.0));
assert_eq!(Some(Pt2D::new(1.0, 1.0)), l1.intersection(&l2));
}
}
23 changes: 15 additions & 8 deletions geom/src/polyline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -653,14 +653,6 @@ impl PolyLine {
}
}

// TODO Why is any of this necessary? Found a test case at the intersection geometry for
// https://www.openstreetmap.org/node/274088813 where this made a huge difference!
if closest_intersection.is_none() {
if self.last_pt() == other.last_pt() {
return Some((self.last_pt(), self.last_line().angle()));
}
}

closest_intersection
}

Expand Down Expand Up @@ -844,3 +836,18 @@ fn to_set(pts: &[Pt2D]) -> (HashSet<HashablePt2D>, HashSet<HashablePt2D>) {
}
(deduped, dupes)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn intersection() {
let pl1 = PolyLine::must_new(vec![Pt2D::new(0.0, 0.0), Pt2D::new(1.0, 1.0)]);
let pl2 = PolyLine::must_new(vec![Pt2D::new(2.0, 2.0), Pt2D::new(1.0, 1.0)]);
assert_eq!(
Some((Pt2D::new(1.0, 1.0), Angle::degrees(45.0))),
pl1.intersection(&pl2)
);
}
}