03.07.2013, 13:31
This function checks if two line segments intersect.
pawn Код:
/*
Function:
SegmentsIntersect
Parameters:
Float:x1 - The X start point of the first line segment.
Float:y1 - The Y start point of the first line segment.
Float:x2 - The X end point of the first line segment.
Float:y2 - The Y end point of the first line segment.
Float:x3 - The X start point of the second line segment.
Float:y3 - The Y start point of the second line segment.
Float:x4 - The X end point of the second line segment.
Float:y4 - The Y end point of the second line segment.
Float:x - A float to store the X intersection point in, passed by reference.
Float:y - A float to store the Y intersection point in, passed by reference.
Returns:
0 - line segments do not intersect
1 - line segments intersect
*/
stock SegmentsIntersect(Float:x1, Float:y1, Float:x2, Float:y2, Float:x3, Float:y3, Float:x4, Float:y4, &Float:x, &Float:y)
{
new
Float:xA = x2 - x1,
Float:yA = y2 - y1,
Float:xB = x4 - x3,
Float:yB = y4 - y3,
Float:d = xA * yB - yA * xB;
if (!d)
{
// Lines are parallel, or one or both segments are zero-length
return 0;
}
new
Float:xC = x3 - x1,
Float:yC = y3 - y1,
Float:pA = (xC * yB - yC * xB) / d,
Float:pB = (xC * yA - yC * xA) / d;
if (pA < 0 || pA > 1 || pB < 0 || pB > 1)
{
return 0;
}
// Compute the intersection point
x = x1 + pA * xA
y = y1 + pA * yA
return 1;
}