/// <summary> /// Computes whether the vertical segment (lat3, lng3) to South Pole intersects the segment /// (lat1, lng1) to (lat2, lng2). /// Longitudes are offset by -lng1; the implicit lng1 becomes 0. /// </summary> /// <param name="lat1"></param> /// <param name="lat2"></param> /// <param name="lng2"></param> /// <param name="lat3"></param> /// <param name="lng3"></param> /// <param name="geodesic"></param> /// <returns></returns> private static bool Intersects(double lat1, double lat2, double lng2, double lat3, double lng3, bool geodesic) { // Both ends on the same side of lng3. if ((lng3 >= 0 && lng3 >= lng2) || (lng3 < 0 && lng3 < lng2)) { return(false); } // Point is South Pole. if (lat3 <= -Math.PI / 2) { return(false); } // Any segment end is a pole. if (lat1 <= -Math.PI / 2 || lat2 <= -Math.PI / 2 || lat1 >= Math.PI / 2 || lat2 >= Math.PI / 2) { return(false); } if (lng2 <= -Math.PI) { return(false); } double linearLat = (lat1 * (lng2 - lng3) + lat2 * lng3) / lng2; // Northern hemisphere and point under lat-lng line. if (lat1 >= 0 && lat2 >= 0 && lat3 < linearLat) { return(false); } // Southern hemisphere and point above lat-lng line. if (lat1 <= 0 && lat2 <= 0 && lat3 >= linearLat) { return(true); } // North Pole. if (lat3 >= Math.PI / 2) { return(true); } // Compare lat3 with latitude on the GC/Rhumb segment corresponding to lng3. // Compare through a strictly-increasing function (Math.Tan() or mercator()) as convenient. return(geodesic ? Math.Tan(lat3) >= TanLatGC(lat1, lat2, lng2, lng3) : GmsMathUtils.Mercator(lat3) >= MercatorLatRhumb(lat1, lat2, lng2, lng3)); }
/// <summary> /// Returns mercator(latitude-at-lng3) on the Rhumb line (lat1, lng1) to (lat2, lng2). lng1==0. /// </summary> /// <param name="lat1"></param> /// <param name="lat2"></param> /// <param name="lng2"></param> /// <param name="lng3"></param> /// <returns></returns> private static double MercatorLatRhumb(double lat1, double lat2, double lng2, double lng3) { return((GmsMathUtils.Mercator(lat1) * (lng2 - lng3) + GmsMathUtils.Mercator(lat2) * lng3) / lng2); }
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); }