namespace Mapbox.Utils { using System.Collections.Generic; using Mapbox.VectorTile.Geometry; public static class PolygonUtils { /// /// Method to check if a point is contained inside a polygon, ignores vertical axis (y axis) /// from https://stackoverflow.com/a/7123291 /// /// true, if point lies inside the constructed polygon, false otherwise. /// Polygon points. /// The point that is to be tested. public static bool PointInPolygon(Point2d p, List>> polygon) { List> poly = polygon[0]; Point2d p1; Point2d p2; bool inside = false; if (poly.Count < 3) { return inside; } var oldPoint = new Point2d( poly[poly.Count - 1].X , poly[poly.Count - 1].Y ); for (int i = 0; i < poly.Count; i++) { var newPoint = new Point2d(poly[i].X, poly[i].Y); if (newPoint.X > oldPoint.X) { p1 = oldPoint; p2 = newPoint; } else { p1 = newPoint; p2 = oldPoint; } 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; } return inside; } } }