基于Matlab的二维变换 C307 Lab-2:2D Geometric Transforms

 Exercise 1 - Answer

clear 
close all
% create box 
% first row is horizontal coordinates; second row is vertical coordinates

my_pts = [2 2 3 3 2;2 3 3 2 2];

% display the original box

figure(1)
plot(my_pts(1,1:end),my_pts(2,1:end),'b*-');

% write code here to create your 2D rotation matrix my_rot
xita=pi/6;
my_rot = [cos(xita) sin(xita);-sin(xita) cos(xita)];

% write code to perform rotation using my_rot and my_pts and store the result in my_rot_pts
my_rot_pts = my_rot*my_pts;

% Plot output
hold on; 
plot(my_rot_pts(1,1:end),my_rot_pts(2,1:end),'r*-');
axis([1.5 4.5 0 3.5]);

Exercise 2 - Answer

d_x = 1; % howmuchever you want to translate in horizontal direction 
d_y = 3; % howmuchever you want to translate in vertical direction
 
% create the original box 
% first row has horizontal coordinates, second row has vertical coordinates 

my_points = [1 1 2 2 1;1 2 2 1 1]; 

% Plot the original box 

figure(1) 
plot(my_points(1,1:end),my_points(2,1:end),'b*-'); 

% write code to create your Homogeneous 2D Translation matrix my_trans using d_x and d_y 
my_trans = [1 0 d_x;0 1 d_y;0 0 1];
 
% Next, we perform the translation 

% write code to convert my_points to the homogeneous system and store the result in hom_my_points 
hom_my_points = [my_points;ones(1,5)]; 

% write code to perform translation in the homogeneous system using my_trans and hom_my_points and store the result in trans_my_points 
trans_my_points = my_trans*hom_my_points; 

% Plot the Translated box (output) which has to be done in Cartesian, so... 
% cut out the X, Y points and ignore the 3rd dimension 

hold on 
plot(trans_my_points(1,1:end),trans_my_points(2,1:end),'r*-'); 

axis([0.5 3.5 0.5 5.5]);    % just to make the plot nicely visible

Exercise 3 - Answer

% we will perform a rotation followed by a translation, and then in reverse 
% order and compare the results, both using the homogeneous system
 
d_x = 1; % howmuchever you want to translate in horizontal direction
d_y = 1; % howmuchever you want to translate in vertical direction

% create original box 
% first row is horizontal and second row is vertical coordinates
 
my_pts = [3 3 4 4 3;3 4 4 3 3]; % Plot the box

% Plot the box
 
figure(1) 
plot(my_pts(1,1:end),my_pts(2,1:end),'b*-');

% write code here to create your 2D rotation matrix my_rot 
xita=pi/6;
my_rot = [cos(xita) sin(xita);-sin(xita) cos(xita)];

% write code to create your Homogeneous 2D Translation matrix hom_trans using d_x & d_y 
hom_trans = [1 0 d_x;0 1 d_y;0 0 1];
 
% Perform Compound transformation
 
% write code to construct your 2D Homogeneous Rotation Matrix using my_rot and storethe result in hom_rot 
% HINT: start with a 3x3 identity matrix and replace a part of it with my_rot to create hom_rot 
hom_rot = [my_rot [0;0];[0 0 1]]; 

% write code to convert my_pts to the homogeneous system and store the result in hom_my_pts 
hom_my_pts = [my_points;ones(1,5)]; 

% write code to perform in a single compound transformation: translation (hom_trans) followed by rotation (hom_rot) on hom_my_pts, and store the result in trans_my_pts 
trans_my_pts = hom_trans*hom_rot*hom_my_pts;

% Plot the transformed box (output) which has to be done in Cartesian, so... 
% cut out the X, Y points and ignore the 3rd dimension 

hold on 
plot(trans_my_pts(1,1:end),trans_my_pts(2,1:end),'r*-');

axis([2 8 -2 5]);    % just to make the plot nicely visible 
% Now, let us reverse the order of rotation and translation and compare
 
figure(2);
 
plot(my_pts(1,1:end),my_pts(2,1:end),'b*-');
  
% write code to perform in a single compound transformation: rotation followed by translation, and store the result in trans_my_pts
trans_my_pts = hom_rot*hom_trans*hom_my_pts;

% Plot the Transformed box (output) which has to be done in Cartesian, so... 
% cut out the X, Y points and ignore the 3rd dimension 
hold on 
plot(trans_my_pts(1,1:end),trans_my_pts(2,1:end),'r*-'); 
axis([2 8 -2 5]);       % just to make the plot nicely visible

猜你喜欢

转载自blog.csdn.net/qq_42276781/article/details/104144931
今日推荐