contains method

bool contains(
  1. num px,
  2. num py
)

returns true if (x,y) is present inside Polygon

Implementation

bool contains(num px, num py) {
  num ax = 0;
  num ay = 0;
  num bx = points[points.length - 1].dx - px;
  num by = points[points.length - 1].dy - py;
  int depth = 0;

  for (int i = 0; i < points.length; i++) {
    ax = bx;
    ay = by;
    bx = points[i].dx - px;
    by = points[i].dy - py;

    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;
}