1 %% Copyright (c) 2011, INRA
2 %% 2008-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{alpha2} =} normalizeAngle (@var{alpha})
36 %% @deftypefnx {Function File} {@var{alpha2} =} normalizeAngle (@var{alpha}, @var{center})
37 %% Normalize an angle value within a 2*PI interval
39 %% ALPHA2 = normalizeAngle(ALPHA);
40 %% ALPHA2 is the same as ALPHA modulo 2*PI and is positive.
42 %% ALPHA2 = normalizeAngle(ALPHA, CENTER);
43 %% Specifies the center of the angle interval.
44 %% If CENTER==0, the interval is [-pi ; +pi]
45 %% If CENTER==PI, the interval is [0 ; 2*pi] (default).
48 %% % normalization between 0 and 2*pi (default)
49 %% normalizeAngle(5*pi)
53 %% % normalization between -pi and +pi
54 %% normalizeAngle(7*pi/2, 0)
59 %% Follows the same convention as apache commons library, see:
60 %% http://commons.apache.org/math/api-2.2/org/apache/commons/math/util/MathUtils.html%%
62 %% @seealso{vectorAngle, lineAngle}
65 function alpha = normalizeAngle(alpha, varargin)
72 alpha = mod(alpha-center+pi, 2*pi) + center-pi;
76 %!assert (pi/2, normalizeAngle (pi/2), 1e-6);
77 %!assert (pi, normalizeAngle (pi), 1e-6);
78 %!assert (3*pi/2, normalizeAngle (3*pi/2), 1e-6);
79 %!assert (pi/2, normalizeAngle (pi/2, pi), 1e-6);
80 %!assert (pi, normalizeAngle (pi, pi), 1e-6);
81 %!assert (3*pi/2, normalizeAngle (3*pi/2, pi), 1e-6);
84 %! theta = linspace(0, 2*pi-.1, 100);
85 %! assert(theta, normalizeAngle (theta), 1e-6);
87 %!assert (0, normalizeAngle (0, 0), 1e-6);
88 %!assert (pi/2, normalizeAngle (pi/2, 0), 1e-6);
89 %!assert (-pi, normalizeAngle (-pi, 0), 1e-6);
90 %!assert (-pi/2, normalizeAngle (7*pi/2, 0), 1e-6);
93 %! theta = linspace(-pi+.1, pi-.1, 100);
94 %! assert(theta, normalizeAngle (theta, 0), 1e-6);