anticlockwise points coordinates sorting
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
LH
am 21 Nov. 2022
Kommentiert: Les Beckham
am 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.
0 Kommentare
Akzeptierte Antwort
Les Beckham
am 21 Nov. 2022
Bearbeitet: Les Beckham
am 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
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')
6 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Lighting, Transparency, and Shading finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!