From 4a6cef40155af6d191c87448ae258c846a2f35d4 Mon Sep 17 00:00:00 2001 From: Jacek Date: Sat, 3 Sep 2022 16:14:39 +0200 Subject: [PATCH] CheckCollisionPointPolygon() --- src/rshapes.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/rshapes.c b/src/rshapes.c index 955f056d4..8018bbdef 100644 --- a/src/rshapes.c +++ b/src/rshapes.c @@ -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) {