bool contains(num px, num py)

Source

bool contains(num px, num py) {

  num ax = 0;
  num ay = 0;
  num bx = points[points.length - 1].x - px;
  num by = points[points.length - 1].y - py;
  int depth = 0;

  for(int i = 0; i < points.length; i++) {

    ax = bx;
    ay = by;
    bx = points[i].x - px;
    by = points[i].y - 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;
}