This commit is contained in:
kai-z99 2024-05-29 19:00:37 -07:00
parent 747f9d9f1b
commit ba934b16c1
2 changed files with 6 additions and 6 deletions

View File

@ -32,7 +32,7 @@ int main(void)
//---------------------------------------------------------- //----------------------------------------------------------
Vector2 p0 = { 0,0 }; Vector2 p0 = { 100,100 };
Vector2 p1 = { 300,300 }; Vector2 p1 = { 300,300 };

View File

@ -2325,18 +2325,18 @@ RLAPI bool CheckCollisionCircleLine(Vector2 center, float radius, Vector2 p1, Ve
// get length of line // get length of line
float dx = p1.x - p2.x; float dx = p1.x - p2.x;
float dy = p1.y - p2.y; float dy = p1.y - p2.y;
float length = sqrtf((dx * dx) + (dy * dy)); float length_sqr = ((dx * dx) + (dy * dy));
// dot product // dot product
float dotProduct = (((center.x - p1.x) * (p2.x - p1.x)) + ((center.y - p1.y) * (p2.y - p1.y))) / (length * length); float dotProduct = (((center.x - p1.x) * (p2.x - p1.x)) + ((center.y - p1.y) * (p2.y - p1.y))) / (length_sqr);
// closest point on line // closest point on line
float closeX = p1.x + (dotProduct * (p2.x - p1.x)); float closeX = p1.x + (dotProduct * (p2.x - p1.x));
float closeY = p1.y + (dotProduct * (p2.y - p1.y)); float closeY = p1.y + (dotProduct * (p2.y - p1.y));
Vector2 close = { closeX, closeY }; Vector2 close = { closeX, closeY };
//is the point on the segment?
bool onSegment = CheckCollisionPointLine(close, p1, p2, 1); bool onSegment = CheckCollisionPointLine(close, p1, p2, 1);
if (!onSegment) return false; if (!onSegment) return false;
@ -2344,9 +2344,9 @@ RLAPI bool CheckCollisionCircleLine(Vector2 center, float radius, Vector2 p1, Ve
dx = closeX - center.x; dx = closeX - center.x;
dy = closeY - center.y; dy = closeY - center.y;
float distance = sqrtf((dx * dx) + (dy * dy)); float distance_sqr = ((dx * dx) + (dy * dy));
if (distance <= radius) return true; if (distance_sqr <= radius * radius) return true;
return false; return false;
} }