# Sorting Points (2D) clockwise

98 views (last 30 days)
Daniela Würmseer on 10 Feb 2022
Edited: Adam Danz on 5 Jul 2022
Hello is there a function or easy way to sort 2D Points clockwise?
For example:
Given:
x = (-1,-1)
y = (4,2)
z = (-1,4)
Thank you

Adam Danz on 10 Feb 2022
Edited: Adam Danz on 10 Feb 2022
And what decides the starting coordinate?
If you want to sort 2D coordinates by their polar angle relative to (0,0) in the clockwise direction, convert the Cartesian coordinates to polar coordinates, wrap the radian angle values to [0:2*pi], then sort the results in descending order for the clockwise direction. The first value will be the coordinate closest to 2*pi radians.
data = [-1 -1; 4 2; -1 4]
data = 3×2
-1 -1 4 2 -1 4
figure()
plot(data(:,1), data(:,2), 'o')
text(data(:,1)+0.2,data(:,2),{'x','y','z'})
axis equal
grid on
% Convert to polar coordinates
text(data(:,1)-0.2, data(:,2),compose('%d',sortIdx),'HorizontalAlignment', 'right') Adam Danz on 5 Jul 2022
You must be doing something different from my answer. In your example, you'd want sortrows.