Engineering Math - Matrix

 

 

 

 

Translation in 3D

 

This is an example of translating a 3D object (a Cube) using a translation vector. A vector (translation vector) is added 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;

     

    % vertices of the cube

    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');

     

    % transformed object

    % This code add a vector to each vertices of the cube and store the result into TxVertices

    TxVector = [1 0 0];

    TxVertices = zeros(8,3);

    for i = 1:8

        TxVertices(i,:) = vert(i,:) + TxVector;

    end

     

    % transformed object

    subplot(2,4,3);

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

    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');