# anticlockwise points coordinates sorting

3 views (last 30 days)
Lama Hamadeh on 21 Nov 2022
Commented: Les Beckham on 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.

Les Beckham on 21 Nov 2022
Edited: Les Beckham on 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')
Les Beckham on 22 Nov 2022
You are quite welcome.

### Categories

Find more on Graphics Object Programming in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by