containsPoint method

bool containsPoint(
  1. double latitude,
  2. double longitude
)

Returns true if the given point is inside this polygon.

Uses the ray casting algorithm (point-in-polygon test). A ray is cast from the point to infinity and the number of intersections with polygon edges is counted. Odd = inside.

Implementation

bool containsPoint(double latitude, double longitude) {
  if (vertices.length < 3) return false;

  // Quick bounding box check first
  final bbox = boundingBox;
  if (latitude < bbox[0] ||
      latitude > bbox[2] ||
      longitude < bbox[1] ||
      longitude > bbox[3]) {
    return false;
  }

  // Ray casting algorithm
  bool inside = false;
  final n = vertices.length;

  for (int i = 0, j = n - 1; i < n; j = i++) {
    final yi = vertices[i].latitude;
    final xi = vertices[i].longitude;
    final yj = vertices[j].latitude;
    final xj = vertices[j].longitude;

    if (((yi > latitude) != (yj > latitude)) &&
        (longitude < (xj - xi) * (latitude - yi) / (yj - yi) + xi)) {
      inside = !inside;
    }
  }

  return inside;
}