Home > matGeom > geom2d > angleSort.m

angleSort

PURPOSE ^

ANGLESORT Sort points in the plane according to their angle to origin.

SYNOPSIS ^

function varargout = angleSort(pts, varargin)

DESCRIPTION ^

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


 ------
 Author: David Legland
 e-mail: david.legland@grignon.inra.fr
 Created: 2005-11-24
 Copyright 2010 INRA - Cepia Software Platform.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

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@grignon.inra.fr
0029 % Created: 2005-11-24
0030 % Copyright 2010 INRA - Cepia Software Platform.
0031 
0032 
0033 %   HISTORY :
0034 
0035 % default values
0036 pt0 = [0 0];
0037 theta0 = 0;
0038 
0039 if length(varargin)==1
0040     var = varargin{1};
0041     if size(var, 2)==1
0042         % specify angle
0043         theta0 = var;
0044     else
0045         pt0 = var;
0046     end
0047 elseif length(varargin)==2
0048     pt0 = varargin{1};
0049     theta0 = varargin{2};
0050 end
0051 
0052 
0053 n = size(pts, 1);
0054 pts2 = pts - repmat(pt0, [n 1]);
0055 angle = lineAngle([zeros(n, 2) pts2]);
0056 angle = mod(angle - theta0 + 2*pi, 2*pi);
0057 
0058 [angles, I] = sort(angle); 
0059 
0060 % format output
0061 switch nargout
0062     case 1
0063         varargout{1} = pts(I, :);
0064     case 2
0065         varargout{1} = pts(I, :);
0066         varargout{2} = I;
0067     case 3
0068         varargout{1} = pts(I, :);
0069         varargout{2} = I;
0070         varargout{3} = angles;
0071 end
0072 
0073

Generated on Wed 16-Feb-2022 15:10:47 by m2html © 2003-2019