How to connect multiple points in order using the shortest line

17 ビュー (過去 30 日間)
Romio
Romio 2021 年 4 月 29 日
回答済み: Matt J 2021 年 4 月 29 日
I have the following discrete points.
The x and y coordinates are the following
X = [-0.5305 -2.0960 -1.6192 -2.2768 -1.9141 -2.0279 -2.0102 -2.0087 -2.0083 -2.0083 -2.0083]
Y = [3.4667 1.7524 1.0045 1.2514 1.0493 1.1818 1.1733 1.1763 1.1762 1.1762 1.1762]
I want to draw lines that connect these coordinates in a consecutive way using the shortest distance among them. For example the first line is between (-0.5305,3.4667) and (-2.0960, 1.7524). The second is between (-2.0960, 1.7524) and (-1.6192,1.0045) and so on.
I tried the following code, but it gave me weird lines that are not the shortest
for i = 1:length(Xk)-1
for j = i+1:length(Xk)-1
plot([Xk(i) Xk(j)],[Yk(i) Yk(i)])
plot([Xk(j) Xk(j)],[Yk(i) Yk(j)])
pause(0.01)
end
end

採用された回答

Matt J
Matt J 2021 年 4 月 29 日
X = [-0.5305 -2.0960 -1.6192 -2.2768 -1.9141 -2.0279 -2.0102 -2.0087 -2.0083 -2.0083 -2.0083];
Y = [3.4667 1.7524 1.0045 1.2514 1.0493 1.1818 1.1733 1.1763 1.1762 1.1762 1.1762];
line(X,Y)

その他の回答 (1 件)

Matt J
Matt J 2021 年 4 月 29 日
X = [-0.5305 -2.0960 -1.6192 -2.2768 -1.9141 -2.0279 -2.0102 -2.0087 -2.0083 -2.0083 -2.0083];
Y = [3.4667 1.7524 1.0045 1.2514 1.0493 1.1818 1.1733 1.1763 1.1762 1.1762 1.1762];
p=polyshape(X,Y);
Warning: Polyshape has duplicate vertices, intersections, or other inconsistencies that may produce inaccurate or unexpected results. Input data has been modified to create a well-defined polyshape.
plot(p,'FaceColor','none')

カテゴリ

Help Center および File ExchangeLighting, Transparency, and Shading についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by