Different articles have discussed about the intersection of two line segments in Python such as
How do I compute the intersection point of two lines?,
How can I check if two segments intersect?
But, no one made it perfect since, they did not cover an especial case. Given the following two line segments:
a = [(0, 2), (2, 4)]
b = [(-2, 0), (0, 2)]
These two segment lines have the same slope. In fact, they intersect at (0, 2)
. How can we obtain such the intersection point?
The second part of my question, what if two line segments overlap (partially/totally)? That is,
a = [(0, 2), (2, 4)]
b = [(-2, 0), (1, 3)]
In your last reference, the first answer returns False if A1 == A2
due to the fact the lines are parallel. You present a legitimate edge case, so all you need to do in case the lines are parallel is to also check if they both lie on the same line. This is by checking if b1 == b2
. Only if this condition is False
, return False as the segments are parallel but do not lie on the same hyperplane. Otherwise, continue as the answer specifies, this is by checking if both segments has a common point.