parry icon indicating copy to clipboard operation
parry copied to clipboard

parry2d::utils::point_in_poly2d gives false negatives

Open kbjakex opened this issue 2 years ago • 2 comments

The documentation says:

Tests if the given point is inside of a polygon with arbitrary orientation.

which sounds like it should work with any arbitrary polygon. However, in my application, I have non-convex polygons I tested against, and while there are no false positives, it leaves parts of the polygon out: Screenshot from 2022-08-25 14-37-10 (the tracing is inexact, I'm just spawning a dot whenever the mouse is detected to be inside the polygon)

Based on the output I'd guess it only works for convex polygons (although I don't understand the algorithm), in which case it would be nice to have a mention of this in the docs.

kbjakex avatar Aug 25 '22 11:08 kbjakex

I was having a similar problem where I was defining a list of points. Sometimes the method would return true but it mostly return false. Turns out my points were getting jumbled up. I think this only works if the points are defined in sequence clockwise or counter clockwise. If your points are "zig zagged" then it won't work.

If the method should handle concave and convex shapes, this makes sense because changing the sequence of points creates a different shape.

image

By arbitrary, I think they meant the ordering doesn't matter. For example, the following sequences all form the same polygon:

[[ 1, 1], [-1, 1], [-1,-1], [ 1,-1]]
[[-1, 1], [-1,-1], [ 1,-1], [ 1, 1]]
[[-1,-1], [ 1,-1], [ 1, 1], [-1, 1]]
[[ 1,-1], [ 1, 1], [-1, 1], [-1,-1]]

ZebTheWizard avatar Sep 01 '22 04:09 ZebTheWizard

This function only supports convex polylines, yeah. We should specify that in the docs. For non-convex (non-self-intersecting) polylines, you can use Polyline::project_local_point_assuming_solid_interior_ccw](https://docs.rs/parry2d/0.9.0/parry2d/shape/struct.Polyline.html#method.project_local_point_assuming_solid_interior_ccw). This method requires the polyline’s orientation to be counterclockwise.

sebcrozet avatar Sep 01 '22 10:09 sebcrozet