1## Copyright (C) 2021 David Legland
2## All rights reserved.
3##
4## Redistribution and use in source and binary forms, with or without
5## modification, are permitted provided that the following conditions are met:
6##
7##     1 Redistributions of source code must retain the above copyright notice,
8##       this list of conditions and the following disclaimer.
9##     2 Redistributions in binary form must reproduce the above copyright
10##       notice, this list of conditions and the following disclaimer in the
11##       documentation and/or other materials provided with the distribution.
12##
13## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ''AS IS''
14## AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15## IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16## ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
17## ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18## DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19## SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
20## CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
21## OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23##
24## The views and conclusions contained in the software and documentation are
25## those of the authors and should not be interpreted as representing official
26## policies, either expressed or implied, of the copyright holders.
27
28function mat = createRotation90(varargin)
29%CREATEROTATION90  Matrix of a rotation for 90 degrees multiples.
30%
31%   MAT = createRotation90
32%   Returns the 3-by-3 matrix corresponding to a rotation by 90 degrees.
33%   As trigonometric functions are explicitley converted to +1 or -1, the
34%   resulting matrix obtained with this function is more precise than
35%   the one obtained with createRotation.
36%
37%   MAT = createRotation90(NUM)
38%   Specifies the number of rotations to performs. NUM should be an integer
39%   (possibly negative).
40%
41%   Example
42%     poly = [10 0;20 0;10 10];
43%     rot = createRotation90;
44%     poly2 = transformPoint(poly, rot);
45%     figure; hold on; axis equal;
46%     drawPolygon(poly);
47%     drawPolygon(poly2, 'm');
48%     legend('original', 'rotated');
49%
50%     % specify number of rotations, and center
51%     rot = createRotation90(2, [10 10]);
52%     poly3 = transformPoint(poly, rot);
53%     drawPolygon(poly3, 'g');
54%
55%   See also
56%   transforms2d, createRotation
57%
58% ------
59% Author: David Legland
60% e-mail: david.legland@grignon.inra.fr
61% Created: 2012-06-20,    using Matlab 7.9.0.529 (R2009b)
62% Copyright 2012 INRA - Cepia Software Platform.
63
64% default values
65num = 1;
66center = [0 0];
67
68% process input arguments
69while ~isempty(varargin)
70    var = varargin{1};
71    if isnumeric(var) && isscalar(var)
72        % extract number of rotations
73        num = mod(mod(var, 4) + 4, 4);
74
75    elseif isnumeric(var) && length(var) == 2
76        % extract rotation center
77        center = var;
78
79    else
80        % unknown argument
81        error('MatGeom:createRotation90', ...
82            'Unable to parse input arguments');
83    end
84    varargin(1) = [];
85end
86
87% determine rotation parameters
88switch num
89    case 0
90        ct = 1;
91        st = 0;
92    case 1
93        ct = 0;
94        st = 1;
95    case 2
96        ct = -1;
97        st = 0;
98    case 3
99        ct = 0;
100        st = -1;
101end
102
103% compute transform matrix
104mat = [ ...
105    ct -st 0; ...
106    st ct 0; ...
107    0 0 1];
108
109% change center if needed
110if sum(center ~= [0 0]) > 0
111    tra = createTranslation(center);
112    mat = tra * mat / tra;
113end
114