How to find the first intersection point while the results showing the order of matrix indx with polyxpoly?

3 views (last 30 days)
hello
I have the below data
x=[25;12];
y=[31;6];
xd=[5;40;NaN;2;11;NaN;10;25];
yd=[13;16;NaN;8;9;NaN;25;20];
Inkeduntitled1_LI.jpg
I want to move from (x1,y1) to (x2,y2) and I know that I will initialy intersect a segment in 7 and then line 1. So I use polyxpoly for the number of intersected segments.
the answer is:
[xi,yi,ii]=polyxpoly(x,y,outxd,outyd)
ii = 2×2
1 1
1 7
I want
ii = 2×2
1 7
1 1
That is a very simple example of my problem. Take into consideration that all my x,y,xd,yd are generated randomly So i can not change the position of each row in xd yd matrix.
Thanks a lot

Answers (1)

Pranjal Kaura
Pranjal Kaura on 30 Aug 2021
Hey,
It is my understanding that you want to find the points where a line segment A (defined by x, y in your example), intersects with a polygon B(defined by xd, yd in your example) and you want the points to be returned in order, wherein you've defined the order to be the traversal from [x1, y1] to [x2, y2]. (points of line segment A)
Here is the code for the same:
x=[25;12];
y=[31;6];
xd=[5;40;NaN;2;11;NaN;10;25];
yd=[13;16;NaN;8;9;NaN;25;20];
[xi,yi,ii]=polyxpoly(x,y,xd,yd);
sourcePoint = [x(1), y(1)];
pointsofIntersection = [xi, yi];
dist = pdist2( sourcePoint, pointsofIntersection); %distance between the sourcePoint and the points of intersection
[~, indexSorted] = sort(dist); %sorting the array and storing the index
ii(indexSorted, :)
pointsofIntersection(indexSorted, :)

Community Treasure Hunt

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

Start Hunting!