Sorting Points (2D) clockwise

조회 수: 58 (최근 30일)
Daniela Würmseer
Daniela Würmseer 2022년 2월 10일
편집: Adam Danz 2022년 7월 5일
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)
Answer: Sorted clockwise: (y,x,z)
Thank you

채택된 답변

Adam Danz
Adam Danz 2022년 2월 10일
편집: Adam Danz 2022년 2월 10일
Clockwise about what center point?
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
axis padded
grid on
% Convert to polar coordinates
rad = cart2pol(data(:,1), data(:,2));
radWrapped = mod(rad,2*pi);
radWrapped(radWrapped==0 & rad>0) = 2*pi;
[~, sortIdx] = sort(radWrapped, 'descend');
text(data(:,1)-0.2, data(:,2),compose('%d',sortIdx),'HorizontalAlignment', 'right')
  댓글 수: 10
Daniela Würmseer
Daniela Würmseer 2022년 7월 5일
d = [-0.0020 -5.0000;
-0.0017 -8.5690;
-0.0010 -5.0041];
sort(d)
So i have a Matrix like d and each row of d is supposed to be a point (coordinates). And i want to sort all the points.
Is the declaration from d for this wrong?
Adam Danz
Adam Danz 2022년 7월 5일
편집: Adam Danz 2022년 7월 5일
You must be doing something different from my answer. In your example, you'd want sortrows.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by