# Determine the rotation necessary to transform one point on a sphere to another.

Given two points on the surface of a sphere in 3D space, is it possible to/how does one systematically determine the set of rotations around the x, y, and z-axes in order to transform one point to the other?

For example, on the unit sphere centred at the origin with a radius of 1, the points $(1, 0, 0)$ and $(0, \frac{1}{\sqrt{2}}, \frac{1}{\sqrt{2}})$ are both (I believe) on the sphere's surface. One can translate from $(1, 0, 0)$ to $(0, \frac{1}{\sqrt{2}}, \frac{1}{\sqrt{2}})$ by rotating $90^o$ degrees (counter-clockwise?) around the z-axis, then rotating $45^0$ degrees (clockwise?) around the x-axis.

I feel like the answer to this should be simple, but I have been struggling to answer it for a few days now.

EDIT: Just to add my attempt to follow Robert Israel's approach below with my real world example; perhaps someone can spot my mistake.

My input point is $(0.248729, -0.617981, 0.07822)$, the target is $(0.31209, -0.524457, 0.07822)$ - note that these both have a length (distance from $(0, 0, 0)$) of $0.670735...$, so are on the same sphere (give or take floating point errors.)

$a = 0.248739,\ b = -0.617981,\ c =0.07822,\ d = 0.31209,\ e = -0.524457,\ f =0.07822$

I compute $r = \sqrt{a^2 + b^2} = 0.6615811...$.

Therefore, $\theta = signum(b)arccos(a/r) = -1 * 1.1881482... = -1.1881482...$

$|d|$ is $0.31209$, which is less than $r$, so $\alpha$ can just be computed as $\alpha = arccos(d/r) - \theta = 2.271361...$

Rotating the original point by this, to $(d, g, h)$, gives $(0.31209, 0.588529, 0.078229)$ - this is good, since $d$ is correct and the length of this is also $0.670735...$, so it's on the sphere. Additionally, $g^2 + h^2 = 0.3524... = e^2 + f^2$.

However, this is where I flounder. I attempt to calculate $\phi$:

$g = \sqrt{g^2 + h^2} cos(\phi)$

$\phi = arccos(\sqrt{g^2 + h^2}/g)$

However, $\sqrt{g^2 + h^2}/g = 1.0087...$, so it doesn't work to take the $arccos$ of it... and now I don't know where to go.

-
If by $0.7071$ you mean $\frac{1}{\sqrt{2}}$, then yes. To do what you want it suffices to restrict your attention to the plane spanned by the two points you're interested in and then compute the angle between them using the dot product. Then a rotation around the axis perpendicular to the plane in question (which you can compute using the cross product) is what you want. – Qiaochu Yuan Feb 27 '12 at 20:27
@QiaochuYuan That's a good method, but I was looking to find the rotations around the x, y, and z-axes specifically. Thanks anyway. – Stephen Feb 27 '12 at 21:38
You want $\arccos(g/\sqrt{g^2+h^2})$, not $\arccos(\sqrt{g^2+h^2}/g)$. – Robert Israel Mar 1 '12 at 19:30

In general, suppose you want to start with $(a,b,c)$ and transform it to $(d,e,f)$ (where $a^2 + b^2 + c^2 = d^2 + e^2 + f^2 = 1$) using a rotation around the $z$ axis followed by a rotation around the $x$ axis.
Now rotation by angle $\alpha$ around the $z$ axis takes $(a,b,c)$ to $(a \cos(\alpha) - b \sin(\alpha), a \sin(\alpha) + b \cos(\alpha), c)$. Since a rotation around the $x$ axis preserves the $x$ coordinate, you need $a \cos(\alpha) - b \sin(\alpha) = d$. Write $a = r \cos(\theta)$, $b = r \sin(\theta)$ where $r = \sqrt{a^2 + b^2}$ and $\theta = \text{signum}(b) \arccos(a/r)$, and this equation becomes $r \cos(\alpha + \theta) = d$. For this to be possible you need $|d|\le r$, and then you can take $\alpha = \arccos(d/r) - \theta$. If the result of this rotation is $(d, g, h)$, we will have $g^2 + h^2 = 1 - d^2 = e^2 + f^2$, and rotation by angle $\beta$ around the $x$ axis will take this to $(d, g \cos(\beta) - h \sin(\beta), g \sin(\beta) + h \cos(\beta))$. Write $g = \sqrt{g^2+h^2} \cos(\phi)$, $h = \sqrt{g^2 + h^2} \sin(\phi)$, $e = \sqrt{g^2+h^2} \cos(\tau)$ and $f = \sqrt{g^2+h^2} \sin(\tau)$, and you'll see that $\beta = \tau - \phi$ will work.
Of course you can always throw in a rotation around the $y$ axis too (and you'll need one if $|d| > r$).
Is there a way to determine the correct order of rotation? As far as I can tell you need to rotate in a specific order; for example, trying to move $(0, 0, 1)$ to $(\frac{1}{\sqrt{2}}, \frac{1}{\sqrt{2}}, 0)$ doesn't work with the above method I believe. – Stephen Feb 27 '12 at 21:37
You can always do it with all three axes in any specific order. If you try to do it with just two, then there will be restrictions, e.g. as I said above, you need $|d| \le \sqrt{a^2+b^2}$ if you want to first rotate about $z$ and then about $x$. Similarly, for $x$ then $y$ you'd need $|e| \le \sqrt{b^2+c^2}$. At least one of ($x$ then $y$, $y$ then $z$, $z$ then $x$) will always work. Similarly for ($x$ then $z$, $z$ then $y$, $y$ then $x$). – Robert Israel Feb 28 '12 at 0:43
... actually at least two of them ($x$ then $y$, $y$ then $z$, $z$ then $x$), and at least two of the other three. – Robert Israel Feb 28 '12 at 2:21