Skip to content

Commit

Permalink
Use clockwise algorithm for line segment intersection checks (resolves
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelVo committed Jun 26, 2023
1 parent 44590a5 commit 0368ddb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
9 changes: 9 additions & 0 deletions rust/src/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ impl LineSegment {
}
}

pub fn intersects(&self, other: &LineSegment) -> bool {
is_clockwise(self.p1, other.p1, other.p2) != is_clockwise(self.p2, other.p1, other.p2)
&& is_clockwise(self.p1, self.p2, other.p1) != is_clockwise(self.p1, self.p2, other.p2)
}

pub fn intersection(&self, other: &LineSegment) -> Option<Point> {
let intersection = self.line.intersection(&other.line);
intersection.filter(|intersection| {
Expand Down Expand Up @@ -192,6 +197,10 @@ impl LineSegment {
}
}

fn is_clockwise(a: Point, b: Point, c: Point) -> bool {
(c.y - a.y) * (b.x - a.x) <= (b.y - a.y) * (c.x - a.x)
}

pub fn between<T: Copy + PartialOrd>(num: T, a: T, b: T) -> bool {
let (min, max) = if a < b { (a, b) } else { (b, a) };
num >= min && num <= max
Expand Down
5 changes: 1 addition & 4 deletions rust/src/pathfinder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,6 @@ impl Pathfinder {
}

fn collides_with_wall(line: &LineSegment, wall: &LineSegment) -> bool {
if !line.bounding_rect().intersects(&wall.bounding_rect()) {
return false;
}
line.intersection(wall).is_some()
line.intersects(wall)
}
}

0 comments on commit 0368ddb

Please sign in to comment.