Check collision point polygon (#2685)

* Update raylib.h

* CheckCollisionPointPolygon()

* typo
This commit is contained in:
Jacek
2022-09-04 10:39:03 +02:00
committed by GitHub
parent 082920eb80
commit aff98d7f2a
2 changed files with 23 additions and 0 deletions

View File

@@ -1631,6 +1631,28 @@ bool CheckCollisionPointTriangle(Vector2 point, Vector2 p1, Vector2 p2, Vector2
return collision;
}
// Check if point is within a polygon described by array of vertices
bool CheckCollisionPointPolygon(Vector2 point, Vector2* vertices, int verticesCount)
{
// http://jeffreythompson.org/collision-detection/poly-point.php
bool collision = false;
if (verticesCount > 2)
{
for (int i = 0; i < verticesCount; i++)
{
Vector2 vc = vertices[i];
Vector2 vn = vertices[i + 1];
if ((((vc.y >= point.y) && (vn.y < point.y)) || ((vc.y < point.y) && (vn.y >= point.y))) &&
(point.x < ((vn.x - vc.x)*(point.y - vc.y)/(vn.y - vc.y) + vc.x))) collision = !collision;
}
}
return collision;
}
// Check collision between two rectangles
bool CheckCollisionRecs(Rectangle rec1, Rectangle rec2)
{