containsPoint method

bool containsPoint(
  1. Offset point
)

Check if point is inside lasso polygon using ray casting

Implementation

bool containsPoint(Offset point) {
  if (points.length < 3) return false;

  int intersections = 0;
  for (int i = 0; i < points.length; i++) {
    final p1 = points[i];
    final p2 = points[(i + 1) % points.length];

    if ((p1.dy <= point.dy && p2.dy > point.dy) || (p2.dy <= point.dy && p1.dy > point.dy)) {
      final xIntersect = (point.dy - p1.dy) / (p2.dy - p1.dy) * (p2.dx - p1.dx) + p1.dx;
      if (point.dx < xIntersect) {
        intersections++;
      }
    }
  }
  return intersections.isOdd;
}