Add a function to know if two lines intersect each other and if yes, get the intersection point

This commit is contained in:
Elkantor 2020-12-21 13:27:55 +01:00
parent 51e75be9d1
commit 0982b52b98

View File

@ -336,6 +336,21 @@ RMDEF Vector2 Vector2MoveTowards(Vector2 v, Vector2 target, float maxDistance)
return result; return result;
} }
// Get the intersection point of two lines A and B defined by A(p1, p2) and B(p3, p4)
RMDEF Vector2 Vector2LineIntersect(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4)
{
Vector2 result = { 0 };
const float div = (p4.y - p3.y)*(p2.x - p1.x) - (p4.x -p3.x)*(p2.y - p1.y);
if (div == 0.f) return result;
const float coeff = ((p4.x - p3.x)*(p1.y - p3.y) - (p4.y - p3.y)*(p1.x - p3.x)) / div;
result.x = p1.x + (p2.x - p1.x) * coeff;
result.y = p1.y + (p2.y - p1.y) * coeff;
return result;
}
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Module Functions Definition - Vector3 math // Module Functions Definition - Vector3 math
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------