Sorting Points (2D) clockwise
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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
채택된 답변
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
Adam Danz
2022년 2월 11일
Daniela Würmseer's comment moved here from the answers section
-------------------------------------------------------------------------------------
Thank you.
but if i try out the code with the two first points exchanged so data = [-1 0; 0 -1; -1 -1]; then sortIdx = 2 3 1. This makes no sense to me normally sortIdx should be 3 1 2 or ?
The sortIdx is the order of the input values. Your input values change order so the sortIdx is expected to change order, too.
This demo below shows that the sorted inputs are the same for both sets of values.
data = [0 -1; -1 0; -1 -1];
% 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');
sortedData = data(sortIdx,:)
sortedData = 3×2
0 -1
-1 -1
-1 0
data2 = [-1 0; 0 -1; -1 -1];
% Convert to polar coordinates
rad = cart2pol(data2(:,1), data2(:,2));
radWrapped = mod(rad,2*pi);
radWrapped(radWrapped==0 & rad>0) = 2*pi;
[~, sortIdx2] = sort(radWrapped, 'descend');
sortedData2 = data(sortIdx,:)
sortedData2 = 3×2
0 -1
-1 -1
-1 0
The variables sortIdx and sortIdx2 show the indices of the input rows.
Daniela Würmseer
2022년 2월 11일
Thank you
Adam Danz
2022년 2월 11일
Happy to help.
Daniela Würmseer
2022년 7월 2일
I have the three (generated) Points:
(0,0)
(-20,4.4963)
(-19.8955, 3.9739)
if i sort those vectors with the function above they stay in this order.
But normally sorted they should be like:
(0,0)
(-19.8955, 3.9739)
(-20,4.4963)
The same i get with some other Points. Do you know why the Polarcoordinate of (-20,4.4963) is smaller than (-19.8955, 3.9739)?
@Daniela Würmseer one of your data points lies on (0,0). This solution sorts in clockwise, descending order starting at angle 0 which is, by convention, at 3 o'clock (from (0,0) to (0,x) where x is >0). Any data point at 0 degrees will be sorted last since the sort is in descending order. If you'd like points at 0 deg to be considered at 2pi, you can change this part of my answer
radWrapped(radWrapped==0 & rad>0) = 2*pi;
to
radWrapped(radWrapped==0 & rad==0) = 2*pi;
Another solution would be to center your data using
dataCentered = data - mean(data,1); % Assuming data is nx2
and then perform the solution in my answer on the centered data.
@Adam Danz Thank you. But for some points i still get an Error in my Algorithm for example:
The following 3 Points are already sorted but
-0.0020 -5.0000
-0.0010 -5.0041
-0.0017 -8.5690
Sorted it should look like
-0.0020 -5.0000
-0.0017 -8.5690
-0.0010 -5.0041
Do you know what is wrong here?
Adam Danz
2022년 7월 5일
Are these [x,y] coordinates? If so, the values in row-2 of the sorted list (-0.0017, -8.569) doesn't exist in the initial list. It looks like you're sorting x and y independently but, if I understand your comments correctly, you should be sorting the (x,y) pairs.
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?
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
