How to find position of a new point along a series of line segments?

3 ビュー (過去 30 日間)
Dominik Mattioli
Dominik Mattioli 2021 年 6 月 1 日
編集済み: Adam Danz 2021 年 6 月 1 日
Say that you know that your query point Q is colinear to one line segment in a sequence of line segments XY. If XY is an Mx2 array and you wanted to insert your 1x2 point Q into XY, how do you figure out which two points of XY to split up? Example"
XY = [1 1; 2 1; 3 1; 5 1; 9 1];
Q = [7 1];
% Figure out where Q lies within XY
% indQ = ...
newXY = vertcat( XY( 1:indQ, : ), Q, XY( indQ+1 : end, : ) );
Sure you could test colinearity of Q to each line segment of XY. I'm wondering if there is a geometric construct for this, though.

採用された回答

Adam Danz
Adam Danz 2021 年 6 月 1 日
編集済み: Adam Danz 2021 年 6 月 1 日
For collinear points,
XY = [1 1; 2 1; 3 1; 5 1; 9 1];
Q = [7 1];
XYQ = sortrows([XY;Q])
XYQ = 6×2
1 1 2 1 3 1 5 1 7 1 9 1
For coordinates along a non-linear line, you can compute the distance between Q (1xn) and each point in XY (mxn). Q is inserted between the two points in XY that are the shortest distance to Q if and only if the two points are next to each other. If the two closest points are not next to each other then placement of Q cannot be determined and an error will be thrown by the assert command.
XY = [1 1; 2 1; 3 1; 5 1; 9 1];
Q = [7 1];
dist = pdist2(Q,XY);
[~, min2idx] = mink(dist,2);
assert(diff(min2idx)==1, 'Placement cannot be determined.')
XYQ = [XY(1:min2idx(1),:); Q; XY(min2idx(2):end,:)]
XYQ = 6×2
1 1 2 1 3 1 5 1 7 1 9 1
Demo using non-linear coordinates
x = 0:.5:pi;
XY = [x',sin(x)'];
Q = XY(5,:)
Q = 1×2
2.0000 0.9093
XY(5,:) = []
XY = 6×2
0 0 0.5000 0.4794 1.0000 0.8415 1.5000 0.9975 2.5000 0.5985 3.0000 0.1411
dist = pdist2(Q,XY);
[~, min2idx] = mink(dist,2);
assert(diff(min2idx)==1, 'Placement cannot be determined.')
XYQ = [XY(1:min2idx(1),:); Q; XY(min2idx(2):end,:)]
XYQ = 7×2
0 0 0.5000 0.4794 1.0000 0.8415 1.5000 0.9975 2.0000 0.9093 2.5000 0.5985 3.0000 0.1411
plot(XYQ(:,1), XYQ(:,2), 'bo-')
hold on; grid on
plot(Q(1),Q(2),'r*')
The first method, using sortrows, works for all of these examples but would fail in some non-linear lines such as circles.
  3 件のコメント
Adam Danz
Adam Danz 2021 年 6 月 1 日
Collinearity implies straight lines,
I'll update my answer to show how to sort nonlinear coordinates.
Dominik Mattioli
Dominik Mattioli 2021 年 6 月 1 日
Oh, thanks for catching that. I used the word because I wanted to say that Q is colinear with one of the line segments defining XY. I see your point, though.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGlobal or Multiple Starting Point Search についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by