diff options
Diffstat (limited to 'src/math/polygon/mod.rs')
| -rw-r--r-- | src/math/polygon/mod.rs | 31 |
1 files changed, 19 insertions, 12 deletions
diff --git a/src/math/polygon/mod.rs b/src/math/polygon/mod.rs index e19e097..cc89169 100644 --- a/src/math/polygon/mod.rs +++ b/src/math/polygon/mod.rs @@ -42,7 +42,15 @@ impl<T: Scalar + Copy> Polygon<T> { } impl< - T: Scalar + Copy + ClosedSub + ClosedMul + ClosedDiv + Neg<Output = T> + PartialOrd + Zero, + T: Scalar + + Copy + + ClosedSub + + ClosedMul + + ClosedDiv + + Neg<Output = T> + + PartialOrd + + RealField + + Zero, > Surface<T> for Polygon<T> { fn contains_point(&self, p: &Vec2<T>) -> bool { @@ -145,18 +153,11 @@ impl< let prev = (c + self.corners.len() - 1) % self.corners.len(); let next = (c + 1) % self.corners.len(); - let last_edge_orientation = - math::triplet_orientation(self.corners[prev], self.corners[c], p); - let current_edge_orientation = - math::triplet_orientation(self.corners[c], self.corners[next], p); + let edge_angle = + math::triplet_angle(self.corners[prev], self.corners[c], self.corners[next]); + let vec_angle = math::triplet_angle(self.corners[prev], self.corners[c], p); - if last_edge_orientation == TripletOrientation::Clockwise - && current_edge_orientation == TripletOrientation::Clockwise - { - false - } else { - true - } + vec_angle == T::zero() || vec_angle >= edge_angle }; for c in 0..self.corners.len() { @@ -297,6 +298,12 @@ mod test { assert!( polygon.contains_line_segment(&LineSegment::new(Vec2::new(2., 0.5), Vec2::new(4., 0.))) ); + + // Start and end point on vertex, not pointing in the dir of adjacent edge normals, + // not completely inside. + assert!( + !polygon.contains_line_segment(&LineSegment::new(Vec2::new(4., 2.), Vec2::new(0., 0.))) + ); } #[test] |
