Lesson 10 of 15

Translation

Translation

A translation moves a point in space. In matrix form, a translation by (x, y, z) is:

[1 0 0 x]
[0 1 0 y]
[0 0 1 z]
[0 0 0 1]
Matrix4 translation(double x, double y, double z) {
    return Matrix4(1,0,0,x, 0,1,0,y, 0,0,1,z, 0,0,0,1);
}

Applying to Tuples

Multiply the matrix by the tuple:

Tuple applyMatrix(Matrix4 M, Tuple t) {
    return Tuple(
        M.get(0,0)*t.x + M.get(0,1)*t.y + M.get(0,2)*t.z + M.get(0,3)*t.w,
        M.get(1,0)*t.x + M.get(1,1)*t.y + M.get(1,2)*t.z + M.get(1,3)*t.w,
        M.get(2,0)*t.x + M.get(2,1)*t.y + M.get(2,2)*t.z + M.get(2,3)*t.w,
        M.get(3,0)*t.x + M.get(3,1)*t.y + M.get(3,2)*t.z + M.get(3,3)*t.w
    );
}

Key Property: Vectors Are Unaffected

Since vectors have w = 0, the translation terms (x*w, y*w, z*w) vanish. Translation only moves points, never directions.

translation(5,-3,2) * point(-3,4,5) = point(2, 1, 7)   ✓
translation(5,-3,2) * vector(-3,4,5) = vector(-3, 4, 5) (unchanged!)

Your Task

Implement translation(double x, double y, double z) and applyMatrix(Matrix4 M, Tuple t).

Expected output:

2 1 7 1
-3 4 5 0
JSCPP loading...
Loading...
Click "Run" to execute your code.