clear allWindow=10square=[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 onaxis squareaxis([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.
Best Answer