CREATEROTATION90 Matrix of a rotation for 90 degrees multiples. MAT = createRotation90 Returns the 3-by-3 matrix corresponding to a rotation by 90 degrees. As trigonometric functions are explicitley converted to +1 or -1, the resulting matrix obtained with this function is more precise than the one obtained with createRotation. MAT = createRotation90(NUM) Specifies the number of rotations to performs. NUM should be an integer (possibly negative). Example poly = [10 0;20 0;10 10]; rot = createRotation90; poly2 = transformPoint(poly, rot); figure; hold on; axis equal; drawPolygon(poly); drawPolygon(poly2, 'm'); legend('original', 'rotated'); % specify number of rotations, and center rot = createRotation90(2, [10 10]); poly3 = transformPoint(poly, rot); drawPolygon(poly3, 'g'); See also transforms2d, createRotation ------ Author: David Legland e-mail: david.legland@grignon.inra.fr Created: 2012-06-20, using Matlab 7.9.0.529 (R2009b) Copyright 2012 INRA - Cepia Software Platform.
0001 function mat = createRotation90(varargin) 0002 %CREATEROTATION90 Matrix of a rotation for 90 degrees multiples. 0003 % 0004 % MAT = createRotation90 0005 % Returns the 3-by-3 matrix corresponding to a rotation by 90 degrees. 0006 % As trigonometric functions are explicitley converted to +1 or -1, the 0007 % resulting matrix obtained with this function is more precise than 0008 % the one obtained with createRotation. 0009 % 0010 % MAT = createRotation90(NUM) 0011 % Specifies the number of rotations to performs. NUM should be an integer 0012 % (possibly negative). 0013 % 0014 % Example 0015 % poly = [10 0;20 0;10 10]; 0016 % rot = createRotation90; 0017 % poly2 = transformPoint(poly, rot); 0018 % figure; hold on; axis equal; 0019 % drawPolygon(poly); 0020 % drawPolygon(poly2, 'm'); 0021 % legend('original', 'rotated'); 0022 % 0023 % % specify number of rotations, and center 0024 % rot = createRotation90(2, [10 10]); 0025 % poly3 = transformPoint(poly, rot); 0026 % drawPolygon(poly3, 'g'); 0027 % 0028 % See also 0029 % transforms2d, createRotation 0030 % 0031 % ------ 0032 % Author: David Legland 0033 % e-mail: david.legland@grignon.inra.fr 0034 % Created: 2012-06-20, using Matlab 7.9.0.529 (R2009b) 0035 % Copyright 2012 INRA - Cepia Software Platform. 0036 0037 % default values 0038 num = 1; 0039 center = [0 0]; 0040 0041 % process input arguments 0042 while ~isempty(varargin) 0043 var = varargin{1}; 0044 if isnumeric(var) && isscalar(var) 0045 % extract number of rotations 0046 num = mod(mod(var, 4) + 4, 4); 0047 0048 elseif isnumeric(var) && length(var) == 2 0049 % extract rotation center 0050 center = var; 0051 0052 else 0053 % unknown argument 0054 error('MatGeom:createRotation90', ... 0055 'Unable to parse input arguments'); 0056 end 0057 varargin(1) = []; 0058 end 0059 0060 % determine rotation parameters 0061 switch num 0062 case 0 0063 ct = 1; 0064 st = 0; 0065 case 1 0066 ct = 0; 0067 st = 1; 0068 case 2 0069 ct = -1; 0070 st = 0; 0071 case 3 0072 ct = 0; 0073 st = -1; 0074 end 0075 0076 % compute transform matrix 0077 mat = [ ... 0078 ct -st 0; ... 0079 st ct 0; ... 0080 0 0 1]; 0081 0082 % change center if needed 0083 if sum(center ~= [0 0]) > 0 0084 tra = createTranslation(center); 0085 mat = tra * mat / tra; 0086 end