ANGLESORT Sort points in the plane according to their angle to origin. PTS2 = angleSort(PTS); Computes angle of points with origin, and sort points with increasing angles in Counter-Clockwise direction. PTS2 = angleSort(PTS, PTS0); Computes angles between each point of PTS and PT0, which can be different from origin. PTS2 = angleSort(..., THETA0); Specifies the starting angle for sorting. [PTS2, I] = angleSort(...); Also returns in I the indices of PTS, such that PTS2 = PTS(I, :); [PTS2, I, ANGLES] = angleSort(...); Also returns the ANGLES in corresponding order to PTS2. See also points2d, angles2d, angle2points, normalizeAngle
0001 function varargout = angleSort(pts, varargin) 0002 %ANGLESORT Sort points in the plane according to their angle to origin. 0003 % 0004 % 0005 % PTS2 = angleSort(PTS); 0006 % Computes angle of points with origin, and sort points with increasing 0007 % angles in Counter-Clockwise direction. 0008 % 0009 % PTS2 = angleSort(PTS, PTS0); 0010 % Computes angles between each point of PTS and PT0, which can be 0011 % different from origin. 0012 % 0013 % PTS2 = angleSort(..., THETA0); 0014 % Specifies the starting angle for sorting. 0015 % 0016 % [PTS2, I] = angleSort(...); 0017 % Also returns in I the indices of PTS, such that PTS2 = PTS(I, :); 0018 % 0019 % [PTS2, I, ANGLES] = angleSort(...); 0020 % Also returns the ANGLES in corresponding order to PTS2. 0021 % 0022 % See also 0023 % points2d, angles2d, angle2points, normalizeAngle 0024 % 0025 0026 % ------ 0027 % Author: David Legland 0028 % E-mail: david.legland@inrae.fr 0029 % Created: 2005-11-24 0030 % Copyright 2005-2024 INRA - Cepia Software Platform 0031 0032 % default values 0033 pt0 = [0 0]; 0034 theta0 = 0; 0035 0036 if isscalar(varargin) 0037 var = varargin{1}; 0038 if size(var, 2)==1 0039 % specify angle 0040 theta0 = var; 0041 else 0042 pt0 = var; 0043 end 0044 elseif length(varargin)==2 0045 pt0 = varargin{1}; 0046 theta0 = varargin{2}; 0047 end 0048 0049 0050 n = size(pts, 1); 0051 pts2 = pts - repmat(pt0, [n 1]); 0052 angle = lineAngle([zeros(n, 2) pts2]); 0053 angle = mod(angle - theta0 + 2*pi, 2*pi); 0054 0055 [angles, I] = sort(angle); 0056 0057 % format output 0058 switch nargout 0059 case 1 0060 varargout{1} = pts(I, :); 0061 case 2 0062 varargout{1} = pts(I, :); 0063 varargout{2} = I; 0064 case 3 0065 varargout{1} = pts(I, :); 0066 varargout{2} = I; 0067 varargout{3} = angles; 0068 end 0069 0070