How can I check if 2 segments intersect?
I’ve the following data:
Segment1 [ {x1,y1}, {x2,y2} ]
Segment2 [ {x1,y1}, {x2,y2} ]
I need to write a small algorithm in Python to detect if the 2 lines are intersecting.

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The equation of a line is:
For a segment, it is exactly the same, except that x is included on an interval I.
If you have two segments, defined as follow:
The abcisse Xa of the potential point of intersection (Xa,Ya) must be contained in both interval I1 and I2, defined as follow :
And we could say that Xa is included into :
Now, we need to check that this interval Ia exists :
So, we have two line formula, and a mutual interval. Your line formulas are:
As we got two points by segment, we are able to determine A1, A2, b1 and b2:
If the segments are parallel, then A1 == A2 :
A point (Xa,Ya) standing on both line must verify both formulas f1 and f2:
The last thing to do is check that Xa is included into Ia:
In addition to this, you may check at startup that two of the four provided points are not equals to avoid all that testing.