From 6ad402a39df907befc4cd8aa3c6a9b653bab409c Mon Sep 17 00:00:00 2001 From: kai-z99 Date: Wed, 29 May 2024 19:30:55 -0700 Subject: [PATCH] optimized dot product --- src/rshapes.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/rshapes.c b/src/rshapes.c index c34547dd1..6f25c3f09 100644 --- a/src/rshapes.c +++ b/src/rshapes.c @@ -2317,11 +2317,6 @@ bool CheckCollisionPointLine(Vector2 point, Vector2 p1, Vector2 p2, int threshol RLAPI bool CheckCollisionCircleLine(Vector2 center, float radius, Vector2 p1, Vector2 p2) { - //test if end points are in circle - bool inside1 = CheckCollisionPointCircle(p1, center, radius); - bool inside2 = CheckCollisionPointCircle(p2, center, radius); - if (inside1 || inside2) return true; - // get length of line float dx = p1.x - p2.x; float dy = p1.y - p2.y; @@ -2330,16 +2325,20 @@ RLAPI bool CheckCollisionCircleLine(Vector2 center, float radius, Vector2 p1, Ve // dot product float dotProduct = (((center.x - p1.x) * (p2.x - p1.x)) + ((center.y - p1.y) * (p2.y - p1.y))) / (length_sqr); + if (dotProduct > 1.0f) + { + dotProduct = 1.0f; + } + else if (dotProduct < 0.0f) + { + dotProduct = 0.0f; + } + // closest point on line float closeX = p1.x + (dotProduct * (p2.x - p1.x)); float closeY = p1.y + (dotProduct * (p2.y - p1.y)); - Vector2 close = { closeX, closeY }; - //is the point on the segment? - bool onSegment = CheckCollisionPointLine(close, p1, p2, 1); - if (!onSegment) return false; - //distnce from circle to closest point dx = closeX - center.x;