Horizontal angle of a vector, or angle between 2 vectors. A = vectorAngle(V); Returns angle between Ox axis and vector direction, in radians, in counter-clockwise orientation. The result is normalised between 0 and 2*PI. A = vectorAngle(V1, V2); Returns the angle from vector V1 to vector V2, in counter-clockwise order, in radians. A = vectorAngle(..., 'midAngle', MIDANGLE); Specifies convention for angle interval. MIDANGLE is the center of the 2*PI interval containing the result. See <a href="matlab:doc ('normalizeAngle')">normalizeAngle</a> for details. Example: rad2deg(vectorAngle([2 2])) ans = 45 rad2deg(vectorAngle([1 sqrt(3)])) ans = 60 rad2deg(vectorAngle([0 -1])) ans = 270 See also: vectors2d, angles2d, normalizeAngle
0001 function alpha = vectorAngle(v1, varargin) 0002 % Horizontal angle of a vector, or angle between 2 vectors. 0003 % 0004 % A = vectorAngle(V); 0005 % Returns angle between Ox axis and vector direction, in radians, in 0006 % counter-clockwise orientation. 0007 % The result is normalised between 0 and 2*PI. 0008 % 0009 % A = vectorAngle(V1, V2); 0010 % Returns the angle from vector V1 to vector V2, in counter-clockwise 0011 % order, in radians. 0012 % 0013 % A = vectorAngle(..., 'midAngle', MIDANGLE); 0014 % Specifies convention for angle interval. MIDANGLE is the center of the 0015 % 2*PI interval containing the result. See <a href="matlab:doc 0016 % ('normalizeAngle')">normalizeAngle</a> for details. 0017 % 0018 % Example: 0019 % rad2deg(vectorAngle([2 2])) 0020 % ans = 0021 % 45 0022 % rad2deg(vectorAngle([1 sqrt(3)])) 0023 % ans = 0024 % 60 0025 % rad2deg(vectorAngle([0 -1])) 0026 % ans = 0027 % 270 0028 % 0029 % See also: 0030 % vectors2d, angles2d, normalizeAngle 0031 % 0032 0033 % ------ 0034 % Author: David Legland 0035 % e-mail: david.legland@inrae.fr 0036 % Created: 2007-10-18 0037 % Copyright 2011 INRA - Cepia Software Platform. 0038 0039 % HISTORY 0040 % 2010-04-16 add psb to specify center interval 0041 % 2011-04-10 add support for angle between two vectors 0042 0043 0044 %% Initializations 0045 0046 % default values 0047 v2 = []; 0048 midAngle = pi; % normalize angles between 0 and 2*PI 0049 0050 % process input arguments 0051 while ~isempty(varargin) 0052 var = varargin{1}; 0053 if isnumeric(var) && isscalar(var) 0054 % argument is normalization constant 0055 midAngle = varargin{1}; 0056 varargin(1) = []; 0057 0058 elseif isnumeric(var) && size(var, 2) == 2 0059 % argument is second vector 0060 v2 = varargin{1}; 0061 varargin(1) = []; 0062 0063 elseif ischar(var) && length(varargin) >= 2 0064 % argument is option given as string + value 0065 if strcmpi(var, 'cutAngle') || strcmpi(var, 'midAngle') 0066 midAngle = varargin{2}; 0067 varargin(1:2) = []; 0068 0069 else 0070 error(['Unknown option: ' var]); 0071 end 0072 0073 else 0074 error('Unable to parse inputs'); 0075 end 0076 end 0077 0078 0079 %% Case of one vector 0080 0081 % If only one vector is provided, computes its angle 0082 if isempty(v2) 0083 % compute angle and format result in a 2*pi interval 0084 alpha = atan2(v1(:,2), v1(:,1)); 0085 0086 % normalize within a 2*pi interval 0087 alpha = normalizeAngle(alpha + 2*pi, midAngle); 0088 0089 return; 0090 end 0091 0092 0093 %% Case of two vectors 0094 0095 % compute angle of each vector 0096 alpha1 = mod(atan2(v1(:,2), v1(:,1)), 2*pi); 0097 alpha2 = mod(atan2(v2(:,2), v2(:,1)), 2*pi); 0098 0099 % difference 0100 alpha = bsxfun(@minus, alpha2, alpha1); 0101 0102 % normalize within a 2*pi interval 0103 alpha = normalizeAngle(alpha + 2*pi, midAngle); 0104