Home > matGeom > geom2d > vectorAngle.m

vectorAngle

PURPOSE ^

VECTORANGLE Horizontal angle of a vector, or angle between 2 vectors.

SYNOPSIS ^

function alpha = vectorAngle(v1, varargin)

DESCRIPTION ^

VECTORANGLE 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

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function alpha = vectorAngle(v1, varargin)
0002 %VECTORANGLE 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 2007-2024 INRA - Cepia Software Platform
0038 
0039 %% Initializations
0040 
0041 % default values
0042 v2 = [];
0043 midAngle = pi; % normalize angles between 0 and 2*PI
0044 
0045 % process input arguments
0046 while ~isempty(varargin)
0047     var = varargin{1};
0048     if isnumeric(var) && isscalar(var)
0049         % argument is normalization constant
0050         midAngle = varargin{1};
0051         varargin(1) = [];
0052         
0053     elseif isnumeric(var) && size(var, 2) == 2
0054         % argument is second vector
0055         v2 = varargin{1};
0056         varargin(1) = [];
0057         
0058     elseif ischar(var) && length(varargin) >= 2
0059         % argument is option given as string + value
0060         if strcmpi(var, 'cutAngle') || strcmpi(var, 'midAngle')
0061             midAngle = varargin{2};
0062             varargin(1:2) = [];
0063             
0064         else
0065             error(['Unknown option: ' var]);
0066         end
0067         
0068     else
0069         error('Unable to parse inputs');
0070     end
0071 end
0072 
0073 
0074 %% Case of one vector
0075 
0076 % If only one vector is provided, computes its angle
0077 if isempty(v2)
0078     % compute angle and format result in a 2*pi interval
0079     alpha = atan2(v1(:,2), v1(:,1));
0080     
0081     % normalize within a 2*pi interval
0082     alpha = normalizeAngle(alpha + 2*pi, midAngle);
0083     
0084     return;
0085 end
0086 
0087 
0088 %% Case of two vectors
0089 
0090 % compute angle of each vector
0091 alpha1 = mod(atan2(v1(:,2), v1(:,1)), 2*pi);
0092 alpha2 = mod(atan2(v2(:,2), v2(:,1)), 2*pi);
0093 
0094 % difference
0095 alpha = bsxfun(@minus, alpha2, alpha1);
0096 
0097 % normalize within a 2*pi interval
0098 alpha = normalizeAngle(alpha + 2*pi, midAngle);
0099

Generated on Thu 21-Nov-2024 11:30:22 by m2html © 2003-2022