1 %% Copyright (c) 2011, INRA
2 %% 2007-2011, David Legland <david.legland@grignon.inra.fr>
3 %% 2011 Adapted to Octave by Juan Pablo Carbajal <carbajal@ifi.uzh.ch>
5 %% All rights reserved.
6 %% (simplified BSD License)
8 %% Redistribution and use in source and binary forms, with or without
9 %% modification, are permitted provided that the following conditions are met:
11 %% 1. Redistributions of source code must retain the above copyright notice, this
12 %% list of conditions and the following disclaimer.
14 %% 2. Redistributions in binary form must reproduce the above copyright notice,
15 %% this list of conditions and the following disclaimer in the documentation
16 %% and/or other materials provided with the distribution.
18 %% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 %% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 %% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 %% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 %% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 %% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 %% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 %% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 %% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 %% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 %% POSSIBILITY OF SUCH DAMAGE.
30 %% The views and conclusions contained in the software and documentation are
31 %% those of the authors and should not be interpreted as representing official
32 %% policies, either expressed or implied, of copyright holder.
35 %% @deftypefn {Function File} {@var{alpha} =} vectorAngle (@var{v1})
36 %% Angle of a vector, or between 2 vectors
38 %% A = vectorAngle(V);
39 %% Returns angle between Ox axis and vector direction, in Counter
40 %% clockwise orientation.
41 %% The result is normalised between 0 and 2*PI.
43 %% A = vectorAngle(V1, V2);
44 %% Returns the angle from vector V1 to vector V2, in counter-clockwise
45 %% order, and in radians.
47 %% A = vectorAngle(..., 'cutAngle', CUTANGLE);
48 %% A = vectorAngle(..., CUTANGLE); % (deprecated syntax)
49 %% Specifies convention for angle interval. CUTANGLE is the center of the
50 %% 2*PI interval containing the result. See <a href="matlab:doc
51 %% ('normalizeAngle')">normalizeAngle</a> for details.
54 %% rad2deg(vectorAngle([2 2]))
57 %% rad2deg(vectorAngle([1 sqrt(3)]))
60 %% rad2deg(vectorAngle([0 -1]))
64 %% @seealso{vectors2d, angles2d, normalizeAngle}
67 function alpha = vectorAngle(v1, varargin)
75 % process input arguments
76 while ~isempty(varargin)
78 if isnumeric(var) && isscalar(var)
79 % argument is normalization constant
80 cutAngle = varargin{1};
83 elseif isnumeric(var) && size(var, 2) == 2
84 % argument is second vector
88 elseif ischar(var) && length(varargin) >= 2
89 % argument is option given as string + value
90 if strcmpi(var, 'cutAngle')
91 cutAngle = varargin{2};
95 error(['Unknown option: ' var]);
99 error('Unable to parse inputs');
104 %% Case of one vector
106 % If only one vector is provided, computes its angle
108 % compute angle and format result in a 2*pi interval
109 alpha = atan2(v1(:,2), v1(:,1));
111 % normalize within a 2*pi interval
112 alpha = normalizeAngle(alpha + 2*pi, cutAngle);
118 %% Case of two vectors
120 % compute angle of each vector
121 alpha1 = atan2(v1(:,2), v1(:,1));
122 alpha2 = atan2(v2(:,2), v2(:,1));
125 alpha = bsxfun(@minus, alpha2, alpha1);
127 % normalize within a 2*pi interval
128 alpha = normalizeAngle(alpha + 2*pi, cutAngle);
133 %! ang = vectorAngle([1 0]);
134 %! assert(0, ang, 1e-6);
137 %! ang = vectorAngle([0 1]);
138 %! assert(pi/2, ang, 1e-6);
141 %! ang = vectorAngle([-1 0]);
142 %! assert(pi, ang, 1e-6);
145 %! ang = vectorAngle([0 -1]);
146 %! assert(3*pi/2, ang, 1e-6);
149 %! ang = vectorAngle([-1 1]);
150 %! assert(3*pi/4, ang, 1e-6);
153 %! ang = vectorAngle([1 0], pi);
154 %! assert(0, ang, 1e-6);
157 %! ang = vectorAngle([0 1], pi);
158 %! assert(pi/2, ang, 1e-6);
161 %! ang = vectorAngle([-1 0], pi);
162 %! assert(pi, ang, 1e-6);
165 %! ang = vectorAngle([0 -1], pi);
166 %! assert(3*pi/2, ang, 1e-6);
169 %! ang = vectorAngle([-1 1], pi);
170 %! assert(3*pi/4, ang, 1e-6);
173 %! vecs = [1 0;0 1;-1 0;0 -1;1 1];
174 %! angs = [0;pi/2;pi;3*pi/2;pi/4];
175 %! assert(angs, vectorAngle(vecs));
176 %! assert(angs, vectorAngle(vecs, pi));
179 %! ang = vectorAngle([1 0], 0);
180 %! assert(0, ang, 1e-6);
183 %! ang = vectorAngle([0 1], 0);
184 %! assert(pi/2, ang, 1e-6);
187 %! ang = vectorAngle([0 -1], 0);
188 %! assert(-pi/2, ang, 1e-6);
191 %! ang = vectorAngle([-1 1], 0);
192 %! assert(3*pi/4, ang, 1e-6);
195 %! vecs = [1 0;0 1;0 -1;1 1;1 -1];
196 %! angs = [0;pi/2;-pi/2;pi/4;-pi/4];
197 %! assert(angs, vectorAngle(vecs, 0), 1e-6);
203 %! assert(ang, vectorAngle(v1, v2), 1e-6);
207 %! v2 = [0 1; 0 1; 1 1; -1 1];
208 %! ang = [pi / 2 ;pi / 2 ;pi / 4 ; 3 * pi / 4];
209 %! assert(ang, vectorAngle(v1, v2), 1e-6);
212 %! v1 = [0 1; 0 1; 1 1; -1 1];
214 %! ang = [pi / 2 ;pi / 2 ; 3 * pi / 4 ; pi / 4];
215 %! assert(ang, vectorAngle(v1, v2), 1e-6);
218 %! v1 = [1 0; 0 1; 1 1; -1 1];
219 %! v2 = [0 1; 1 0; -1 1; -1 0];
220 %! ang = [pi / 2 ;3 * pi / 2 ;pi / 2 ; pi / 4];
221 %! assert(ang, vectorAngle(v1, v2), 1e-6);