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

조회 수: 1 (최근 30일)
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

답변 (1개)

Pranjal Kaura
Pranjal Kaura 2021년 8월 30일
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, :)

카테고리

Help CenterFile Exchange에서 Elementary Polygons에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by