contains method

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

Implementation

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

  for (var 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

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

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

  return (depth & 1) == 1;
}