Length and squared length
Like vectors, the squared length of a quaternion is the same as the dot product of the quaternion with itself. The length of a quaternion is the square root of the square length:
- Implement the
lenSq
function inquat.cpp
and declare the function inquat.h
:float lenSq(const quat& q) { return q.x * q.x + q.y * q.y + q.z * q.z + q.w * q.w; }
- Implement the
len
function inquat.cpp
. Don't forget to add the function declaration toquat.h
:float len(const quat& q) { float lenSq = q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w; if (lenSq< QUAT_EPSILON) { return 0.0f; } return sqrtf(lenSq); }
Quaternions that represent a rotation should always have a length of 1. In the next section, you will learn about unit quaternions, which always have a length of 1.