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.