Convert Two Vectors To each Other

  • Thread starter Thread starter mamali
  • Start date Start date
  • Tags Tags
    Convert Vectors
AI Thread Summary
To convert one vector to another using a rotation matrix in C++, the algorithm involves creating orthogonal matrices based on the input vectors. For 3D vectors, the rotation matrix can be derived using the cross product to find the axis of rotation and the dot product to determine the angle. The rotation matrix is constructed by applying transformations around the appropriate axes, ensuring the determinant remains 1. The discussion emphasizes the importance of understanding the geometric interpretation of the rotation, including the plane of rotation and the orientation. Overall, a solid grasp of linear algebra and matrix operations is essential for implementing this function effectively.
mamali
Messages
5
Reaction score
0
Hi .

I want a to write a function in c++ that get two vectors ( whith same size ) and return the rotation matrix to convert first one to second .

But i don't know the algorithm and formula . can anyone help me please ?

thanks ... :)
 
Mathematics news on Phys.org
Are these 2D or 3D vectors? In general, to find a rotation matrix R that maps a vector u to a vector v (assumed to be normalized) you need to create two orthogonal matrices U and V whose first columns are u and v, respectively. Make sure that the determinant is 1 in both cases (rather than -1), and the rotation you are looking for is

R = V U^T

In 3D, a good way to build these matrices is to calculate w =\frac{u \times v}{|u \times v|} and set U=(u, w, u \times w) and V=(v, w, v \times w).
 
Last edited:
I'm sure monea83's method is valid, but I wanted to give a more visual explanation.

The big questions about a rotation matrix are

1) What plane does it rotate in?
2) In which orientation?
3) How much does it rotate?

The plane it rotates it will be the plane containing the origin and your start and end vectors.

The orientation of the plane is decided by choosing a normal vector to the plane. The normal to the plane is found using the cross product of your start and end vectors (and then normalizing the result).

The amount of rotation (in radians) can be found by using the dot product. (Which gives the cosine of the angle between the vectors, times the length of both vectors).

Knowing this much, there are formulas for coming up with the appropriate rotation matrices

http://en.wikipedia.org/wiki/Rotation_matrix

I'd personally just plug in the formula, but you can also derive the formula yourself with a bit of work. You essentially break all vectors v into a vector that lies on your plane and a vector that is perpendicular to it, associate the points on the plane you found above with R^2, do a simple two-dimensional rotation on that plane, revert back, then add the perpendicular parts back with the newly rotated parts.
 
Okay, here's my very simple minded take on it:

You want to rotate so that vector \vec{v}= x_1\vec{i}+ y_1\vec{j}+ z_1\vec{k} is mapped into \vec{u}= x_2\vec{i}+ y_2\vec{j}+ z_2\vec{k}.

As monea83 and Tac-Tics said, your axis of rotation will be aligned with the cross product, \vec{u}\times\vec{v}= \alpha\vec{i}+ \beta\vec{j}+ \gamma\vec{k} with \alpha= y_1z_2- y_2z_1, \beta= z_1x_2- z_2x_1, and \gamma= x_1y_2- x_2y_1.

The angle of rotation is given by
cos(\theta)= \frac{\vec{u}\cdot\vec{v}}{|\vec{u}||\vec{v}|}= \frac{x_1x_2+ y_1y_2+ z_1z_2}{\sqrt{x_1^2+ y_1^2+ z_1^2}\sqrt{x_2^2+ y_2^2+ z_2^2}}
If the two vectors have the same length, so that we really are rotating one into the other and not just along the same direction, that denominator is x_1^2+ y_1^2+ z_1^2= x_2^2+ y_2^2+ z_2^2. In fact, it wouldn't change the problem to take \vec{u} and \vec{v} to be unit vectors so that we have simply cos(\theta)= x_1x_2+ y_1y_2+ z_1z_2. We don't really need to calculate \theta itself- we only need cos(\theta) and sin(\theta)= \sqrt{1- cos^2(\theta)}.

Now, I presume you know this but some reading this may not- in two dimensions, to rotate vector (x, y) through angle \theta around the origin you need the matrix multiplication
\begin{bmatrix}cos(\theta) & -sin(\theta) \\ sin(\theta) & cos(\theta)\end{bmatrix}\begin{bmatrix}x \\ y \end{bmatrix}

To see that that is true, just look at what happens to the "basis vectors" (1, 0) and (0, 1).
\begin{bmatrix}cos(\theta) & -sin(\theta) \\ sin(\theta) & cos(\theta)\end{bmatrix}\begin{bmatrix}1 \\ 0 \end{bmatrix}= \begin{bmatrix}cos(\theta) \\ sin(\theta)\end{bmatrix}
Dropping a perpendicular from the endpoint (cos(\theta), sin(\theta)) to the x- axis gives a right triangle with "opposite side" cos(\theta) and "near side" sin(\theta) which does, in fact, give a triangle with angle \theta at the origin. That, transforming (1, 0) into (cos(\theta), sin(\theta)) is, in fact, rotating it through angle \theta.

Similarly,
\begin{bmatrix}cos(\theta) & -sin(\theta) \\ sin(\theta) & cos(\theta)\end{bmatrix}\begin{bmatrix}0 \\ 1 \end{bmatrix}= \begin{bmatrix}-sin(\theta) \\ cos(\theta)\end{bmatrix}
and dropping a perpendicular from that endpoint to the y-axis shows that we have a right triangle with angle at the origin of \theta.

In three dimensions, if we rotate around the z-axis, we are just rotating the x and y components as in two dimensions while z stays the same. That is, rotation through angle \theta around the z axis is given by
\begin{bmatrix}cos(\theta) & -sin(\theta) & 0 \\ sin(\theta) & cos(\theta) & 0 \\ 0 & 0 & 1\end{bmatrix}\begin{bmatrix}x \\ y \\ z\end{bmatrix}

From that, by the symmetry of Cartesian coordinates, it is easy to see that rotation through angle \theta about the y-axis is given by
\begin{bmatrix}cos(\theta) & 0 -sin(\theta) \\ 0 & 1 & 0 \\ sin(\theta) & 0 & cos(\theta) \end{bmatrix}\begin{bmatrix}x \\ y \\ z\end{bmatrix}
and that rotation through angle \theta about the x-axis is given by
\begin{bmatrix}1 & 0 & 0 \\ 0 & cos(\theta) & -sin(\theta)\\ 0 & sin(\theta) & cos(\theta)\end{bmatrix}\begin{bmatrix}x \\ y \\ z\end{bmatrix}

To rotate through angle \theta around the arbitrary vector \vec{U}= \alpha\vec{i}+ \beta\vec{j}+ \gamma\vec{k}, we use the following strategy:
1) Rotate around the z-axis so that vector \vec{U} is rotated to the vector \vec{V} in the xz-plane.
2) Rotate around the y-axis so that the vecotr \vec{V} is rotated to the vector \vec{W} parallel to the z-axis.
3) Rotate around the z-axis through angle \theta.
4) Reverse the rotation in (2).
5) Reverse the rotation in (3).
(Apparently this was too long to be posted as a single post so I cut it into two.)
 
Last edited by a moderator:
More specifically, we want to rotate around the z-axis so that the vector
\vec{U}= \alpha\vec{i}+ \beta\vec{j}+ \gamma\vec{k}
is rotated into the vector
\vec{V}= r\vec{i}+ \gamma\vec{k}
where, since rotation does not change the length of a vector, r= \sqrt{\alpha^2+ \beta^2}. Since the specific angle is not necessary here, I will write "c" for "cos(\theta)" and "s" for "sin(\theta)".

With that notation we want
\begin{bmatrix}c & -s & 0 \\ s & c & 0 \\ 0 & 0 & 1\end{bmatrix}\begin{bmatrix}\alpha \\ \beta \\ \gamma\end{bmatrix}= \begin{matrix}r \\ 0 \\ \gamma\end{bmatrix}

That gives the equations \alpha c- \beta s= r and \alpha s+ \beta c= 0. From the second equation, \s= -(\beta/\alpha) so the first equation becomes c(\alpha+ \beta^2/alpha)= c(\alpha^2+ \beta^2)/\alpha= c(r^2/\alpha)= r so that c= \alpha/r and then s= -(\beta/\alpha)(\alpha/r)= -\beta/r.

That tells us that our rotation matrix is
\begin{bmatrix}\frac{\alpha}{r} & \frac{\beta}{r} & 0 \\ -\frac{\beta}{r} & \frac{\alpha}{r} & 0 \\ 0 & 0 & 1\end{bmatrix}.

Now we want to rotate around the y-axis to rotate (r, 0, \gamma) into (0, 0, \rho) where, again because the length of the vector remains constant, \rho= \sqrt{r^2+ \gamma^2}= \sqrt{\alpha^2+ \beta^2+ \gamma^2}. We need a matrix multiplication of the form
\begin{bmatrix}c & 0 & -s \\ 0 & 1 & 0 \\s & 0 & c\end{bmatrix}\begin{bmatrix}r \\ 0 \\ \gamma \end{bmatrix}= \begin{bmatrix}rc- \gamma s \\ 1 \\ rs+ \gamma c\end{bmatrix}= \begin{bmatrix}0 \\ 0 \\ \rho\end{bmatrix}.

That is, now we have rc- \gamma s= 0 and rs+ \gamma c= \rho. From the first equation, s= (r/\gamma)c so the second equation becomes c(r^2/\gamma+ \gamma)= c((r^2+\gamma^2)/\gamma)= c(\rho^2/\gamma)= \rho so that c= \gamma/\rho and s= r/\rho.

The matrix is
\begin{bmatrix}\frac{\gamma}{\rho} & 0 & -\frac{r}{\rho} \\ 0 & 1 & 0 \\ \frac{r}{\rho} & 0 & \frac{\gamma}{\rho}\end{bmatrix}.

Now, of course, the matrix to rotate through angle \theta about the z- axis is
\begin{bmatrix}cos(\theta) & -sin(\theta) & 0 \\ sin(\theta) & cos(\theta) & 0 \\ 0 & 0 7 1\end{bmatrix}

The last two steps, reversing the first two are easy- reversing a rotation is just rotating around the same axis with the negative angle. And, since cosine is an even function and sine is an odd function, and sine only shows up in the off-diagonal terms, we just need to change signs on the off-diagonal terms in the previous matrices.

Putting that all together, to rotate vector x\vec{i}+ y\vec{j}+ z\vec{k} through angle \theta around axis \alpha\vec{i}+ \beta\vec{j}+ \gamma\vec{k} we have to perform the following matrix multiplications:

\begin{bmatrix}\frac{\alpha}{r} & -\frac{\beta}{r} & 0 \\ \frac{\beta}{r} & \frac{\alpha}{r} & 0 \\ 0 & 0 & 1\end{bmatrix}\begin{bmatrix}\frac{\gamma}{\rho} & 0 & \frac{r}{\rho} \\ 0 & 1 & 0 \\ \frac{r}{\rho} & 0 & -\frac{\gamma}{\rho}\end{bmatrix}\begin{bmatrix}cos(\theta) & -sin(\theta) & 0 \\ sin(\theta) & cos(\theta) & 0 \\ 0 & 0 7 1\end{bmatrix}\begin{bmatrix}\frac{\gamma}{\rho} & 0 & -\frac{r}{\rho} \\ 0 & 1 & 0 \\ \frac{r}{\rho} & 0 & \frac{\gamma}{\rho}\end{bmatrix}\begin{bmatrix}\frac{\alpha}{r} & \frac{\beta}{r} & 0 \\ -\frac{\beta}{r} & \frac{\alpha}{r} & 0 \\ 0 & 0 & 1\end{bmatrix}\begin{bmatrix}x \\ y \\ z\end{bmatrix}
 
Last edited by a moderator:
Seemingly by some mathematical coincidence, a hexagon of sides 2,2,7,7, 11, and 11 can be inscribed in a circle of radius 7. The other day I saw a math problem on line, which they said came from a Polish Olympiad, where you compute the length x of the 3rd side which is the same as the radius, so that the sides of length 2,x, and 11 are inscribed on the arc of a semi-circle. The law of cosines applied twice gives the answer for x of exactly 7, but the arithmetic is so complex that the...
Is it possible to arrange six pencils such that each one touches the other five? If so, how? This is an adaption of a Martin Gardner puzzle only I changed it from cigarettes to pencils and left out the clues because PF folks don’t need clues. From the book “My Best Mathematical and Logic Puzzles”. Dover, 1994.
Thread 'Imaginary Pythagoras'
I posted this in the Lame Math thread, but it's got me thinking. Is there any validity to this? Or is it really just a mathematical trick? Naively, I see that i2 + plus 12 does equal zero2. But does this have a meaning? I know one can treat the imaginary number line as just another axis like the reals, but does that mean this does represent a triangle in the complex plane with a hypotenuse of length zero? Ibix offered a rendering of the diagram using what I assume is matrix* notation...

Similar threads

Back
Top