Lesson 3 of 15
Negating and Scaling Tuples
Negating and Scaling Tuples
Two more fundamental tuple operations: negation and scalar multiplication.
Negation
Negating a tuple flips all components. For a vector, this reverses its direction:
// -tuple(1, -2, 3, -4) = tuple(-1, 2, -3, 4)
Tuple negate(Tuple a) {
return Tuple(-a.x, -a.y, -a.z, -a.w);
}
Scalar Multiplication
Multiplying by a scalar scales all components. For a vector, this changes its length:
// tuple(1, -2, 3, -4) * 3.5 = tuple(3.5, -7, 10.5, -14)
Tuple scale(Tuple a, double t) {
return Tuple(a.x * t, a.y * t, a.z * t, a.w * t);
}
You can also divide by a scalar — just multiply by its reciprocal:
Tuple divide(Tuple a, double t) {
return scale(a, 1.0 / t);
}
Your Task
Implement negate(Tuple a) and scale(Tuple a, double t).
Expected output:
-1 2 -3 4
3.5 -7 10.5 -14JSCPP loading...
Loading...
Click "Run" to execute your code.