intersects static method

bool intersects(
  1. num lat1,
  2. num lat2,
  3. num lng2,
  4. num lat3,
  5. num lng3,
  6. bool geodesic,
)

intersects the segment (lat1, lng1) to (lat2, lng2). Longitudes are offset by -lng1; the implicit lng1 becomes 0.

Implementation

static bool intersects(num lat1, num lat2, num lng2, num lat3, num 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 <= -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
  // MathUtil.mercator()) as convenient.
  return geodesic ? tan(lat3) >= _tanLatGC(lat1, lat2, lng2, lng3) : mercator(lat3) >= _mercatorLatRhumb(lat1, lat2, lng2, lng3);
}