intersects static method

bool intersects(
  1. double lat1,
  2. double lat2,
  3. double lng2,
  4. double lat3,
  5. double lng3, [
  6. bool geodesic = false,
])

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.

Implementation

static bool intersects(
    double lat1, double lat2, double lng2, double lat3, double lng3,
    [bool geodesic = false]) {
  // 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 <= -pi / 2) {
    return false;
  }
  // Any segment end is a pole.
  if (lat1 <= -pi / 2 ||
      lat2 <= -pi / 2 ||
      lat1 >= pi / 2 ||
      lat2 >= pi / 2) {
    return false;
  }
  if (lng2 <= -pi) {
    return false;
  }
  final 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 >= pi / 2) {
    return true;
  }
  // Compare lat3 with latitude on the GC/Rhumb segment corresponding to lng3.
  // Compare through a strictly-increasing function (tan() or mercator()) as convenient.
  return geodesic
      ? tan(lat3) >= _tanLatGC(lat1, lat2, lng2, lng3)
      : _mercator(lat3) >= _mercatorLatRhumb(lat1, lat2, lng2, lng3);
}