isPointInPolygon static method

bool isPointInPolygon(
  1. Offset point,
  2. List<Offset> polygon
)

Checks if a point is inside a polygon.

point is the point to check. polygon is the list of polygon points.

Implementation

static bool isPointInPolygon(Offset point, List<Offset> polygon) {
  if (polygon.length < 3) return false;

  bool inside = false;
  for (int i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
    if (((polygon[i].dy > point.dy) != (polygon[j].dy > point.dy)) &&
        (point.dx <
            (polygon[j].dx - polygon[i].dx) *
                    (point.dy - polygon[i].dy) /
                    (polygon[j].dy - polygon[i].dy) +
                polygon[i].dx)) {
      inside = !inside;
    }
  }

  return inside;
}