CREATEROTATION Create the 3*3 matrix of a rotation. TRANS = createRotation(THETA); Returns the rotation corresponding to angle THETA (in radians) The returned matrix has the form : [cos(theta) -sin(theta) 0] [sin(theta) cos(theta) 0] [0 0 1] TRANS = createRotation(POINT, THETA); TRANS = createRotation(X0, Y0, THETA); Also specifies origin of rotation. The result is similar as performing translation(-X0, -Y0), rotation(THETA), and translation(X0, Y0). Example % apply a rotation on a polygon poly = [0 0; 30 0;30 10;10 10;10 20;0 20]; trans = createRotation([10 20], pi/6); polyT = transformPoint(poly, trans); % display the original and the rotated polygons figure; hold on; axis equal; axis([-10 40 -10 40]); drawPolygon(poly, 'k'); drawPolygon(polyT, 'b'); See also: transforms2d, transformPoint, createRotation90, createTranslation
0001 function trans = createRotation(varargin) 0002 %CREATEROTATION Create the 3*3 matrix of a rotation. 0003 % 0004 % TRANS = createRotation(THETA); 0005 % Returns the rotation corresponding to angle THETA (in radians) 0006 % The returned matrix has the form : 0007 % [cos(theta) -sin(theta) 0] 0008 % [sin(theta) cos(theta) 0] 0009 % [0 0 1] 0010 % 0011 % TRANS = createRotation(POINT, THETA); 0012 % TRANS = createRotation(X0, Y0, THETA); 0013 % Also specifies origin of rotation. The result is similar as performing 0014 % translation(-X0, -Y0), rotation(THETA), and translation(X0, Y0). 0015 % 0016 % Example 0017 % % apply a rotation on a polygon 0018 % poly = [0 0; 30 0;30 10;10 10;10 20;0 20]; 0019 % trans = createRotation([10 20], pi/6); 0020 % polyT = transformPoint(poly, trans); 0021 % % display the original and the rotated polygons 0022 % figure; hold on; axis equal; axis([-10 40 -10 40]); 0023 % drawPolygon(poly, 'k'); 0024 % drawPolygon(polyT, 'b'); 0025 % 0026 % See also: 0027 % transforms2d, transformPoint, createRotation90, createTranslation 0028 % 0029 0030 % --------- 0031 % author : David Legland 0032 % INRA - TPV URPOI - BIA IMASTE 0033 % created the 06/04/2004. 0034 % 0035 0036 % HISTORY 0037 % 22/04/2009: rename as createRotation 0038 0039 % default values 0040 cx = 0; 0041 cy = 0; 0042 theta = 0; 0043 0044 % get input values 0045 if length(varargin)==1 0046 % only angle 0047 theta = varargin{1}; 0048 elseif length(varargin)==2 0049 % origin point (as array) and angle 0050 var = varargin{1}; 0051 cx = var(1); 0052 cy = var(2); 0053 theta = varargin{2}; 0054 elseif length(varargin)==3 0055 % origin (x and y) and angle 0056 cx = varargin{1}; 0057 cy = varargin{2}; 0058 theta = varargin{3}; 0059 end 0060 0061 % compute coefs 0062 cot = cos(theta); 0063 sit = sin(theta); 0064 tx = cy*sit - cx*cot + cx; 0065 ty = -cy*cot - cx*sit + cy; 0066 0067 % create transformation matrix 0068 trans = [cot -sit tx; sit cot ty; 0 0 1];