Rework the declaration of the 'Vector2LineIntersect' function, and add the 'Vector2SegmentIntersect' function
This commit is contained in:
parent
04a798bdf7
commit
db9d393fdd
|
|
@ -336,19 +336,43 @@ 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)
|
// Get the intersection point of two lines A and B defined by A(p1, p2) and B(p3, p4), return true if it exists, else false
|
||||||
RMDEF Vector2 Vector2LineIntersect(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4)
|
RMDEF _Bool Vector2 Vector2LineIntersect(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4, Vector2* pointIntersection)
|
||||||
{
|
{
|
||||||
Vector2 result = { 0 };
|
|
||||||
const float div = (p4.y - p3.y)*(p2.x - p1.x) - (p4.x -p3.x)*(p2.y - p1.y);
|
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;
|
if (div == 0.f) return false;
|
||||||
|
|
||||||
const float coeff = ((p4.x - p3.x)*(p1.y - p3.y) - (p4.y - p3.y)*(p1.x - p3.x)) / div;
|
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;
|
if (pointIntersection)
|
||||||
result.y = p1.y + (p2.y - p1.y) * coeff;
|
{
|
||||||
return result;
|
pointIntersection.x = p1.x + (p2.x - p1.x) * coeff;
|
||||||
|
pointIntersection.y = p1.y + (p2.y - p1.y) * coeff;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the intersection point of two segments A and B defined by A(p1, p2) and B(P3, p4), return true if it exists, else false
|
||||||
|
RMDEF _Bool Vector2SegmentIntersect(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4, Vector2* pointIntersection)
|
||||||
|
{
|
||||||
|
const float div = (p4.y - p3.y)*(p2.x - p1.x) - (p4.x -p3.x)*(p2.y - p1.y);
|
||||||
|
|
||||||
|
if (div == 0.f) return false;
|
||||||
|
|
||||||
|
const float xi = ((p3.x - p3.x)*(p1.x * p2.y - p1.y * p2.x) - (p1.x - p2.x)*(p3.x * p4.y - p3.y * p4.x)) / div;
|
||||||
|
const float yi = ((p3.y - p4.y)*(p1.x * p2.y - p1.y * p2.x) - (p1.y - p2.y)*(p3.x * p4.y - p3.y * p4.x)) / div;
|
||||||
|
|
||||||
|
if (xi < fminf(x1, x2) || xi > fmaxf(x1, x2)) return false;
|
||||||
|
if (xi < fminf(x3, x4) || xi > fmaxf(x3, x4)) return false;
|
||||||
|
if (yi < fminf(y1, y2) || yi > fmaxf(y1, y2)) return false;
|
||||||
|
if (yi < fminf(y3, y4) || yi > fmaxf(y3, y4)) return false;
|
||||||
|
if (pointIntersection)
|
||||||
|
{
|
||||||
|
pointIntersection->x = xi;
|
||||||
|
pointIntersection->y = yi;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user