diff --git a/rust/src/geometry.rs b/rust/src/geometry.rs index b5288bb..ecaca4e 100644 --- a/rust/src/geometry.rs +++ b/rust/src/geometry.rs @@ -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 { let intersection = self.line.intersection(&other.line); intersection.filter(|intersection| { @@ -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(num: T, a: T, b: T) -> bool { let (min, max) = if a < b { (a, b) } else { (b, a) }; num >= min && num <= max diff --git a/rust/src/pathfinder.rs b/rust/src/pathfinder.rs index 6316985..c4fc406 100644 --- a/rust/src/pathfinder.rs +++ b/rust/src/pathfinder.rs @@ -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) } }