DarkRewar / BaseTool

A big library of basic tools that you might need in your Unity projects.
MIT License
41 stars 6 forks source link

Added a `IsPointInsidePolygon` method #109

Closed DarkRewar closed 4 weeks ago

DarkRewar commented 4 weeks ago

IsPointInsidePolygon(Vector2 point, Vector2[] polygon)

Check if a point is inside a polygon (determined by a list of Vector2). Also exists for Vector2Int by using IsPointInsidePolygon(Vector2Int point, Vector2Int[] polygon).

using BaseTool;

List<Vector2> square = new()
{
   new(0, 0),
   new(0, 1),
   new(1, 1),
   new(1, 0)
};

MathUtils.IsPointInsidePolygon(new Vector2(0.5f, 0.5f), points)); // true
MathUtils.IsPointInsidePolygon(new Vector2(-1, -1), points)); // false

Note: currently, there are no Vector3 equivalent.

closes #108