clear all
Window=10
square=[0 0; 0 Window; Window 0; Window Window];% Box coordinates
N=5;
% Coordinates of segments endpoints
X1=rand(N,1)*(10+0.1)+0.1;
X2=rand(N,1)*(10+0.1)+0.1;
Y1=rand(N,1)*(10+0.1)+0.1;
Y2=rand(N,1)*(10+0.1)+0.1;
A = [X1(:), X2(:)]; B =[Y1(:), Y2(:)];
% Find the intersection between segments
% Here i use the intersection function by Douglas M. Schwarz
ABx=[X1(:), X2(:)];
reshape(ABx,1,[]);
transpose(ABx);
ABx.';
ABx(:);
ABx=reshape(ABx.',1,[]);
ABy=[Y1(:), Y2(:)];
reshape(ABy,1,[]);
transpose(ABy);
ABy.';
ABy(:);
ABy=reshape(ABy.',1,[]);
pos = 3:3:((((length(ABy))/2)-1)*3);
[r,c] = size(ABy);
add = numel(pos);
Xnew = NaN(r,c+add);
Ynew = NaN(r,c+add);
idx = setdiff(1:c+add,pos);
Xnew(1,idx) = ABx;
Ynew(1,idx) = ABy;
Inter=intersections(Xnew,Ynew);% This gives the intersection points
figure, plot(A.',B.','LineWidth', 0.75)
grid on
axis square
axis([0 Window 0 Window])
I have segments as shown above. For each segment, i have the coordinates of endpoints. Now, I want to determine the pathways that connect both sides of the box, and remove the unconnected segments. So far, i have been able to find the intersections betweeen segments, and will appreciate any help finding the pathways.

댓글 수: 3

darova
darova 2019년 8월 30일
How would you know the order of connections?
12Untitled.png
Tchilabalo
Tchilabalo 2019년 8월 30일
By sorting the intersections in increasing order of x and y.
darova
darova 2019년 8월 30일
SO what is the question? You have all points. Why don't just use plot()?

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

 채택된 답변

darova
darova 2019년 8월 30일

0 개 추천

Simple example
n = 5;
x1 = rand(n,2);
y1 = rand(n,2);
x2 = rand(n,2);
y2 = rand(n,2);
k = 0;
% loop through all segment to find all intersections
for i = 1:n
for j = 1:n
[xc,yc] = polyxpoly(x1(i,:),y1(i,:),x2(j,:),y2(j,:));
if ~isempty(xc)
k = k+1;
x(k) = xc;
y(k) = yc;
end
end
end
[~, ix] = sort(x);
plot(x(ix),y(ix),'o-k','linewidth',2)
hold on
plot(x1',y1','r',x2',y2','b')
hold off

댓글 수: 2

Tchilabalo
Tchilabalo 2019년 8월 30일
Thanks Darova for your answer.
darova
darova 2019년 9월 8일
Accept the answer if it helped

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

추가 답변 (0개)

카테고리

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

태그

질문:

2019년 8월 29일

댓글:

2019년 9월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by