It is known that the two endpoints of a line segment are a (x1, Y1), B (X2, Y2), and the two endpoints of the other line segment are C (X3, Y3), D (X4, y4), to determine whether there is a point between AB and Cd, if any.
First Judge d = (y2-y1) (x4-x3)-(y4-y3) (x2-x1 ),
If D = 0, the line AB and Cd are parallel or overlapped,
If D! = 0, the line AB and CD have the intersection, and the intersection (x0, y0) is:
X0 = [(x2-x1) * (x4-x3) * (y3-y1) + (y2-y1) * (x4-x3) * x1-(y4-y3) * (x2-x1) * X3]/d
X0 = [(y2-y1) * (y4-y3) * (x3-x1) + (x2-x1) * (y4-y3) * Y1-(x4-x3) * (y2-y1) * Y3]/(-d)
After finding the intersection, determine whether the intersection is on the online segment, that is, determine the following formula:
(X0-x1) * (x0-x2) <= 0
(X0-x3) * (x0-x4) <= 0
(Y0-y1) * (y0-y2) <= 0
(Y0-y3) * (y0-y4) <= 0
Only when the four formulas above are vertical can we determine that (x0, y0) is the intersection of line AB and Cd, otherwise there is no intersection between the two lines
========================================================== ======================================
// Function: Find the point on the left or right of a straight line.
// Return value: 0, 1, left,-1, and right
Int left_right (point a, point B, double X, Double Y)
{
Double T;
A. X-= x; B. X-= X;
A. Y-= y; B. Y-= y;
T = A. x * B. y-a.y * B. X;
Return T = 0? 0: T> 0? 1:-1;
}
// Function: whether the line segments C, D, and a and B intersect
Bool intersect1 (point a, point B, point C, point D)
{
Return left_right (A, B, C. X, C. Y) ^ left_right (A, B, D. x, D. y) =-2;
}
// Function: determines whether the segments C, D, and A and B are intersecting.
Bool intersect (point a, point B, point C, point D)
{
Return intersect1 (A, B, C, D) & intersect (c, d, A, B );
}