anticlockwise points coordinates sorting

18 vues (au cours des 30 derniers jours)
LH
LH le 21 Nov 2022
Commenté : Les Beckham le 22 Nov 2022
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.

Réponse acceptée

Les Beckham
Les Beckham le 21 Nov 2022
Modifié(e) : Les Beckham le 21 Nov 2022
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 commentaires
LH
LH le 22 Nov 2022
I will give it a try. Thanks again.
Les Beckham
Les Beckham le 22 Nov 2022
You are quite welcome.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Elementary Polygons dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by