# Find Rotation Matrix to rotate axes and move coordinates of point from P0 to P1

I have a point $P_0 = [x_0, y_0, z_0]'$. I want to rotate the axes so that the new coordinates will be $P_1 = [x_1, y_1, z_1]'$. Define the following rotation matrices:

$R_x = \left[\matrix{ 1 & 0 & 0\\ 0 & \cos\alpha & - \sin\alpha\\ 0 & \sin\alpha & \cos\alpha} \right]$, $R_y = \left[\matrix{ \cos\beta & 0 & \sin\beta\\ 0 & 1 & 0\\ -\sin\beta & 0 & \cos\beta} \right]$, $R_z = \left[\matrix{ \cos\gamma & -\sin\gamma & 0\\ \sin\gamma & \cos\gamma & 0\\ 0 & 0 & 1} \right]$ and $R_{xyz} = R_x R_y R_z$.

I want $P_1 = R_{xyz} P_0$ and $\left[\alpha, \beta, \gamma\right]$ be such that $\alpha, \beta, \gamma = 0$ if $P_1 = P_0$; $\alpha, \beta = 0$ if the rotation can be obtained by setting only $\gamma$; $\alpha = 0$ if the rotation can be obtained by setting only $\beta$ and $\gamma$. Any hint?

EDIT: Following JordiC's answer: yes, the distance to the origin is the same for $P_0$ and $P_1$; I can set $\alpha = 0$ and the rotation matrix will be simplified:

$R = \left[\matrix{ \cos\beta \cos\gamma & -\cos\beta \sin\gamma & \sin\beta\\ \sin\gamma & \cos\gamma & 0\\ -\cos\gamma \sin\beta & \sin\beta \sin\gamma & \cos\beta} \right]$.

I have found this: Find rotation that maps a point to its target which seems to be the same question.

If you use only rotations to convert the coordinates from P0 to P1, you will need only 2 rotations for the conversion. Also the distance to the origin of P0 and P1 must be the same. In this case you can always consider that:

α = 0.

So you have only to think in β and γ.

And β will be always 0 if z0 = z1.

• Ok, I have clarified my question. Now I have three trigonometric equations in two unknown angles. Is there a simple way to solve for $\beta$ and $\gamma$? – randomatlabuser Nov 9 '13 at 9:56

Ok, I have found a quick fix. Short example with comments in Matlab.

% P0 and P1 are two vectors on a unit sphere:
P0 = randn(3, 1); P0 = P0 / norm(P0);
P1 = randn(3, 1); P1 = P1 / norm(P1);

% theta and phi are the respective spherical coordinates:
phi_0 = acos(P0(3));
theta_0 = atan2(P0(2), P0(1));
phi_1 = acos(P1(3));
theta_1 = atan2(P1(2), P1(1));

% Show that the coordinates are equivalent:
P0b = zeros(3, 1);
P0b(1) = sin(phi_0) .* cos(theta_0);
P0b(2) = sin(phi_0) .* sin(theta_0);
P0b(3) = cos(phi_0);
all(fix(1e12 * (P0 - P0b)) == 0)

% R1: Rotation matrix to move P0 to the north pole:
P0_y = -phi_0; P0_z = -theta_0;
R1 = [cos(P0_y) * cos(P0_z) -cos(P0_y) * sin(P0_z) sin(P0_y);
sin(P0_z) cos(P0_z) 0;
-sin(P0_y) * cos(P0_z) sin(P0_y) * sin(P0_z) cos(P0_y)];

% R2: Rotation matrix to move P1 to the north pole ...
P1_y = -phi_1; P1_z = -theta_1;
R2 = [cos(P1_y) * cos(P1_z) -cos(P1_y) * sin(P1_z) sin(P1_y);
sin(P1_z) cos(P1_z) 0;
-sin(P1_y) * cos(P1_z) sin(P1_y) * sin(P1_z) cos(P1_y)];
% ... and back to its original position:
R2 = R2 ^ (-1);

% Rotation matrix to move P0 to the north pole and from the north pole to P1
R = R2 * R1;

% New coordinates of P0 after rotation:
P0_rot = R * P0;

% Check that the new coordinates of P0 are the same as P1
all(fix(1e12 * (P0_rot - P1)) == 0)

% Rotation matrix to move back the rotated point P0 to its original coordinates
R_back = R^(-1);
% Coordinates rotated back
P0_rot_back = R_back * P0_rot;
% Check the new coordinates of P0, once rotated back:
all(fix(1e12 * (P0_rot_back - P0)) == 0)

I hope there is a more obvious way to do it, but for now this solution seems to work fine.