Convert Two Vectors To each Other

In summary, to write a function in C++ that converts one vector to another using a rotation matrix, you need to calculate the cross product of the two vectors to determine the axis of rotation and the angle of rotation. Then, using the appropriate rotation matrices for each axis, you can rotate the first vector to align with the second vector. Finally, you can use the inverse of the rotation matrices to transform the rotated vector back to its original basis. This will give you the rotation matrix needed to convert the first vector to the second. However, it is important to note that the length of the vectors must be the same for this method to work properly.
  • #1
mamali
5
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
  • #2
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

[tex]R = V U^T[/tex]

In 3D, a good way to build these matrices is to calculate [tex]w =\frac{u \times v}{|u \times v|}[/tex] and set [tex]U=(u, w, u \times w)[/tex] and [tex]V=(v, w, v \times w)[/tex].
 
Last edited:
  • #3
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.
 
  • #4
Okay, here's my very simple minded take on it:

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

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

The angle of rotation is given by
[tex]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}}[/tex]
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 [itex]x_1^2+ y_1^2+ z_1^2= x_2^2+ y_2^2+ z_2^2[/itex]. In fact, it wouldn't change the problem to take [itex]\vec{u}[/itex] and [itex]\vec{v}[/itex] to be unit vectors so that we have simply [itex]cos(\theta)= x_1x_2+ y_1y_2+ z_1z_2[/itex]. We don't really need to calculate [itex]\theta[/itex] itself- we only need [itex]cos(\theta)[/itex] and [itex]sin(\theta)= \sqrt{1- cos^2(\theta)}[/itex].

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

To see that that is true, just look at what happens to the "basis vectors" (1, 0) and (0, 1).
[tex]\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}[/tex]
Dropping a perpendicular from the endpoint [itex](cos(\theta), sin(\theta))[/itex] to the x- axis gives a right triangle with "opposite side" [itex]cos(\theta)[/itex] and "near side" [itex]sin(\theta)[/itex] which does, in fact, give a triangle with angle [itex]\theta[/itex] at the origin. That, transforming (1, 0) into [itex](cos(\theta), sin(\theta))[/itex] is, in fact, rotating it through angle [itex]\theta[/itex].

Similarly,
[tex]\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}[/tex]
and dropping a perpendicular from that endpoint to the y-axis shows that we have a right triangle with angle at the origin of [itex]\theta[/itex].

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 [itex]\theta[/itex] around the z axis is given by
[tex]\begin{bmatrix}cos(\theta) & -sin(\theta) & 0 \\ sin(\theta) & cos(\theta) & 0 \\ 0 & 0 & 1\end{bmatrix}\begin{bmatrix}x \\ y \\ z\end{bmatrix}[/tex]

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

To rotate through angle [itex]\theta[/itex] around the arbitrary vector [itex]\vec{U}= \alpha\vec{i}+ \beta\vec{j}+ \gamma\vec{k}[/itex], we use the following strategy:
1) Rotate around the z-axis so that vector [itex]\vec{U}[/itex] is rotated to the vector [itex]\vec{V}[/itex] in the xz-plane.
2) Rotate around the y-axis so that the vecotr [itex]\vec{V}[/itex] is rotated to the vector [itex]\vec{W}[/itex] parallel to the z-axis.
3) Rotate around the z-axis through angle [itex]\theta[/itex].
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:
  • #5
More specifically, we want to rotate around the z-axis so that the vector
[tex]\vec{U}= \alpha\vec{i}+ \beta\vec{j}+ \gamma\vec{k}[/tex]
is rotated into the vector
[tex]\vec{V}= r\vec{i}+ \gamma\vec{k}[/tex]
where, since rotation does not change the length of a vector, [itex]r= \sqrt{\alpha^2+ \beta^2}[/itex]. Since the specific angle is not necessary here, I will write "c" for "[itex]cos(\theta)[/itex]" and "s" for "[itex]sin(\theta)[/itex]".

With that notation we want
[tex]\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}[/tex]

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

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

Now we want to rotate around the y-axis to rotate [itex](r, 0, \gamma)[/itex] into [itex](0, 0, \rho)[/itex] where, again because the length of the vector remains constant, [itex]\rho= \sqrt{r^2+ \gamma^2}= \sqrt{\alpha^2+ \beta^2+ \gamma^2}[/itex]. We need a matrix multiplication of the form
[tex]\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}[/tex].

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

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

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

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 [itex]x\vec{i}+ y\vec{j}+ z\vec{k}[/itex] through angle [itex]\theta[/itex] around axis [itex]\alpha\vec{i}+ \beta\vec{j}+ \gamma\vec{k}[/itex] we have to perform the following matrix multiplications:

[tex]\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}[/tex][tex]\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}[/tex]
 
Last edited by a moderator:

1. How do I convert a vector from one coordinate system to another?

To convert a vector from one coordinate system to another, you will need to use a transformation matrix. This matrix will represent the relationship between the two coordinate systems and can be used to convert the vector using matrix multiplication.

2. What is the difference between a vector and a coordinate system?

A vector is a mathematical representation of direction and magnitude, while a coordinate system is a framework used to define the position of points in space. Vectors can be converted between coordinate systems, but they are fundamentally different concepts.

3. Can I convert a 3D vector to a 2D vector?

Yes, it is possible to convert a 3D vector to a 2D vector. However, this conversion will result in loss of information as the 2D vector will only represent the projection of the 3D vector onto a 2D plane.

4. Is there a specific formula for converting vectors to each other?

Yes, there are specific formulas for converting vectors to each other based on the type of transformation needed (e.g. rotation, translation, scaling). These formulas involve using transformation matrices and basic vector operations.

5. Can I convert a vector to a different unit of measurement?

Yes, you can convert a vector to a different unit of measurement by scaling the vector accordingly. For example, if you want to convert a vector from meters to centimeters, you would multiply each component of the vector by 100.

Similar threads

  • General Math
Replies
11
Views
1K
  • General Math
Replies
4
Views
1K
  • General Math
Replies
20
Views
1K
Replies
12
Views
3K
Replies
2
Views
1K
  • Programming and Computer Science
Replies
10
Views
1K
Replies
1
Views
1K
  • General Math
Replies
3
Views
2K
Replies
2
Views
1K
Replies
5
Views
4K
Back
Top