Vector/Matrix                                                                                        Home : www.sharetechnote.com

 

 

 

Example - Rotation and Translation

 

This is an example of rotating and translating a 3D object (a Cube) using a Transformation Matrix in the form as shown below. The transformation matrix is applied (multiplied) to each vertex of the cube.

 

 

The result of this example is as shown below.

 

 

Following is the MatLab code for this example. For now, don't pay too much about the code itself, just focus on number marked in red and try to undertand the mathematical meaning intuitively. You can just copy the code here into your Matlab and change the numbers in red part until you develop the intuitive understanding.

Note : When copy and paste this code, '...' may cause some error. In that case, erase '...' and retry '...' in your matlab editor.

    clear all;

     

    vert = [-0.5 -0.5 -0.5;  ...

            -0.5 0.5 -0.5;  ...

             0.5 0.5 -0.5;  ...

             0.5 -0.5 -0.5; ...

            -0.5 -0.5 0.5; ...

             -0.5 0.5 0.5;  ...

              0.5 0.5 0.5; ...

              0.5 -0.5 0.5];

       

    fac = [1 2 3 4; ...

        2 6 7 3; ...

        4 3 7 8; ...

        1 5 8 4; ...

        1 2 6 5; ...

        5 6 7 8];

     

    % original object

    subplot(2,4,1);

    patch('Faces',fac,'Vertices',vert,'FaceColor','r');  

    axis([-2 2 -2 2 -2 2]);

    grid();

    material shiny;

    alpha('color');

    alphamap('rampdown');

    view(30,30);

    title('original');

     

    % view along y-axis (x-z plane)

    subplot(2,4,2);

    patch('Faces',fac,'Vertices',vert,'FaceColor','r');  

    axis([-2 2 -2 2 -2 2]);

    grid();

    material shiny;

    alpha('color');

    alphamap('rampdown');

    view(0,0);

    title('x-z plane');

     

    % view along x-axis (y-z plane)

    subplot(2,4,5);

    patch('Faces',fac,'Vertices',vert,'FaceColor','r');  

    axis([-2 2 -2 2 -2 2]);

    grid();

    material shiny;

    alpha('color');

    alphamap('rampdown');

    view(90,0);

    title('y-z plane');

     

    % view along z-axis (x-y plane)

    subplot(2,4,6);

    patch('Faces',fac,'Vertices',vert,'FaceColor','r');  

    axis([-2 2 -2 2 -2 2]);

    grid();

    material shiny;

    alpha('color');

    alphamap('rampdown');

    view(0,90);

    title('x-z plane');

     

     

    % transforming the object

     

    VerticesHom = zeros(8,4);

    for i = 1:8

        VerticesHom(i,:) = [vert(i,:) 1];

    end

     

    phi = pi/6;

    TxMatrix = [1    0       0       1;...

                0 cos(phi) -sin(phi) 0; ...

                0 sin(phi) cos(phi)  0; ...

                0    0       0       1];

    TxVerticesHom = TxMatrix * VerticesHom';

    TxVerticesHom = TxVerticesHom';

     

    TxVertices = zeros(8,3);

    for i = 1:8

        TxVertices(i,:) = TxVerticesHom(i,1:3);

    end

     

    % transformed object

    subplot(2,4,3);

    patch('Faces',fac,'Vertices',TxVertices,'FaceColor','b');  

    axis([-2 2 -2 2 -2 2]);

    grid();

    material shiny;

    alpha('color');

    alphamap('rampdown');

    view(30,30);

    title('transformed');

     

    % view along y-axis (x-z plane)

    subplot(2,4,4);

    patch('Faces',fac,'Vertices',TxVertices,'FaceColor','b');  

    axis([-2 2 -2 2 -2 2]);

    grid();

    material shiny;

    alpha('color');

    alphamap('rampdown');

    view(0,0);

    title('x-z plane');

     

    % view along x-axis (y-z plane)

    subplot(2,4,7);

    patch('Faces',fac,'Vertices',TxVertices,'FaceColor','b');  

    axis([-2 2 -2 2 -2 2]);

    grid();

    material shiny;

    alpha('color');

    alphamap('rampdown');

    view(90,0);

    title('y-z plane');

     

    % view along z-axis (x-y plane)

    subplot(2,4,8);

    patch('Faces',fac,'Vertices',TxVertices,'FaceColor','b');  

    axis([-2 2 -2 2 -2 2]);

    grid();

    material shiny;

    alpha('color');

    alphamap('rampdown');

    view(0,90);

    title('x-z plane');