MATLAB: How to create two rectangles with the same shape, one stay at specific position that created by using matrix, and the other change its position from 0 to 90 degree

MATLAB

How can I create two rectangles with the same shape, one stay at specific position that created by using matrix, and the other change its position from 0 to 90 degree? I got the code below just for the changing one. Let me know if somgthing I do wrong. Thank you.
%% Main Code
% Initialise the graph
hSquare = plot (NaN, NaN , '-b'); %Open empty graph. NaN - Not a number.
axis([-10 10 -10 10]); grid on;
% Main
X = [2 6 6 2 2];
Y = [2 2 4 4 2];
x1 = [get(hSquare, 'XData') X];
y1= [get(hSquare, 'YData') Y];
set(hSquare, 'XData', x1, 'YData', y1);
drawnow;
for thetad =1:90
R = [cosd(thetad) -sind(thetad); sind(thetad) cosd(thetad)];
x2 = X(1:4);
y2 = Y(1:4);
V = R*[X;Y];% do the rotation
Vt = V(1,:);
Vb = V(2,:);
plot(Vt,Vb,'r-');
axis([-10 10 -10 10]); grid on;
drawnow;
pause(0.05);
end

Best Answer

%% Main Code
% Initialise the graph
hSquare = plot (NaN, NaN , '-b'); %Open empty graph. NaN - Not a number.
axis([-10 10 -10 10]); grid on;
% Main
X = [2 6 6 2 2];
Y = [2 2 4 4 2];
Xm = mean(X) ;
Ym = mean(Y) ;
x1 = [get(hSquare, 'XData') X];
y1= [get(hSquare, 'YData') Y];
set(hSquare, 'XData', x1, 'YData', y1);
drawnow;
for thetad =1:90
R = [cosd(thetad) -sind(thetad); sind(thetad) cosd(thetad)];
x2 = X(1:4);
y2 = Y(1:4);
V = R*([X;Y]-[Xm;Ym])+[Xm;Ym];% do the rotation relative to the centre of the square
Vt = V(1,:);
Vb = V(2,:);
plot(X,Y,'b',Vt,Vb,'r-');
axis([-10 10 -10 10]); grid on;
drawnow;
pause(0.05);
end