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
  2 件のコメント
Zarak kh
Zarak kh 2019 年 10 月 15 日
No the flip() will not work in this case, because in real situation we may have more than 2 lines so the flip() will not work.

サインインしてコメントする。

回答 (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 Center および File ExchangeWorkspace Variables and MAT-Files についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by