# Rotate a vector about a given axis by the use of a quaternion

I encountered a problem in programming where I need to rotate a given vector about a given angle. To be precise, I need to change it to a quaternion so that I can later change it to a 4x4 matrix to perform transformation. I found the following solution to my problem:

Rotate vector 'vec' by an angle 'Angle' about an axis 'Axe'

SinHalfAngle = sinf(ToRadian(Angle/2));   //obtain sine angle
CosHalfAngle = cosf(ToRadian(Angle/2));   //obtain cosine angle

Rx = Axe.x * SinHalfAngle;           //x-component of Axe times sine angle
Ry = Axe.y * SinHalfAngle;
Rz = Axe.z * SinHalfAngle;
Rw = CosHalfAngle;

//those values are now used to make a quaternion
Quaternion RotationQ(Rx, Ry, Rz, Rw);
//obtain conjugate of quaternion and normalize it
Quaternion ConjugateQ = RotationQ.Conjugate();
ConjugateQ.Normalize();
Quaternion W = RotationQ * vec * ConjugateQ;


I am assuming that when 'vec' is multiplied by quaternion 'RotaionQ', I get the final orientation of 'vec'. However, I am not sure as to why the conjugate of quaternion is needed to obtain final transformation. Also, I did not quite understand how the values Rx, Ry and Rz were obtained. One of the sites I saw is this. So, any links or an answer would be very useful.

## 1 Answer

I'm going to explain in a few bullet points; any that you don't understand, just ask and I'll amplify.

1. The map $x \mapsto qx$ from $\mathbb R^4$ to $\mathbb R^4$ is called "left multiplication by $q$", and denoted $L_q$. If $q$ is a unit quarternion (sum of squares of entries is 1), then $L_q$ turns out to be a rotation of 4-space, and its inverse is $L_\bar{q}$, where $\bar{q}$ is the conjugate of $q$. (If $q = a + bi + cj + ck$, then $\bar{q} = a - bi - cj - dk$.
2. Because $L_q$ is a rotation, it preserves length and perpendicularity, i.e., if $v$ and $w$ are perpendicular vectors in 4-space (written as quaternions), then $L_q(v)$ and $L_q(w)$ are still perpendicular.
3. There's a corresponding map, $R_q(x) = x q$ called "right multplication by $q$", and it has the same properties as $L_q$: preserves lengths and perpendicularity, at least when $q$ is a unit quaternion.
4. The map $R(v) = R_\bar{q} (L_q(v)) = q v \bar{q}$, being a composition of rotations, is itself a rotation of $4$-space.
5. Let's look at the vector $n = (1,0,0,0)$, or in quaternion form, $n = 1 + 0i + 0j + 0k$. We have $R(n) = (q n) \bar{q}$. But $qn = q$ (check this!), so $R(n) = q \bar{q}$. And if $q$ is a unit vector, $q \bar{q}$, being the squared length of $q$, is just 1. So we have $R(n) = n$.
6. Since $R(n) = n$, if $v$ is a vector perpendicular to $n$ (i.e., a "pure vector quaternion," having the form $v = xi + yj + zk$), then $R(v)$ must be perpendicular to $R(n)$, i.e., to $n$...so $R(v)$ must also be a pure vector quaternion.
7. If we restrict our attention to pure vector quaternions, we have a rotation from this space to itself. That's called "the rotation associated to $q$".

In your code above, you compute the quaternions $W$; that will necessarily be a pure-vector quaternion of the form $xi + yj + zj$, so you can extract $(x,y,z)$ (I assume!), and this is your "rotation" applied to the vector $v$.

Slightly more surprising than the previous details is that the rotation $R$ associated to the unit quaternion $q$ has a few useful properties. Let's suppose that $$q = a + bi + cj + dk$$ then since $a^2 + (b^2 + c^2 + d^2) = 1$, there's some angle whose cosine is $a$, and whose sine is $\sqrt{b^2 + c^2 + d^2}$. I'm going to call than angle $\theta/2$. So now $$q = \cos(\theta/2) + \sin(\theta/2) b' i + c' j + d' k$$ where $(b', c', d')$ is a unit vector $u$ in the direction of $(b, c, d)$.

• In this situation, $R(u) = u$, which means that the vector $u$ is the axis of the rotation.
• Furthermore, the amount of rotation is exactly $\theta$.

Each of these requires proof, but ... I'm going to hope you can do it yourself. If not, I'll shamelessly suggest that you look at the sections on 3D rotations in Computer Graphics: Principles and Practice, 3rd edition, by Hughes et al. They cover this approach in detail, along with an analogy to rotations in 1D (not very interesting, but far easier to understand) represented by conjugation by a unit complex number. As the author of those bits, I personally think they do a pretty decent job. :)