containsLocationPoly static method

bool containsLocationPoly(
  1. Point<num> point,
  2. List<Point<num>> polygon
)

Checks if point is inside polygon

Implementation

static bool containsLocationPoly(Point point, List<Point> polygon) {
  num ax = 0;
  num ay = 0;
  num bx = polygon[polygon.length - 1].x - point.x;
  num by = polygon[polygon.length - 1].y - point.y;
  int depth = 0;

  for (int i = 0; i < polygon.length; i++) {
    ax = bx;
    ay = by;
    bx = polygon[i].x - point.x;
    by = polygon[i].y - point.y;

    if (ay < 0 && by < 0) continue; // both "up" or both "down"
    if (ay > 0 && by > 0) continue; // both "up" or both "down"
    if (ax < 0 && bx < 0) continue; // both points on left

    num lx = ax - ay * (bx - ax) / (by - ay);

    if (lx == 0) return true; // point on edge
    if (lx > 0) depth++;
  }

  return (depth & 1) == 1;
}