I have two vectors, $v$ and $u$. How do I rotate $u$ around the x-, y-, and z-axes (or one axis) so that it points in the same direction as $v$?
-
No need to "rotate": divide $v$ by its norm, then multiply by the norm of $u$. – David Mitra Feb 27 '12 at 16:57
-
@David: I don't understand; scaling doesn't change whether $u$ and $v$ are linearly dependent. – Zev Chonoles Feb 27 '12 at 16:59
-
@ZevChonoles Maybe I misunderstood the question. I thought he ultimately wanted a vector in the direction of $v$ that had length $\Vert u\Vert$. – David Mitra Feb 27 '12 at 17:02
-
But if $u$ is not initially in the same direction as $v$, scaling won't change that. – Zev Chonoles Feb 27 '12 at 17:07
-
@David, to clarify, $u$ and $v$ are not initially pointing in the same direction. I'd like to rotate $u$ so that it points in the same direction as $v$, not rescale it. – FlyWheel Feb 27 '12 at 17:08
-
@ZevChonoles I'm just saying (or trying to) that if you take $u$ and rotate it so that the resulting vector $w$ points in the same direction as $v$, then $w$ is just the vector I described ($v$ scaled appropriately). Am I missing something here? – David Mitra Feb 27 '12 at 17:11
-
@David, I think I understand your comment now, but my question is about how one would work out the set of rotations. – FlyWheel Feb 27 '12 at 17:12
-
@David: Aha! I understand now. Your solution is very clever :) However FlyWheel might be interested in knowing the corresponding linear transformation, not just the end result. – Zev Chonoles Feb 27 '12 at 17:13
-
"... if you take u and rotate it so that the resulting vector w points in the same direction as v, then w is just the vector I described..." Yes, that's correct. – FlyWheel Feb 27 '12 at 17:13
-
@ZevChonoles "FlyWheel might be interested in knowing the corresponding linear transformation, not just the end result." Yes, exactly. – FlyWheel Feb 27 '12 at 17:14
-
Their dot product divided by absolute values product is constant? – Narasimham Apr 09 '16 at 03:00
4 Answers
Two 3-vectors define a plane. Rotation in that plane (i.e. about normal vector of that plane) brings one vector to another. So the quick sketch for the solution would be:
- find normal vector to the common plane (I think this is just the vector product $u \times v$)
- find rotation angle $\theta$ using dot product ($\theta = \cos^{-1}(\frac{u \cdot v}{||u|| ||v||})$)
- express the rotation using some axis-angle representation (axis is the normal vector from 1. and angle is the $\theta$ from 2.)
I am curious if someone expresses the straightforward formula here...

- 1,307
Quaternions are what you are looking for, don't let the word scare you it is very easy, here is matlab code to do what you want. (Quaternions allow for easy rotations about any arbitrary axis)
% example vectors
u = [1/sqrt(3),1/sqrt(3),1/sqrt(3)]; % vector to rotate
v = [0 0 1]; % vector to align to
% angle between vectors
theta = acos(dot(u,v)/(norm(u)*norm(v)));
% find axis perpendicular to u and v (cross product) to rotate about
r = cross(u,v);
% rotation axis must be a unit vector
r = r/norm(r);
% quaternion to rotate [0,ux,uy,uz]
q1 = [0,u];
% quaternion rotation matrix, rotates angle theta about axis r
q2 = [cos(theta/2),r(1)*sin(theta/2),r(2)*sin(theta/2),r(3)*sin(theta/2)];
q3 = quatmultiply(q2,q1);
q3 = quatmultiply(q3,quatconj(q2));
u_new = q3(2:4);
% plot the vectors
figure(1)
clf
hold on
plot3([0,u(1)],[0,u(2)],[0,u(3)],'b')
plot3([0,v(1)],[0,v(2)],[0,v(3)],'k')
plot3([0,u_new(1)],[0,u_new(2)],[0,u_new(3)],'r--')
axis equal
grid on

- 21
-
also check out here for more detailed info, it also explains how to do this with standard rotation matrix, its not as elegant, http://paulbourke.net/geometry/rotate/ – wmohr Apr 09 '16 at 02:38
I believe this article can help: Arbitrary Axis Rotation

- 1,869
- 13
- 12
-
Thanks, that's a nice link. However, my question is about how one actually comes up with a set of rotations so that $u$ can point in the same direction as $v$. – FlyWheel Feb 27 '12 at 17:09
If $u,v\in\mathbb R^2$, Find the angle between $u$ and $v$ by $$\cos\theta=\frac{<u,v>}{|u|.|v|}$$ Now take matrix of rotation $A_\theta$ of angle $\theta$. Now take $A_\theta u$ or $A_{-\theta} u$. These will rotate $u$ to the direction of $v$. $$A_\theta= \left( \begin{array}{cc} \cos\theta &-\sin\theta \\ \sin\theta &\cos\theta \\ \end{array} \right) $$
For $u,v\in \mathbb R^3$,
Write $v= (a,b,c)$ and $u= (x,y,z)$. If all x,y,z is non zero, then we want $T$ such that $Tu = v$ , define $T$ such that $$T(e_1)= \frac{a}{x} e_1$$
$$T(e_2)=\frac{b}{y} e_2$$
$$T(e_3)= \frac{c}{z}e_3$$ Then we have $T(x,y,z)= (a,b,c)$. That is rotation matrix is matrix of $T$ that is $$\left(
\begin{array}{ccc}
\frac{a}{x } &0 & 0 \\
0 &\frac{b}{y} &0 \\
0 & 0 &\frac{c}{z} \\
\end{array}
\right)$$
If some of x,y,z is zero, then case reduces to $\mathbb R^2$ keeping one axis fixed.
Example: Assume $u= (1,2,3)$ and $v= (2,-5,6)$ Then rotation matrix which take $u$ to $v$ is $\left( \begin{array}{ccc} \frac{2}{1} &0 & 0 \\ 0 &\frac{-5}{2} &0 \\ 0 & 0 &\frac{6}{3}\\ \end{array} \right)= $ $\left( \begin{array}{ccc} 2 &0 & 0 \\ 0 &\frac{-5}{2} &0 \\ 0 & 0 &2 \\ \end{array} \right)$
putting value of $a,b,c$ and $x,y,z $ you may have many more example.... If some of $x,y,z$ is what happen see and if didn't get comment it.. i will give example for that too.

- 1,896
- 13
- 20
-
-
@FlyWheel, read this article http://en.wikipedia.org/wiki/Transformation_matrix – zapkm Feb 27 '12 at 17:27
-
@FlyWheel http://math.stackexchange.com/questions/92206/matrix-of-linear-transformation This question may help you in leaning find matrix of linear transformation.. – zapkm Feb 27 '12 at 17:29
-
I really appreciate you taking the time to answer my question. But if you could spare a moment, could you give me a simple example of rotating $u$ to $v$ in 3-space? – FlyWheel Feb 27 '12 at 17:32
-
-
Thanks, I've accepted your answer. I would upvote, but I need to register first! – FlyWheel Feb 27 '12 at 17:45
-
1What on earth? This is not a rotation matrix at all. It does not preserve the lengths of vectors. – Feb 06 '13 at 17:00