我在黑白图像中有一些点。我有质心的坐标x,y,我想顺时针订购它们。要做到这些,我想使用角度。但是我有一个很大的困惑:我假设atan2的坐标轴在我照片的中心。然后,我使用其中一个点作为偏移量来制作零角度。
我对此完全感到困惑吗?这难以实现吗?我只想获得所有点的角度,角度零点是我选择的点,然后使用角度按顺时针方向(向角度方向)对质心进行排序。
我希望能在你的帮助下解决这个问题!非常感谢, 赫
答案 0 :(得分:2)
示例:
% 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)
请注意,atan2
会在区间中以逆时针方向返回角度:[-pi,pi]
。这实际上也被cart2pol
函数使用(查看其源代码)。