# How to rotate a vector through another vector in the same direction?

I have two vectors $$v_{1}$$ and $$v_{2}$$, the angle between them is $$\theta$$

$$\theta\neq 0$$, hence both vectors share a plane.

Say we are operating in any arbitrary dimension, please how can I generally rotate vector $$v_{1}$$ through $$v_{2}$$ from $$0^\text{o}$$ to $$360^\text{o},$$ to get $$_{rot}v_{1}$$ for any value of $$\theta.$$

UPDATE
It seems there's a slight misconception about the second diagram, I should have made things clearer, here's a better representation of the problem. (Now $$\theta$$ is the angle between the two vectors $$v_1$$ and $$v_2$$, $$\phi$$ is the angle between $$v_1$$ and $$_{rot}v_1$$, therefore the problem has now evolved to address specifically how one may rotate $$v_1$$ via $$\phi$$ through $$v_2$$ to obtain $$_{rot}v_1$$, given $$\theta\neq 0$$ i.e. rotating $$v_1$$ on the plane it makes with $$v_2$$)

• Not sure I understand the final requirement (the second diagram doesn't match what I'd understand by "rotation by $\theta$"), but first do Gram-Schmidt to get an ordered orthonormal basis $(e_1,e_2)$ with first element $e_1 := v_1/|v_1|$; then let $(e_1', e_2')$ denote the rotated basis, and finally decompose the rotated $v_2$ as $(v_2 \cdot e_1)e_1' + (v_2\cdot e_2)e_2'$, see also math.stackexchange.com/questions/4373008/…. Commented Feb 19, 2022 at 21:46
• If you want people to see $\theta\neq 0$, write $\theta\neq 0$. By the way, in general, if you have a one-line equation, the entire equation should be just one MathJax expression ($... something ...$ or $$... something ...$$). Commented Feb 19, 2022 at 21:58
• There's a highly intuitive way to do this wihtout matrices. I made a video on this some time back: youtube.com/watch?v=3UAbwfkazxU Commented Feb 19, 2022 at 22:08
• In this particular case, you can find the axis vector by cross the two vectors (final and original)/ if you can specify the plane of rotation to begin with Commented Feb 19, 2022 at 22:09
• A common scheme is to transform the vectors by rotate the plane containing $v_1$ and $v_2$ to be aligned with the $x$-$y$ plane, then rotate in the $x$-$y$ plane as usual and undo the transformation. It will typically have the form $TRT^{-1}$ with $R$ the rotation and $T$ the planar alignment transformation. Commented Feb 19, 2022 at 22:39

There are so many questions about rotations of vectors already on this site, I was surprised when I failed to find one that is really asking just the same exact question.

There are some questions and answers that assume you already have a vector parallel to the desired axis of rotation. And you will get such a vector if you compute the cross product $$v_1 \times v_2$$, but I will show an alternative method that does not require a known rotation axis and works in any number of dimensions, not just 3D.

The trick is to express $$v_2$$ as a sum of two orthogonal unit vectors $$e_1$$ and $$e_2$$ in the same plane as $$v_1$$ and $$v_2.$$ While $$v_1$$ and $$v_2$$ are a basis for the vector subspace (the plane) within which you want to rotate $$v_1,$$ the vectors $$e_1$$ and $$e_2$$ are an orthonormal basis for that plane. We will construct this orthonormal basis so that $$e_1$$ points in the same direction as $$v_1.$$ Finding these vectors will mean that we would be able to write $$v_2 = c_1 e_1 + c_2 e_2$$ where $$c_1$$ and $$c_2$$ are some scalar values. It will be convenient to choose the orthonormal basis in such a way that $$c_2$$ is positive.

First we set $$e_1$$ to the unit vector in the same direction as $$v_1$$:

$$e_1 = \frac{1}{\lVert v_1\rVert} v_1.$$

Now it's simple to find $$c_1$$ using the dot product:

$$c_1 = v_2 \cdot e_1.$$

Let $$u_2 = c_2 e_2$$; then

$$u_2 = c_2 e_2 = v_2 - c_1 e_1 = v_2 - (v_2 \cdot e_1) e_1.$$

Set $$c_2$$ to the length of $$u_2$$, that is, $$c_2 = \lVert u_2\rVert$$; this guarantees that $$c_2$$ is positive. Then

$$e_2 = \frac{1}{c_2} u_2 = \frac{1}{\lVert u_2\rVert}u_2.$$

This is essentially just the Gram-Schmidt process applied to the basis $$v_1, v_2.$$

Having done all this, it turns out that \begin{align} c_1 &= \lVert v_2\rVert \cos \theta,\\ c_2 &= \lVert v_2\rVert\sin \theta, \end{align} that is,

$$v_2 = (\lVert v_2\rVert\cos\theta) e_1 + (\lVert v_2\rVert\sin\theta) e_2.$$

To rotate $$v_1$$ through an angle of exactly $$\theta$$ so that it points in exactly the same direction as $$v_2$$, compute

$$(\lVert v_1\rVert\cos\theta) e_1 + (\lVert v_1\rVert\sin\theta) e_2.$$

That's a vector just like $$v_2$$ except that its length is $$\lVert v_1\rVert$$ instead of $$\lVert v_2\rVert$$ -- just compare the formulas.

To rotate $$v_1$$ toward $$v_2$$ by some other angle $$\phi$$, just compute

$$\mathop{\mathrm{rot}_\phi} v_1 = (\lVert v_1\rVert\cos\phi) e_1 + (\lVert v_1\rVert\sin\phi) e_2.$$

Be careful when doing these operations that you are really working with 3D vectors in the usual mathematical sense; they represent only a direction and length, not specific starting and ending points. I add this warning to the answer because the first diagram illustrating the problem appears to show a basis or set of axes for your 3D coordinates, while the vectors are drawn as if they originated from a point other than the origin of the coordinates. Since the vectors in 3D space should be completely described by just three coordinates, it should be possible to simplify the figure by placing the intersection of the axes, the tails of vectors $$v_1$$ and $$v_2,$$ and the center of the circle all at the exact same point.

If the reason for drawing the figure as you did is because you are actually trying to find coordinates along a circle in 3D space with a center that is not at the origin, you'll want to find a vector $$v_c$$ from the origin to the center of the circle and then add $$v_1'$$ (the rotated $$v_1$$) to the vector $$v_c$$. The coordinates of $$v_c + v_1'$$ will be coordinates of a point on the circle.

• Hi, is $v_1’$ the same as $_{rot}v_1$ , I just tried your implementation. I got NaN as the value of my $_{rot}v_1$ components, Also I made an update to the question to reflect $\phi$ Commented Feb 20, 2022 at 11:24
• There are not many ways for NaN to creep into these formulas. The $\lVert\cdot\rVert$ function uses a square root, but the input should be a sum of squares and therefore non-negative. $\lVert v_1\rVert$ should be positive (not zero), assuming the components of $v_1$ are not so tiny that they disappear when you square them. Assuming $v_1$ and $v_2$ are independent (which they should be as long as $\theta$ is not any multiple of $180$ degrees), it should be impossible for $v_2 - (v_2 \cdot e_1) e_1$ (which is a linear combination of $v_1$ and $v_2$) to come out to zero. Commented Feb 20, 2022 at 16:34
• Somewhere along the way one of the intermediate computations must have had an unexpected result. If it were my code I would examine intermediate results of computation until I found where NaN first showed up, and then figure out why. Commented Feb 20, 2022 at 16:40
• You could also get wrong results by applying sin and cos functions directly to degree measures of angles instead of radians, but I've never seen that result in NaN. Commented Feb 20, 2022 at 16:41
• Yeah, I have figured it out (my gram-schmidt implementation was a bit incorrect), Your answer is exactly what I was looking for, everything is working perfectly now, thanks. Commented Feb 20, 2022 at 21:56