-1

I have some points in a black and white image. I have the coordinates, x,y, of the centroids and I want to order them clockwise. To do these I want to use the angles. However I have a big confusion: I'm assuming that the axis of coordinates of atan2 are in the center of my photo. Then, I'm using one of the points as an offset to make the zero angle there.

Am I completely confused about this? Is this so difficult to achieve? I just want to get the angles of all the points, the angle zero being a point I choose, and then use the angles to order the centroids in a clockwise direction (incresing angle direction).

I hope I can solve this problem with your help! Many thanks, Hector

  • This should not be too difficult. But please give us some code and a good bugreport (What did you expect? What did happen?) An example image together with the code and the expected outcome would also help. (Also, the origin of atan2 is at 0|0) – DasKrümelmonster Sep 24 '13 at 09:31
  • similar question: [Sorting clockwise polygon points in MATLAB](http://stackoverflow.com/q/13935324/97160) – Amro Sep 24 '13 at 09:51

1 Answers1

2

Example:

% some random 2D points coordinates
xy = rand(10,2);

% zero-centered
xy_ = bsxfun(@minus, xy, mean(xy));

% compute angles
theta = atan2(xy_(:,2), xy_(:,1));

% sort points clockwise
[~,ord] = sort(theta, 'descend');
xy = xy(ord,:);

% plot newly arranged points and labels
scatter(xy(:,1), xy(:,2), 'filled')
text(xy(:,1), xy(:,2), num2str((1:10)'), 'VerticalAlign','bottom')

% show radius lines
cx = zeros(2,size(xy,1));
cy = zeros(2,size(xy,1));
cx(1,:) = mean(xy(:,1)); cx(2,:) = xy(:,1);
cy(1,:) = mean(xy(:,2)); cy(2,:) = xy(:,2);
line(cx, cy)

sort by angle

Note that atan2 returns angles counter-clockwise in the interval: [-pi,pi]. This is in fact used by cart2pol function as well (view its source code).

Amro
  • 123,847
  • 25
  • 243
  • 454
  • 1
    complex numbers also use this to compute the [phase angle](http://www.mathworks.com/help/matlab/ref/angle.html), so you could write: `theta = angle(complex(xy_(:,1),xy_(:,2)))` – Amro Sep 24 '13 at 09:54