Sorting Points (2D) clockwise
    31 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    Daniela Würmseer
 el 10 de Feb. de 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)
Answer: Sorted clockwise: (y,x,z)
Thank you
0 comentarios
Respuesta aceptada
  Adam Danz
    
      
 el 10 de Feb. de 2022
        
      Editada: Adam Danz
    
      
 el 10 de Feb. de 2022
  
      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]
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 comentarios
Más respuestas (0)
Ver también
Categorías
				Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


