isPointInsidePolygon method

bool isPointInsidePolygon(
  1. double x,
  2. double y,
  3. PolygonBean polygonBean
)

Determine if the click is inside the polygon body

Implementation

bool isPointInsidePolygon(
    double x, double y, PolygonBean polygonBean) {
  bool isPointInsideArea = false;
  int intersectCount = 0;
  if (polygonBean.polygonPoint.length < 3) {
    throw Exception('Polygon must have at least 3 points.');
  }
  for (int i = 0; i < polygonBean.polygonPoint.length; i++) {
    int nextIndex = (i + 1) % polygonBean.polygonPoint.length;
    Point localPoint = polygonBean.polygonPoint[i];
    Point nextPoint = polygonBean.polygonPoint[nextIndex];
    if (localPoint.Y > y && nextPoint.Y <= y ||
        nextPoint.Y > y && localPoint.Y <= y) {
      double intersectX = (y - localPoint.Y) /
          (nextPoint.Y - localPoint.Y) *
          (nextPoint.X - localPoint.X) +
          localPoint.X;
      if (x <= intersectX) {
        intersectCount++;
      }
    }
  }
  if (intersectCount % 2 == 1) {
    isPointInsideArea = true;
  } else {
    isPointInsideArea = false;
  }
  debugPrint(
      '_isPointInsidePolygon----isPointInsideArea, $isPointInsideArea');

  return isPointInsideArea;
}