containsPoint method

  1. @override
bool containsPoint(
  1. Vector2 point
)
override

Checks whether the polygon contains the point. Note: The polygon needs to be convex for this to work.

Implementation

@override
bool containsPoint(Vector2 point) {
  // If the size is 0 then it can't contain any points
  if (size.x == 0 || size.y == 0) {
    return false;
  }

  final vertices = globalVertices();
  for (var i = 0; i < vertices.length; i++) {
    final edge = getEdge(i, vertices: vertices);
    final isOutside = (edge.to.x - edge.from.x) * (point.y - edge.from.y) -
            (point.x - edge.from.x) * (edge.to.y - edge.from.y) >
        0;
    if (isOutside) {
      // Point is outside of convex polygon
      return false;
    }
  }
  return true;
}