pointInPolygon function

bool pointInPolygon(
  1. GmlPoint p,
  2. GmlPolygon poly
)

Whether a point lies inside a polygon (ray-casting algorithm).

Tests only the exterior ring. Interior rings (holes) are not considered.

Implementation

bool pointInPolygon(GmlPoint p, GmlPolygon poly) {
  final ring = poly.exterior.coordinates;
  if (ring.isEmpty) return false;
  final px = p.coordinate.x;
  final py = p.coordinate.y;
  var inside = false;
  for (var i = 0, j = ring.length - 1; i < ring.length; j = i++) {
    final xi = ring[i].x, yi = ring[i].y;
    final xj = ring[j].x, yj = ring[j].y;
    if ((yi > py) != (yj > py) &&
        px < (xj - xi) * (py - yi) / (yj - yi) + xi) {
      inside = !inside;
    }
  }
  return inside;
}