IsInPolygon function

bool IsInPolygon(
  1. List<Vector2> contour,
  2. Vector2 p
)

Implementation

bool IsInPolygon(final List<VM.Vector2> contour, final VM.Vector2 p) {
  VM.Vector2 a;
  VM.Vector2 b = contour.last - p;
  int depth = 0;
  for (final VM.Vector2 v in contour) {
    a = b;
    b = v - p;
    if (a.y < 0 && b.y < 0) continue; // both "up" or both "down"
    if (a.y > 0 && b.y > 0) continue; // both "up" or both "down"
    if (a.x < 0 && b.x < 0) continue; // both points on left

    double lx = a.x - a.y * (b.x - a.x) / (b.y - a.y);

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