# 3d axis rotation

I have a vector V= and several line segments Seg1, Seg2, Seg3, Seg4.

I want to know how to rotate each of the line segments so that the X axis is parallel to my given vector.

How can I do this?

Note: I am aware that I can get the angle of rotation by taking the inner product of my vector and the x-unit vector (i.e. V DOT X) but I am unaware of what to do after this.

-
How is the vector related to the line segments? –  Matt Apr 25 '11 at 23:29
It is the average direction vector. –  basil Apr 26 '11 at 14:17

If the spherical coordinate representation of your vector $V$ is $\langle\rho,\phi,\theta\rangle$, where $\rho=|V|$, $\phi$ is the angle between $V$ and the positive $z$ axis, and $\theta$ is the angle between the positive $x$ axis and the projection of $V$ onto the $xy$ plane, then rotating the coordinate system by $\phi$ about the $y$ axis, then by $\theta$ about the $z$ axis should be sufficient. (This can be prefixed with an arbitrary rotation about the $x$ axis and still satisfy your criteria.) Using the rotation matrices from Wikipedia, $$\begin{bmatrix} x'\\ y'\\ z' \end{bmatrix}=\begin{bmatrix} \cos\theta & -\sin\theta & 0\\ \sin\theta & \cos\theta & 0\\ 0 & 0 & 1 \end{bmatrix}\begin{bmatrix} \cos\phi & 0 & \sin\phi\\ 0 & 1 & 0\\ -\sin\phi & 0 & \cos\phi \end{bmatrix}\begin{bmatrix} x\\ y\\ z \end{bmatrix}$$
@basil: $\phi=\arccos(\frac{V\cdot\langle 0,0,1\rangle}{|V|})$ (that is, use the dot product with the $z$ axis unit vector, since $\phi$ is the angle from the positive $z$ axis); if $V=\langle v_x,v_y,v_z\rangle$, then $\theta$ is determined by $v_x=r\cos\theta$ and $v_y=r\sin\theta$ where $r=\sqrt{v_x^2+v_y^2}$ (which means that $\tan\theta=\frac{v_y}{v_x}$, but beware of using the arctangent function blindly, as it $\theta$ might not be in the interval $(-\frac{\pi}{2},\frac{\pi}{2})$.) –  Isaac Apr 26 '11 at 16:23
ok that makes sense, so rotation around z is a better method than around x? also, from the above rotation matrices, i would do the cross product between the first two and then take the result and do the cross product with the third to get $x' = x*\cos\phi *(\cos\theta + sin\theta)$, $y' = y *(\cos\theta - \sin\theta)$ and $z' = z *\sin\phi (\cos\theta + \sin\theta)$, does this look correct? –  basil Apr 26 '11 at 19:34