pointInPolygon function

bool pointInPolygon(
  1. DiagramPoint point,
  2. List<DiagramPoint> polygon
)

Even-odd containment of point in the closed polygon.

Implementation

bool pointInPolygon(DiagramPoint point, List<DiagramPoint> polygon) {
  var inside = false;
  for (
    var current = 0, previous = polygon.length - 1;
    current < polygon.length;
    previous = current++
  ) {
    final a = polygon[current];
    final b = polygon[previous];
    if ((a.y > point.y) != (b.y > point.y) &&
        point.x < (b.x - a.x) * (point.y - a.y) / (b.y - a.y) + a.x) {
      inside = !inside;
    }
  }
  return inside;
}