anticlockwise points coordinates sorting

5 次查看(过去 30 天)
Hi,
I have the follwoing points cooridnates:
A = [0 0 ;
1 0.2;
0 1 ;
0 0.6;
1 0 ;
1 1 ;
0.6 1;
0.2 0;
0 0 ;
0.2 1;
0 0.2;
0.6 0;
1 0.6];
I would like to sort these points in a way that they resmbles points on a square polygon in an anticloskwise direction, as follwoing:
B = [0 0 ;
0.2 0;
0.6 0;
1 0 ;
1 0.2;
1 0.6;
1 1 ;
0.6 1;
0.2 1;
0 1 ;
0 0.6;
0 0.2;
0 0];
%here is how to visulaise the points
figure;
plot(A(:,1),A(:,2),'rx','LineWidth',2)
Any idea how to sort/arrange these points?
Thanks.

采纳的回答

Les Beckham
Les Beckham 2022-11-21
编辑:Les Beckham 2022-11-21
A = [0 0 ;
1 0.2;
0 1 ;
0 0.6;
1 0 ;
1 1 ;
0.6 1;
0.2 0;
0 0 ;
0.2 1;
0 0.2;
0.6 0;
1 0.6];
c = [mean(A(:,1)), mean(A(:,2))]; % centroid of the points
d = A - c; % vectors from points to centroid
angles = atan2d(d(:,1), d(:,2)); % angles from centroid to each point
[~,idx] = sort(angles, 'descend'); % sort the angles anti-clockwise
B = A(idx, :) % sort the points based on the angles
B = 13×2
0.6000 0 1.0000 0 1.0000 0.2000 1.0000 0.6000 1.0000 1.0000 0.6000 1.0000 0.2000 1.0000 0 1.0000 0 0.6000 0 0.2000
plot(B(:,1),B(:,2), 'rx')
grid on
hold on
text(B(:,1), B(:,2), sprintfc('%d', 1:numel(idx)))
plot(c(1), c(2), 'bo')
  6 个评论

请先登录,再进行评论。

更多回答(0 个)

类别

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