Get intersection of two segments

Hi,
I have these two following matrices:
Can you help me with some way to check if every line from segmentBA intersects every line from obstacleMatrix?
Every line in both matrices represent a segment defined as: [x1 y1 x2 y2] (x1,y1)----(x2,y2)
I don't want to know exactly the intersection point, just only true if the intersection exists or false.

댓글 수: 2

darova
darova 2020년 3월 23일
for loop and polyxpoly? Did you think about this?
pb1106
pb1106 2020년 3월 23일
Well, I am aware of the function but I want the code to be adaptable to other programming language because I want this code to be integrated in a control algorithm for a 2 DOF robot.
So, if you have some idea without polyxpoly I will be grateful

댓글을 달려면 로그인하십시오.

답변 (2개)

darova
darova 2020년 3월 23일

1 개 추천

Here is a script
It solves line-line intersection (x1,y1) - line1, (x2,y2) - line2
a1 = (y1(i+1)-y1(i)) / (x1(i+1) - x1(i));
b1 = y1(i) - a1*x1(i);
a2 = (y2(j+1)-y2(j)) / (x2(j+1) - x2(j));
b2 = y2(j) - a2*x2(j);
X = (b1-b2)/(a2-a1);
Then just checks if intersection point lies on both lines
cond1 = x1(i) < X && X < x1(i+1);
cond2 = x1(i) > X && X > x1(i+1);
cond3 = x2(j) < X && X < x2(j+1);
cond4 = x2(j) > X && X > x2(j+1);
if ( cond1 || cond2 ) && ( cond3 || cond4 )
% do stuff
end

댓글 수: 1

Matthew Boyd
Matthew Boyd 2023년 6월 2일
Does this work for all line segment - line segment intersection? It does not assume infinite lines right?

댓글을 달려면 로그인하십시오.

Image Analyst
Image Analyst 2020년 3월 23일

0 개 추천

Are the lines in rows or columns? Anyway, just take a line from each and subtract them and see if the signs are all the same:
if length(unique(sign(vec1-vec2))) == 1
% No crossing
fprintf('They do not cross or they completely overlap.\n');
else
% Crossing
fprintf('They cross or intersect.\n');
end
where vec1 and vec2 are the signals.

카테고리

도움말 센터File Exchange에서 Function Creation에 대해 자세히 알아보기

태그

질문:

2020년 3월 23일

댓글:

2023년 6월 2일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by