Rotation of a point in 3 dimensional space by theta about an arbitrary axes defined by a line between two points P_{1} = (x_{1},y_{1},z_{1}) and P_{2} = (x_{2},y_{2},z_{2}) can be achieved by the following steps
(2) rotate space about the x axis so that the rotation axis lies in the xz plane (3) rotate space about the y axis so that the rotation axis lies along the z axis (4) perform the desired rotation by theta about the z axis (5) apply the inverse of step (3) (6) apply the inverse of step (2) (7) apply the inverse of step (1)
Note:
Step 1 Translate space so that the rotation axis passes through the origin. This is accomplished by translating space by P_{1} (x_{1},y_{1},z_{1}). The translation matrix T and the inverse T^{1} (required for step 7) are given below
Step 2 Rotate space about the x axis so that the rotation axis lies in the xz plane. Let U = (a,b,c) be the unit vector along the rotation axis. and define d = sqrt(b^{2} + c^{2}) as the length of the projection onto the yz plane. If d = 0 then the rotation axis is along the x axis and no additional rotation is necessary. Otherwise rotate the rotation axis so that is lies in the xz plane. The rotation angle to achieve this is the angle between the projection of rotation axis in the yz plane and the z axis. This can be calculated from the dot product of the z component of the unit vector U and its yz projection. The sine of the angle is determine by considering the cross product.
The rotation matrix R_{x} and the inverse R_{x}^{1} (required for step 6) are given below
Step 3 Rotate space about the y axis so that the rotation axis lies along the positive z axis. Using the appropriate dot and cross product relationships as before the cosine of the angle is d, the sine of the angle is a. The rotation matrix about the y axis R_{y} and the inverse R_{y}^{1} (required for step 5) are given below.
Step 4 Rotation about the z axis by t (theta) is R_{z} and is simply
The complete transformation to rotate a point (x,y,z) about the rotation axis to a new point (x`,y`,z`) is as follows, the forward transforms followed by the reverse transforms.
Using quaternionsTo rotate a 3D vector "p" by angle theta about a (unit) axis "r" one forms the quaternion and the rotation quaternion The rotated vector is the last three components of the quaternion It is easy to see that rotation in the opposite direction (theta) can be achieved by reversing the order of the multiplication. Note also that the quaternion Q_{2} is of unit magnitude.
