Sorting Points (2D) clockwise
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
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 ?
Adam Danz
2022 年 2 月 11 日
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)?
Adam Danz
2022 年 7 月 2 日
@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.
Daniela Würmseer
2022 年 7 月 5 日
@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!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
