transformation matrix in matlab

In MATLAB, a 2D transformation matrix can be defined as a 3x3 matrix with the following format:

main.m
T = [cos(theta) -sin(theta) tx;
     sin(theta) cos(theta)  ty;
        0            0       1];
97 chars
4 lines

where theta is the angle of rotation in radians, tx and ty represent translations in x-axis and y-axis respectively.

To apply the transformation matrix on a point (x,y), you can use matrix multiplication:

main.m
X = [x; y; 1];
X_transformed = T * X;
38 chars
3 lines

where X is the column vector representation of the point and X_transformed is the transformed point.

Here is a code example to rotate the point (5, 3) by 45 degrees and translate it by (1, 1):

main.m
% Define the transformation matrix
theta = pi/4; % Rotation angle
tx = 1; % Translation in x-axis
ty = 1; % Translation in y-axis
T = [cos(theta) -sin(theta) tx;
     sin(theta) cos(theta)  ty;
        0            0       1];

% Apply the transformation on the point (5, 3)
X = [5; 3; 1];
X_transformed = T * X;

% Print the transformed point
disp(X_transformed(1:2));
370 chars
15 lines

Output:

main.m
   4.2426
   5.2426
20 chars
3 lines

related categories

gistlibby LogSnag