Lesson 11 of 15

Scaling

Scaling

A scaling transformation stretches or shrinks along each axis independently:

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

Properties

  • Uniform scaling: use the same value for all three axes → sphere stays a sphere
  • Non-uniform scaling: different values → stretches a sphere into an ellipsoid
  • Reflection: use a negative scale factor to flip along an axis: scaling(-1, 1, 1) reflects across the yz-plane

Scaling Vectors and Points

Unlike translation, scaling affects both points and vectors:

scaling(2,3,4) * point(-4,6,8) = point(-8, 18, 32)
scaling(2,3,4) * vector(-4,6,8) = vector(-8, 18, 32)

Building Complex Objects

Combine translation and scaling to position objects in your scene. A unit sphere at origin scaled by 2 gives a sphere of radius 2:

Matrix4 transform = scaling(2, 2, 2);
// Apply to each ray: transform the ray by the inverse transform instead

Your Task

Implement scaling(double x, double y, double z) using the Matrix4 and applyMatrix infrastructure from the previous lesson.

Expected output:

-8 18 32 1
JSCPP loading...
Loading...
Click "Run" to execute your code.