How do I check (in MATLAB) whether two finite line segments, L1 (drawn between points (x1,y1) and (x2,y2)) and L2 (drawn between points (x3,y3) and (x4,y4)), intersect with each other? Please note that intersection point is not needed, Only boolean answer (yes or no).
0
votes
How are the points represented? Are they given as x,y?
- David.Jones
@rayryeng: The above question (already answered) is to find point of intersection (x,y) of two lines. But my question is to check whether two lines intersect or not (just boolean answer yes or no). I tried my best on S.O., but not found a duplicate? Please remove duplicate tag, if you agree.
- erbal
@DavidJones: Just edited the question.
- erbal
@user11659 - I agree as well. I misinterpreted your question. It has been reopen!
- rayryeng
@user11659 - Check this post: blogs.mathworks.com/loren/2011/08/29/intersecting-lines/…
- rayryeng
1 Answers
1
votes
I found an answer (2nd last) by Roger Stafford at MatlabCentral. Here it is:
The two line segments L1 and L2 will intersect if and only if det([1,1,1;x1,x2,x3;y1,y2,y3])*det([1,1,1;x1,x2,x4;y1,y2,y4]) <= 0 and det([1,1,1;x1,x3,x4;y1,y3,y4])*det([1,1,1;x2,x3,x4;y2,y3,y4]) <= 0, MATLAB coding is:
x=[x1 x2 x3 x4];
y=[y1 y2 y3 y4];
dt1=det([1,1,1;x(1),x(2),x(3);y(1),y(2),y(3)])*det([1,1,1;x(1),x(2),x(4);y(1),y(2),y(4)]);
dt2=det([1,1,1;x(1),x(3),x(4);y(1),y(3),y(4)])*det([1,1,1;x(2),x(3),x(4);y(2),y(3),y(4)]);
if(dt1<=0 & dt2<=0)
intrsct=1 %If lines intesect
else
intrsct=0
end