How to preform simple Matrix Transmormations

조회 수: 5 (최근 30일)
Vincent
Vincent 2015년 7월 31일
편집: Madhav Rajan 2015년 8월 4일
I can't figure out how to do translations, rotations and scaling by using matrices. Here is the code. We were told only to use matrices to achieve the effect.
Thanks in advance!
%%Initialize
clc
clear all
close all
%%Part a
shape=[0 1 0; 0 0 1];
Mshape=[0 1 0; 0 0 1;1 1 1];
DX =-3;
DY= 4;
dx =DX/100;
dy = DY/100;
steps = 100
Trans = [1 0 dx ; 0 1 dy; 0 0 1]
for n=1:steps
Mshape =Trans*Mshape
fill(Mshape(1,:),Mshape(2,:),'g')
axis([-8 8 -8 8])
drawnow
end
rev=720;
theta = 1;
Transto0 = [1 0 -DX ; 0 1 -DY; 0 0 1]
TransT = [1 0 DX ; 0 1 DY; 0 0 1]
for n=1:rev
thetaT = n*theta
R=[cosd(thetaT) sind(thetaT) 0 ; -sind(thetaT) cosd(thetaT) 0; 0 0 1]
Mshape = Trans*R*Transto0*Mshape
fill(Mshape(1,:),Mshape(2,:),'g')
axis([-8 8 -8 8])
Mshape
drawnow
end
S = 2
s =S/100;
steps = 100
Scale = [S 0 0 ; 0 S 0; 0 0 1]
for n=1:steps
Mshape =Scale*Mshape
fill(Mshape(1,:),Mshape(2,:),'g')
axis([-8 8 -8 8])
drawnow
end

답변 (1개)

Madhav Rajan
Madhav Rajan 2015년 8월 4일
편집: Madhav Rajan 2015년 8월 4일
I understand that you want to perform Matrix transformations such as rotation and scaling. Assuming that you want to scale or rotate a matrix M, you would have to first translate your matrix to the origin then perform the rotation or the scaling and then translate your matrix back to its original position. This is because scaling and rotation always happen with respect to the origin.
In the script attached, you can see the effect of applying scaling and rotation on a triangle which has not been translated to its origin and also the effect of first translating the object to the origin and then applying the transformations.
You can refer the following links for more information about scaling and rotations about the origin: http://www.willamette.edu/~gorr/classes/GeneralGraphics/Transforms/transforms2d.htm#Combining
You can also also refer the following documentation for 2d affine transformations in MATLAB: http://www.mathworks.com/help/images/performing-general-2-d-spatial-transformations.html

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by