Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

***THIS MUST BE DONE IN MATLAB*** 7. Consider the square in EXAMPLE 9. The goal

ID: 3748160 • Letter: #

Question

***THIS MUST BE DONE IN MATLAB***

7. Consider the square in EXAMPLE 9. The goal of this exercise is to bring back the square to its original position by first translating it horizontally to the left two units using 20 iterations, and then rotating it counterclockwise /2 radians around the point (1, 0) using 5 iterations. This can be done by modifying the code in EXAMPLE 9 by adding two for loops. The first loop should translate the square while the second should rotate it around the point (1,0). Note that the rotation is counterclockwise, while in EXAMPLE 9 it was clockwise. Include the M-file. You do not need to include the figure.

EXAMPLE In this example we first translate the square horizontally 2 units using increments of 0.1. We then rotate the resulting square clockwise /2 radians around the vertex (3.0) using increments of /10 radians clf s-[0 , î , î ,0 , 0; 0 ,0 , 1 , 1 , 0; 1 , 1 , 1 , 1 , 1] ; % define the square in homogeneous coordinates M1- [1,0,0. 1:0, 1,0;0,0,1]; % define the first translation matrix theta pi/10: % define the angle theta q-[cos(theta),-sin(thet a) ,0; sin(thet a),cos(theta),0;0,0,1]; % rotation matrix about the origin QP-[1,0 , 3:0, 1, 0:0 ,0, 1]«Q,* [1,0,-3:0, î ,0;0,0,1] ; % define the rotation matrix around (3,0) p- plot(S (1, :),S(2, :)); % plot the original tsquare axis([-0.5,5,-0.5,2]), grid on axis egual figure(gcf) for i1:20 S M1*S; % compute the translated square set (p, Xdat a ,,S(1, :),,ydata,, S (2, :)); % plot the translated square pause(0.2) SAP#8; % compute the rotated square set(p, Xdata,,S(1, ,,ydata,,S(2, :)) ; pause(0.2) % plot the rotated square

Explanation / Answer

clf
S=[0,1,1,0,0;0,0,1,1,0;1,1,1,1,1]; %define the square in homogeneous coordinates
M1 = [1,0,0.1;0,1,0;0,0,1]; %define the first translation matrix
theta = pi/10; % define the angle theta
Q = [cos(theta), - sin(theta),0;sin(theta), cos(theta),0;0,0,1]; %rotaion matrix about the origin
QP = [1,0,3;0,1,0;0,0,1]*Q'*[1,0,-3;0,1,0;0,0,1]; %define the rotation matrix around (3,0)
p = plot(S(1,:),S(2,:)); %plot the original taquare
axis([-0.5,5,-0.5,2]), grid on
axis equal
figure(gcf)
for i = 1:20
   S = M1*S; %compute the translated square
   set(p, 'xdata',S(1,:),'ydata',S(2,:)) %plot the translated square
   pause(0.2)
end
for i = 1:5
   S = QP*S; %compute the rotated square
   set(p, 'xdata',S(1,:),'ydata',S(2,:)) %plot the rotated square
   pause(0.2)
end

%Code for Reversing the translated matrix

M2 = [1,0,-0.1;0,1,0;0,0,1]; %define the first translation matrix
Q = [cos(theta), sin(theta),0; - sin(theta), cos(theta),0;0,0,1]; %rotaion matrix about the origin
QP = [1,0,1;0,1,0;0,0,1]*Q'*[1,0,-1;0,1,0;0,0,1]; %define the rotation matrix around (1,0)
for i = 1:20
   S = M2*S; %compute the translated square
   set(p, 'xdata',S(1,:),'ydata',S(2,:)) %plot the translated square
   pause(0.2)
end
for i = 1:5
   S = QP*S; %compute the rotated square
   set(p, 'xdata',S(1,:),'ydata',S(2,:)) %plot the rotated square
   pause(0.2)
end