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

Bugfix/point in polygon #1894

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions documentation/docs/05-changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### Bug Fixes
- Fixes a bug where add collider feature didn't work for flat terrain mesh.
- Fix a bug where vector tile processing fired multiple times depending on Terrain/Image data states
- Fixed incorrect computation of PointInPolygon when point was near center of polygon
### Improvements
- Improves Directions factory and prefabs to provide better UX and support for all types of maps

Expand Down
47 changes: 7 additions & 40 deletions sdkproject/Assets/Mapbox/Unity/Utilities/PolygonUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,54 +17,21 @@ public static class PolygonUtils
/// <param name="p">The point that is to be tested.</param>
public static bool PointInPolygon(Point2d<float> p, List<List<Point2d<float>>> polygon)
{
List<Point2d<float>> poly = polygon[0];

Point2d<float> p1;
Point2d<float> p2;
bool inside = false;

if (poly.Count < 3)
{
return inside;
}

var oldPoint = new Point2d<float>(
poly[poly.Count - 1].X
, poly[poly.Count - 1].Y
);

int j = poly.Count - 1;
for (int i = 0; i < poly.Count; i++)
{
var newPoint = new Point2d<float>(poly[i].X, poly[i].Y);

if (newPoint.X > oldPoint.X)
{
p1 = oldPoint;
p2 = newPoint;
}
else
if (poly[i].Y < p.Y && poly[j].Y >= p.Y || poly[j].Y < p.Y && poly[i].Y >= p.Y)
{
p1 = newPoint;
p2 = oldPoint;
if (poly[i].X + (p.Y - poly[i].Y) / (poly[j].Y - poly[i].Y) * (poly[j].X - poly[i].X) < p.X)
{
inside = !inside;
}
}

if (
(newPoint.X < p.X) == (p.X <= oldPoint.X)
&& (p.Y - (long)p1.Y) * (p2.X - p1.X) < (p2.Y - (long)p1.Y) * (p.X - p1.X)
)
{
inside = !inside;
}

oldPoint = newPoint;
j = i;
}

return inside;
}



}

}