How to connect multiple points in order using the shortest line

19 views (last 30 days)
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

Accepted Answer

Matt J
Matt J on 29 Apr 2021
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)

More Answers (1)

Matt J
Matt J on 29 Apr 2021
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')

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!