-
Notifications
You must be signed in to change notification settings - Fork 214
/
Copy pathPolygonUtils.cs
37 lines (32 loc) · 1 KB
/
PolygonUtils.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
namespace Mapbox.Utils
{
using System.Collections.Generic;
using Mapbox.VectorTile.Geometry;
public static class PolygonUtils
{
/// <summary>
/// <para>Method to check if a point is contained inside a polygon, ignores vertical axis (y axis)</para>
/// <para>from https://stackoverflow.com/a/7123291</para>
/// </summary>
/// <returns><c>true</c>, if point lies inside the constructed polygon, <c>false</c> otherwise.</returns>
/// <param name="polygon">Polygon points.</param>
/// <param name="p">The point that is to be tested.</param>
public static bool PointInPolygon(Point2d<float> p, List<List<Point2d<float>>> polygon)
{
bool inside = false;
int j = poly.Count - 1;
for (int i = 0; i < poly.Count; i++)
{
if (poly[i].Y < p.Y && poly[j].Y >= p.Y || poly[j].Y < p.Y && poly[i].Y >= p.Y)
{
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;
}
}
j = i;
}
return inside;
}
}
}