Lesson 9 of 15

The Identity Matrix and Transpose

The Identity Matrix and Transpose

The Identity Matrix

The identity matrix is the matrix equivalent of the number 1 — multiplying any matrix or tuple by it returns the original:

I = [1 0 0 0]
    [0 1 0 0]
    [0 0 1 0]
    [0 0 0 1]

A useful factory function:

Matrix4 identity() {
    return Matrix4(1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1);
}

Transposing a Matrix

Transposing a matrix flips it along its diagonal — rows become columns:

M^T[i][j] = M[j][i]
Matrix4 transpose(Matrix4 M) {
    return Matrix4(
        M.get(0,0), M.get(1,0), M.get(2,0), M.get(3,0),
        M.get(0,1), M.get(1,1), M.get(2,1), M.get(3,1),
        M.get(0,2), M.get(1,2), M.get(2,2), M.get(3,2),
        M.get(0,3), M.get(1,3), M.get(2,3), M.get(3,3)
    );
}

Note: transpose(identity()) = identity().

Transposing is used internally when computing surface normals for transformed shapes.

Your Task

Implement transpose(Matrix4 M).

Given M = [1,2,3,4; 5,6,7,8; 9,8,7,6; 5,4,3,2]:

  • M^T[0][1] should equal M[1][0] = 5
  • M^T[1][0] should equal M[0][1] = 2

Expected output:

5
2
JSCPP loading...
Loading...
Click "Run" to execute your code.