Game Development Reference
/** Gets the squared magnitude of this vector. */
real squareMagnitude() const
/** Turns a non-zero vector into a vector of unit length. */
real l = magnitude();
if (l > 0)
Notice that I've also added a function to calculate the square of the magnitude of
a vector. This is a faster process because it avoids the call to sqrt , which can be slow
on some machines. There are many cases where we don't need the exact magnitude
and where the square of the magnitude will do. For this reason it is common to see a
squared magnitude function in a vector implementation.
S CALAR AND V ECTOR M ULTIPLICATION
In the normalization equations I have assumed we can multiply a scalar (1 /d )bya
vector. This is a simple process, given by the formula
In other words we multiply a vector by a scalar by multiplying all the components of
the vector by the scalar.
To divide a vector by a scalar, we make use of the fact that
which is how we arrived at the normalization equation 2.2 from equation 2.1.