Vector/Matrix                                                                                        Home : www.sharetechnote.com Transformation of Multiple Segments : Two Segments   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;   AxisRange = 4; L1 = 2;   vertSeg1 = [-0.5 -0.5 -0.5;  ...             -0.5 0.5 -0.5;  ...             (0.5 + L1) 0.5 -0.5;  ...             (0.5 + L1) -0.5 -0.5; ...             -0.5 -0.5 0.5; ...             -0.5 0.5 0.5;  ...             (0.5 + L1) 0.5 0.5; ...             (0.5 + L1) -0.5 0.5];     facSeg1 = [1 2 3 4; ...            2 6 7 3; ...            4 3 7 8; ...            1 5 8 4; ...            1 2 6 5; ...            5 6 7 8];   vertHand1 = [0.00 0.5 -0.5;  ...              0.5 0.5 -0.5;  ...              0.5 1.0 -0.5;  ...              1.5 1.0 -0.5;  ...              1.5 0.5 -0.5;  ...              1.0 0.5 -0.5;  ...              1.0 -0.5 -0.5;  ...              1.5 -0.5 -0.5;  ...              1.5 -1.0 -0.5;  ...              0.5 -1.0 -0.5;  ...              0.5 -0.5 -0.5;  ...              0.00 -0.5 -0.5;  ...              0.00 0.5 0.5;  ...              0.5 0.5 0.5;  ...              0.5 1.0 0.5;  ...              1.5 1.0 0.5;  ...              1.5 0.5 0.5;  ...              1.0 0.5 0.5;  ...              1.0 -0.5 0.5;  ...              1.5 -0.5 0.5;  ...              1.5 -1.0 0.5;  ...              0.5 -1.0 0.5;  ...              0.5 -0.5 0.5;  ...              0.00 -0.5 0.5;  ...             ];     facHand1 = [1 2 11 12; ...             2 3 4 5; ...               2 6 7 11; ...             11 8 9 10; ...             13 14 23 24; ...             14 15 16 17; ...               14 18 19 23; ...             23 20 21 22; ...             1 12 24 13; ...             1 13 14 2; ...             2 14 15 3; ...             3 15 16 4; ...             4 16 17 5; ...             5 17 18 6; ...             6 18 19 7; ...             7 19 20 8; ...             8 20 21 9; ...             9 21 22 10; ...             10 22 23 11; ...             11 23 24 12; ...             ];   phiXSeg1 = 0; TxMatrixXSeg1 = [1       0             0        0;...                  0 cos(phiXSeg1) -sin(phiXSeg1) 0; ...                  0 sin(phiXSeg1) cos(phiXSeg1)  0; ...                  0       0             0        1];          phiYSeg1 = pi/6; TxMatrixYSeg1 = [cos(phiYSeg1)  0 -sin(phiYSeg1) 0;...                  0              1      0         0; ...                  sin(phiYSeg1)  0  cos(phiYSeg1) 0; ...                  0              0      0         1];          phiZSeg1 = 0; TxMatrixZSeg1 = [cos(phiZSeg1) -sin(phiZSeg1)  0  0;...                  sin(phiZSeg1) cos(phiZSeg1)   0  0; ...                  0                 0           1  0; ...                  0                 0           0  1];          TxMatrixShiftScaleHand1 = [1        0        0        L1;...                            0        1        0        0; ...                            0        0        1        0; ...                            0        0        0        1];               phiXHand1 = pi/4; TxMatrixXHand1 = [1       0             0        0;...                  0 cos(phiXHand1) -sin(phiXHand1) 0; ...                  0 sin(phiXHand1) cos(phiXHand1)  0; ...                  0       0             0        1];          phiYHand1 = 0; TxMatrixYHand1 = [cos(phiYHand1)  0 -sin(phiYHand1) 0;...                  0              1      0         0; ...                  sin(phiYHand1)  0  cos(phiYHand1) 0; ...                  0              0      0         1];          phiZHand1 = 0; TxMatrixZHand1 = [cos(phiZHand1) -sin(phiZHand1)  0  0;...                  sin(phiZHand1) cos(phiZHand1)   0  0; ...                  0                 0           1  0; ...                  0                 0           0  1];   % transformed object VerticesHomSeg1 = zeros(8,4); for i = 1:8     VerticesHomSeg1(i,:) = [vertSeg1(i,:) 1]; end       TxMatrixRotSeg1 = TxMatrixZSeg1 * TxMatrixYSeg1 * TxMatrixXSeg1;              TxVerticesHomSeg1 = TxMatrixRotSeg1 * VerticesHomSeg1'; TxVerticesHomSeg1 = TxVerticesHomSeg1';   TxVerticesSeg1 = zeros(8,3); for i = 1:8     TxVerticesSeg1(i,:) = TxVerticesHomSeg1(i,1:3); end   % transforming object VerticesHomHand1 = zeros(24,4); for i = 1:24     VerticesHomHand1(i,:) = [vertHand1(i,:) 1]; end   TxMatrixRotSeg1 = TxMatrixZSeg1 * TxMatrixYSeg1 * TxMatrixXSeg1;   TxMatrixRotHand1 = TxMatrixXHand1 * TxMatrixYHand1 * TxMatrixZHand1; TxVerticesHomHand1 = TxMatrixRotSeg1 ...                     * TxMatrixShiftScaleHand1 ...                     * TxMatrixRotHand1 ...                     * VerticesHomHand1'; TxVerticesHomHand1 = TxVerticesHomHand1';   TxVerticesHand1 = zeros(24,3); for i = 1:24     TxVerticesHand1(i,:) = TxVerticesHomHand1(i,1:3); end   % plotting transformed arm and hand --------------------------------------- subplot(2,2,1); patch('Faces',facSeg1,'Vertices',TxVerticesSeg1,'FaceColor','b'); patch('Faces',facHand1,'Vertices',TxVerticesHand1,'FaceColor','g');  % draw the red cube axis([-AxisRange AxisRange -AxisRange AxisRange -AxisRange AxisRange]); grid(); material shiny; alpha('color'); alphamap('rampdown'); view(30,30); title('transformed'); set(gca,'xticklabel',[]);set(gca,'yticklabel',[]); set(gca,'zticklabel',[]);   % view along y-axis (x-z plane) subplot(2,2,2); patch('Faces',facSeg1,'Vertices',TxVerticesSeg1,'FaceColor','b'); patch('Faces',facHand1,'Vertices',TxVerticesHand1,'FaceColor','g');   axis([-AxisRange AxisRange -AxisRange AxisRange -AxisRange AxisRange]); grid(); material shiny; alpha('color'); alphamap('rampdown'); view(0,0); title('x-z plane'); set(gca,'xticklabel',[]);set(gca,'yticklabel',[]); set(gca,'zticklabel',[]);   % view along x-axis (y-z plane) subplot(2,2,3); patch('Faces',facSeg1,'Vertices',TxVerticesSeg1,'FaceColor','b'); patch('Faces',facHand1,'Vertices',TxVerticesHand1,'FaceColor','g'); axis([-AxisRange AxisRange -AxisRange AxisRange -AxisRange AxisRange]); grid(); material shiny; alpha('color'); alphamap('rampdown'); view(90,0); title('y-z plane'); set(gca,'xticklabel',[]);set(gca,'yticklabel',[]); set(gca,'zticklabel',[]);   % view along z-axis (x-y plane) subplot(2,2,4); patch('Faces',facSeg1,'Vertices',TxVerticesSeg1,'FaceColor','b'); patch('Faces',facHand1,'Vertices',TxVerticesHand1,'FaceColor','g');   axis([-AxisRange AxisRange -AxisRange AxisRange -AxisRange AxisRange]); grid(); material shiny; alpha('color'); alphamap('rampdown'); view(0,90); title('x-y plane'); set(gca,'xticklabel',[]);set(gca,'yticklabel',[]); set(gca,'zticklabel',[]);     I created another program for the same transformation, but just to show the detailed steps of transformation for each segment step by step. Following is the result of the programe. You may need a large screen to get the detailed look. The source code may look too intimidating, but it is just long.. the mathematical logic is exactly same as above. It got long just because of too many plots. Get the source from here and try it.