/// <summary> /// Computes whether the given point lies inside the specified polygon. /// The polygon is always cosidered closed, regardless of whether the last point equals /// the first or not. /// Inside is defined as not containing the South Pole -- the South Pole is always outside. /// The polygon is formed of great circle segments if geodesic is true, and of rhumb /// (loxodromic) segments otherwise. /// </summary> /// <param name="point"></param> /// <param name="polygon"></param> /// <param name="geodesic"></param> /// <returns></returns> public static bool ContainsLocation(Position point, IEnumerable <Position> polygon, bool geodesic) { int size = polygon.Count(); if (size == 0) { return(false); } double lat3 = point.Latitude.ToRadian(); double lng3 = point.Longitude.ToRadian(); Position prev = polygon.Last(); double lat1 = prev.Latitude.ToRadian(); double lng1 = prev.Longitude.ToRadian(); int nIntersect = 0; foreach (var point2 in polygon) { double dLng3 = GmsMathUtils.Wrap(lng3 - lng1, -Math.PI, Math.PI); // Special case: point equal to vertex is inside. if (lat3 == lat1 && dLng3 == 0) { return(true); } double lat2 = point2.Latitude.ToRadian(); double lng2 = point2.Longitude.ToRadian(); // Offset longitudes by -lng1. if (Intersects(lat1, lat2, GmsMathUtils.Wrap(lng2 - lng1, -Math.PI, Math.PI), lat3, dLng3, geodesic)) { ++nIntersect; } lat1 = lat2; lng1 = lng2; } return((nIntersect & 1) != 0); }
private static bool IsLocationOnEdgeOrPath(Position point, IEnumerable <Position> poly, bool closed, bool geodesic, double toleranceEarth) { int size = poly.Count(); if (size == 0) { return(false); } double tolerance = toleranceEarth / GmsMathUtils.EarthRadius; double havTolerance = GmsMathUtils.Hav(tolerance); double lat3 = point.Latitude.ToRadian(); double lng3 = point.Longitude.ToRadian(); Position prev = poly.ElementAt(closed ? size - 1 : 0); double lat1 = prev.Latitude.ToRadian(); double lng1 = prev.Longitude.ToRadian(); if (geodesic) { foreach (var point2 in poly) { double lat2 = point2.Latitude.ToRadian(); double lng2 = point2.Longitude.ToRadian(); if (IsOnSegmentGC(lat1, lng1, lat2, lng2, lat3, lng3, havTolerance)) { return(true); } lat1 = lat2; lng1 = lng2; } } else { // We project the points to mercator space, where the Rhumb segment is a straight line, // and compute the geodesic distance between point3 and the closest point on the // segment. This method is an approximation, because it uses "closest" in mercator // space which is not "closest" on the sphere -- but the error is small because // "tolerance" is small. double minAcceptable = lat3 - tolerance; double maxAcceptable = lat3 + tolerance; double y1 = GmsMathUtils.Mercator(lat1); double y3 = GmsMathUtils.Mercator(lat3); double[] xTry = new double[3]; foreach (var point2 in poly) { double lat2 = point2.Latitude.ToRadian(); double y2 = GmsMathUtils.Mercator(lat2); double lng2 = point2.Longitude.ToRadian(); if (Math.Max(lat1, lat2) >= minAcceptable && Math.Min(lat1, lat2) <= maxAcceptable) { // We offset longitudes by -lng1; the implicit x1 is 0. double x2 = GmsMathUtils.Wrap(lng2 - lng1, -Math.PI, Math.PI); double x3Base = GmsMathUtils.Wrap(lng3 - lng1, -Math.PI, Math.PI); xTry[0] = x3Base; // Also explore wrapping of x3Base around the world in both directions. xTry[1] = x3Base + 2 * Math.PI; xTry[2] = x3Base - 2 * Math.PI; foreach (var x3 in xTry) { double dy = y2 - y1; double len2 = x2 * x2 + dy * dy; double t = len2 <= 0 ? 0 : GmsMathUtils.Clamp((x3 * x2 + (y3 - y1) * dy) / len2, 0, 1); double xClosest = t * x2; double yClosest = y1 + t * dy; double latClosest = GmsMathUtils.InverseMercator(yClosest); double havDist = GmsMathUtils.HavDistance(lat3, latClosest, x3 - xClosest); if (havDist < havTolerance) { return(true); } } } lat1 = lat2; lng1 = lng2; y1 = y2; } } return(false); }