Define a point in 2-d space as a column vector (x,y)=(1,-2)
x=1; y=-2;
Draw line from the origin with a blue dotted line.
plot([0 x],[0 y],'b:');
add a blue circle at end, without erasing.
hold on;
plot(x, y, 'bo');
Now add labels, scale axes and add grid.
xlabel('x'); ylabel('y');
title('The point (x,y) = (1,-2)');
axis([-3 3 -3 3], 'square'); grid on
Define transformation matrix for a -90 degree rotation
theta = -90 * pi/180;
T=[cos(theta) sin(-theta) 0;
sin(theta) cos(theta) 0;
0 0 1]
T =
0.0000 1.0000 0
-1.0000 0.0000 0
0 0 1.0000
Perform rotation and plot Note, for the transformations we add a third row that is always 1
xy=[x; y; 1]
xy =
1
-2
1
xyp=T*xy
xyp =
-2.0000
-1.0000
1.0000
xp=xyp(1)
yp=xyp(2)
xp =
-2
yp =
-1.0000
Plot
plot([0 xp],[0 yp],'b-','Linewidth',2);
plot(xp,yp,'bo','MarkerFaceColor','b');
title('The point (1,-2) rotated by -90^\circ')
hold off; %Next plot will clear the screen
Now for a translation, tx=1, ty=4
tx=1; ty=4;
T=[1 0 tx; 0 1 ty; 0 0 1]
T =
1 0 1
0 1 4
0 0 1
do transformation
xyp=T*xy
xp=xyp(1); yp=xyp(2);
xyp =
2
2
1
Plot both points
plot([0 x],[0 y],'b:'); hold on; plot(x, y, 'bo');
xlabel('x'); ylabel('y');
plot([0 xp],[0 yp],'b-','Linewidth',2);
plot(xp,yp,'bo','MarkerFaceColor','b');
title('The point (1,-2) translated by t_x=1.0, t_y=4');
axis([-3 3 -3 3],'square'); grid on
hold off; %Next plot will clear the screen
theta=-90*pi/180; tx=1; ty=4;
T=[cos(theta) sin(-theta) tx;
sin(theta) cos(theta) ty;
0 0 1]
T =
0.0000 1.0000 1.0000
-1.0000 0.0000 4.0000
0 0 1.0000
do transformation
xyp=T*xy
xp=xyp(1); yp=xyp(2);
xyp =
-1
3
1
Plot both points
plot([0 x],[0 y],'b:'); hold on; plot(x, y, 'bo');
xlabel('x'); ylabel('y');
plot([0 xp],[0 yp],'b-','Linewidth',2);
plot(xp,yp,'bo','MarkerFaceColor','b');
title('(1,-2) Rotation: -90^\circ, Translation: t_x=1, t_y=4');
axis([-3 3 -3 3], 'square'); grid on;
hold off; %Next plot will clear the screen
Clear the figure, set axis limits, grid
clf; axis([-3 3 -3 3], 'square'); grid on;
Define a patch by its vertices
x=[0 2 2]; y=[0 1 0];
myTriangle=patch(x,y,'r');
Rotate by 45 degrees and translate by 1, -2
xy=[x; y; ones(size(x))]
xy =
0 2 2
0 1 0
1 1 1
theta=45*pi/180; tx=1; ty=-2;
T=[cos(theta) sin(-theta) tx;
sin(theta) cos(theta) ty;
0 0 1]
T =
0.7071 -0.7071 1.0000
0.7071 0.7071 -2.0000
0 0 1.0000
do transformation
xyp=T*xy
xyp =
1.0000 1.7071 2.4142
-2.0000 0.1213 -0.5858
1.0000 1.0000 1.0000
Pull out new x and y values
xp=xyp(1,:)
yp=xyp(2,:)
xp = 1.0000 1.7071 2.4142
yp = -2.0000 0.1213 -0.5858
Update shape
set(myTriangle,'Xdata',xp,'Ydata',yp);