/// <summary> /// Returns sin(initial bearing from (lat1,lng1) to (lat3,lng3) minus initial bearing /// from (lat1, lng1) to (lat2,lng2)). /// </summary> /// <param name="lat1"></param> /// <param name="lng1"></param> /// <param name="lat2"></param> /// <param name="lng2"></param> /// <param name="lat3"></param> /// <param name="lng3"></param> /// <returns></returns> private static double SinDeltaBearing(double lat1, double lng1, double lat2, double lng2, double lat3, double lng3) { double sinLat1 = Math.Sin(lat1); double cosLat2 = Math.Cos(lat2); double cosLat3 = Math.Cos(lat3); double lat31 = lat3 - lat1; double lng31 = lng3 - lng1; double lat21 = lat2 - lat1; double lng21 = lng2 - lng1; double a = Math.Sin(lng31) * cosLat3; double c = Math.Sin(lng21) * cosLat2; double b = Math.Sin(lat31) + 2 * sinLat1 * cosLat3 * GmsMathUtils.Hav(lng31); double d = Math.Sin(lat21) + 2 * sinLat1 * cosLat2 * GmsMathUtils.Hav(lng21); double denom = (a * a + b * b) * (c * c + d * d); return(denom <= 0 ? 1 : (a * d - b * c) / Math.Sqrt(denom)); }
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); }