I recently worked out the matrix multiplications necessary to rotation a vector around a given axis through a given angle. That is related to this problem.
First, you need to know that, in two dimensions, the matrix that rotates around the origin through angle is of the form . You can see that is true by looking at what it does to the "basis" vectors and :
which is clearly a vector making angle with the x-axis and
which is clearly a vector making angle with the y-axis.
In three dimensions it is easy extend that and see that rotates through an angle around the z-axis and then to see that and rotate around the x and y axes, respectively.
To rotate through angle , around arbitrary axis , use the following strategy:
1) Rotate around the z- axis so that the vector is rotated into in the xz-plane. That is, rotate so that the y component is 0. Since a rotation preserves length, we must have .
2) Rotate around the y- axis so that the vector is rotated into the vector pointing along the z-axis. Again, since a rotation preserves length, we must have .
3) Rotate around the z- axis through angle
4) Rotate back reversing the rotation in (2).
5) Rotate back reverseing the rotation in (3).
The rotation in (1), since it is about the z-axis, is of the form . Since the specific angle is not relevant, I am going to call that .
That is, we must have .
That gives the two equations and . From the second equation, . Putting that into the first equation, so that . From that, .
That is, the matrix required for the first rotation is .
Now, for (2) we need to rotate around the y-axis. Specifically, we need .
That gives the two equations and . From the first equation, . Putting that into the second equation, so that and then .
That is, the matrix giving the rotation in (2) is .
The rotation in (3), about the z-axis through angle is, of course, .
The matrix in (4) is the inverse of the matrix in (2) so we are rotating around the y-axis but through the negative angle. Since cosine is an even function and sine is odd, that just changes the sign on the "s" terms. The matrix needed is
The matrix is (5) is the inverse of the matrix is (1) so we just need to change the signs on the "s" terms. The matrix is
Putting that all together, to rotate the vector , around the axis vector through angle , do the matrix multiplications:
where, again, and .
That is to rotate around the vector , through angle .
To rotate so that vector is rotated into vector , we need to rotate around an axis vector perpendicular to both through the angle they make with each other. Since it is direction that is important here, not length, it is sufficient to assume that both have length 1. If not, just divide each by its length to make that true.
The vector perpendicular to both is, of course, the cross product of the two vectors: . That is our " above: , , and .
The angle between the two vectors is given by their dot product: . Since it is direction that is important here, not length, it is sufficient to assume that both have length 1. If not, just divide each by its length to make that true. With [math