# How to find the rotation matrix of a circle of a sphere that is perpendicular with a vector on the sphere's surface

Hello, Imagine having a sphere of ray R and then you randomly pick a point on the surface of said sphere
(x1=x0+ ray* sin(theta)cos(sigma) , y1= y0+ ray sin(theta)sin(sigma), z1= z0+ ray math.cos(theta)) where (x0,y0,z0) represent the center of the sphere and you know theta and sigma.

Now the line that passes through this point and the center of the sphere makes a 90 degree angle with the plane of another circle on this sphere, but what I want to know is how do I get a rotation matrix that applied to the circle that cuts the sphere only in the xy (z=z0, the dotted circle) will make a 90 degree angle with the aforementioned line.

• What is the plane of a rotation matrix? Is it the plane perpendicular to the matrix's axis of rotation? The image of the $x,y$ plane when the rotation is applied? Something else? Commented Apr 5, 2020 at 15:11
• @David K The ideea is that I want to rotate the circle that has z=z0 on all of its points to be perpendicular with my vector (x1,y1,z1). Commented Apr 5, 2020 at 15:39

Remember that a rotation matrix will only give a rotation around an axis that passes through the origin. So you cannot just apply a rotation matrix.

First subtract $$(x_0,y_0,z_0)$$ from the coordinates of any point you want to rotate. Then apply the rotation matrix. Then add $$(x_0,y_0,z_0)$$ to the rotated coordinates.

With that in mind, the rotation will actually be performed on a sphere whose center is at $$(0,0,0)$$ (after subtracting $$(x_0,y_0,z_0)$$ and before adding $$(x_0,y_0,z_0)$$ back again).

The simplest rotation, conceptually, is to simply rotate the circle about the axis through the two points where the two circles should intersect. This axis is perpendicular to the vector $$(x_1-x_0,y_1-y_0,z_1-z_0),$$ and also perpendicular to the projection of that vector onto the $$x,y$$ plane. The two intersection points are therefore $$\left(r \cos\left(\sigma + \frac\pi2\right), r \sin\left(\sigma + \frac\pi2\right), 0\right)$$ and $$\left(r \cos\left(\sigma - \frac\pi2\right), r \sin\left(\sigma - \frac\pi2\right), 0\right),$$ where $$r$$ is the sphere's radius ("ray" in your formulas). Slightly simpler formulas for these points (using the trig identities for angles plus or minus $$\frac\pi2$$ radians) are $$(- r \sin(\sigma), r \cos(\sigma), 0)$$ and $$(r \sin(\sigma), -r \cos(\sigma), 0)$$.

When describing an axis of rotation we typically just take a unit vector along the axis. The factor of $$r$$ makes the vector that much longer or shorter, so let's just use $$(-\sin(\sigma), \cos(\sigma), 0).$$ I chose this vector because it's the one on the "far side" of the sphere in your diagram and a right-hand-rule rotation by $$\theta$$ will give the result you want. If we used the other vector as an axis we'd have to rotate by $$-\theta$$ or us a left-hand rule.

From Wikipedia, the matrix for a rotation by angle $$\theta$$ around axis $$(u_x,u_y,u_z)$$ is $$\begin{bmatrix}\cos \theta +u_{x}^{2}\left(1-\cos \theta \right)&u_{x}u_{y}\left(1-\cos \theta \right)-u_{z}\sin \theta &u_{x}u_{z}\left(1-\cos \theta \right)+u_{y}\sin \theta \\u_{y}u_{x}\left(1-\cos \theta \right)+u_{z}\sin \theta &\cos \theta +u_{y}^{2}\left(1-\cos \theta \right)&u_{y}u_{z}\left(1-\cos \theta \right)-u_{x}\sin \theta \\u_{z}u_{x}\left(1-\cos \theta \right)-u_{y}\sin \theta &u_{z}u_{y}\left(1-\cos \theta \right)+u_{x}\sin \theta &\cos \theta +u_{z}^{2}\left(1-\cos \theta \right)\end{bmatrix}.$$

We want $$u_x = -\sin(\sigma),$$ $$u_y = \cos(\sigma),$$ and $$u_z = 0.$$ Plugging these into the matrix formula, we get $$\begin{bmatrix} \cos\theta + (1-\cos \theta )\sin ^2\sigma & -(1-\cos \theta ) \sin\sigma \cos\sigma & \sin \theta \cos\sigma \\ -\left(1-\cos \theta \right)\sin\sigma \cos\sigma & \cos \theta + (1-\cos \theta)\cos^2\sigma & \sin \theta \sin\sigma \\ -\sin \theta\cos\sigma & -\sin \theta\sin\sigma & \cos \theta \end{bmatrix}.$$

But note that either before or after you apply this rotation, you could rotate the circle in place and it would still be the same circle. When you combine that rotation with the rotation matrix above, you get a rotation about yet another axis which also takes the dotted-line circle to the solid circle.

For example, you could rotate by $$\pi$$ radians ($$180$$ degrees) around the vector $$\left(\sin\left(\frac\theta2\right)\cos(\sigma), \sin\left(\frac\theta2\right)\sin(\sigma), \cos\left(\frac\theta2\right)\right)$$, which is the bisector of the angle between the $$z$$ axis and $$(x_1-x_0,y_1-y_0,z_1-z_0).$$ Since we are rotating by $$\pi$$ radians rather than the angle $$\theta,$$ we have to substitute $$-1$$ where the Wikipedia formula has $$\cos\theta$$ and $$0$$ where Wikipedia has $$\sin\theta.$$ That gives a matrix $$\begin{bmatrix} 2u_{x}^{2} - 1 & 2u_{x}u_{y} & 2u_{x}u_{z} \\ 2u_{y}u_{x} & 2u_{y}^{2} - 1 & 2u_{y}u_{z} \\ 2u_{z}u_{x} & 2u_{z}u_{y} & 2u_{z}^{2} - 1 \end{bmatrix}.$$

Set $$u_x = \sin\left(\frac\theta2\right)\cos(\sigma),$$ $$u_y = \sin\left(\frac\theta2\right)\sin(\sigma),$$ and $$u_z = \cos\left(\frac\theta2\right).$$ Also consider the trig identities $$2\sin^2\left(\frac\theta2\right) = 1 - \cos\theta$$, $$2\cos^2\left(\frac\theta2\right) = 1 + \cos\theta,$$ and $$2\sin\left(\frac\theta2\right) \cos\left(\frac\theta2\right) = \sin\theta,$$ we get $$\begin{bmatrix} (1 - \cos\theta)\cos^2\sigma - 1 & (1 - \cos\theta)\sin\sigma\cos\sigma & \sin\theta\cos\sigma \\ (1 - \cos\theta)\sin\sigma\cos\sigma & (1 - \cos\theta)\sin^2\sigma - 1 & \sin\theta\sin\sigma \\ \sin\theta\cos\sigma & \sin\theta\sin\sigma & \cos\theta \end{bmatrix}.$$

A little more manipulation would show that this is very similar to the rotation matrix derived earlier, except that the signs of all entries in the first two columns have been reversed. This is because each individual point of the circle ends up $$180$$ degrees around the circle from where it does under the other rotation.

• Sorry, but I find your methods rather complicated. Commented Apr 6, 2020 at 7:10
• @JeanMarie I see multiple ways that criticism could apply,. One is that I incorporated a general formula from Wikipedia with all the final entries in the matrix spelled out, unlike your method, which is more of an elegant recipe for constructing the matrix from first principles. (If you plug in the spherical coordinates of $k_x$ and $k_y$, evaluate $K^2,$ and combine the matrices, I expect your result to look like mine.) And then I added a long and possibly confusing appendix to the answer to illustrate that the solution is not unique. Did you have other criticisms in mind? Commented Apr 6, 2020 at 12:16
• @JeanMarie You might say that by simply quoting the spelled-out matrix formula from Wikipedia, I skipped several steps of the explanation, resulting in a ton of notation with no basis for understanding. I plead guilty on that. It's a cookbook answer, not a derivation. Commented Apr 6, 2020 at 12:18
• No, I apologize. I didn't want to offend you. I am almost sure, as you, that our results are the same. I should have turned things differently. Having worked on 3D geometry, in particular rotations, I have progressively found that some very simple recipes almost always work, and my trend (tendancy to be overzealous and "propagator"of the true faith :)) is to say "use these methods instead of others"... Commented Apr 6, 2020 at 12:26
• @JeanMarie No offense taken. Mathematically, I find your answer preferable. And if I could improve mine by some simple changes, I would be happy to do so. Commented Apr 6, 2020 at 12:33

Warning the following may only work when the given vector is not already collinear to the $$z$$-axis. Moreover I restrict myself to the sphere of radius 1 centered in the origin, as it makes my life easier.

You have got a vector $$n$$ of length $$1$$. This vector is the normal of some plane $$P$$, which yields the desired circle $$C’$$ when intersected with the sphere of radius $$1$$. The question is how (and by what amount) to rotate the circle $$C$$ of radius $$1$$ in the $$xy$$ plane such that it agrees with the circle $$C‘$$.

Note that this is equivalent to rotating the normal of the circle $$C$$, say $$z=(0,0,1)$$ to the normal of the circle $$C‘$$, which is $$n$$. We can do this by rotating along the axis $$z\times n$$ (which is perpendicular to both $$z$$ and $$n$$) by $$\angle(z,n)=\cos^{-1}(\langle z,n\rangle)$$ (the angle between $$z$$ and $$n$$).

Warning: this may not be the shortest rotation making the circles line up, as it depends on the orientation of the normal vectors of the circles. We can circumvent this by subtracting $$180^\circ$$ respectively $$\pi$$ when $$\angle(z,n) > 90^\circ$$ respectively $$\pi/2$$.

Edit Upon request I want to add the following method, which involves spherical coordinates.

Note that it suffices to rotate the vector of the $$z$$-axis onto $$n=(x_1,y_1,z_1)$$. According to wikipedia: spherical coordinates the angle between the $$z$$axis and our vector $$n$$ is given by $$\theta$$. Moreover the rotation axis can be seen to be $$(\cos(\sigma+\pi/2), \sin(\sigma+\pi/2),0)$$.

• Is there no way to use our already known angles (theta/sigma) to apply a rotation on the axis? Commented Apr 5, 2020 at 16:24
• Tbh I am way more familiar with vectors than with polar coordinates. But I think there is... $\sigma$ is the angle within the $xy$ plane and $theta$ is the angle of the vector to the $xy$plane, isnt it? Commented Apr 5, 2020 at 16:30
• I think you are right regarding σ but I am not really sure what happens with theta. Commented Apr 5, 2020 at 16:34
• According to Wikipedia I got it wrong and $\theta$ is already the angle between $z$ and $n$. So the only thing to worry about is the rotation axis, which is simply given by $(\cos (\sigma+\pi/2), \sin( \sigma+\pi/2), 0)$. Commented Apr 5, 2020 at 16:41
• I added a method involving spherical coordinates to my answer... Commented Apr 5, 2020 at 16:58

Let us simplify the issue by taking $$(x_0,y_0,z_0)=(0,0,0)$$.

Let $$R$$ be the sphere's radius, $$V_1:=(x_1,y_1,z_1)$$ and $$V_z:=(0,0,R)$$

A rotation is known when you know its axis and its angle :

a) Its axis is defined by the cross product

$$V_1 \times V_z=\begin{pmatrix} \ \ Ry_1\\-Rx_1\\0\end{pmatrix}$$

We will denote the normalized version of this cross product by $$\begin{pmatrix}k_x\\k_y\\k_z\end{pmatrix}$$.

b) Its angle is

$$\theta=\cos^{-1}\left(\dfrac{V_1.V_z}{\|V_1\|\|V_z\|}\right)$$

The rotation matrix is given by the second Rodrigues formula in this reference :

$$\mathbf{R} =\mathbf{I} +(\sin \theta )\mathbf{K} +(1-\cos \theta )\mathbf{K}^{2}\tag{1}$$

where $$\mathbf{K}$$ is the skew symmetric matrix defined by :

$$\mathbf{K} =\left({\begin{array}{rrr}0&-k_{z}&k_{y}\\k_{z}&0&-k_{x}\\-k_{y}&k_{x}&0\end{array}}\right).$$

Remark : The Rodrigues formula under its matrix form (1) is a way to express a rotation as the exponential matrix of a skew symmetric matrix, precisely $$\mathbf{R}=\exp(\theta \mathbf{K})$$

• Isn't θ already know to us? Commented Apr 5, 2020 at 19:12
• Yes, you are right, I was forgetting that you work in spherical coordinates. Say that my solution doesn't assume especially this. Commented Apr 5, 2020 at 19:16
• See the remark I just added. Commented Apr 6, 2020 at 7:07
• @TobyB I assume you are implementing this in software. If you can find where matrices are implemented in the software you are using, this method will be very simple to apply, moreover it will be quite efficient. Remember that you only have to generate the matrix $R$ once for a rotation, before applying it to every point you want to rotate. Commented Apr 6, 2020 at 12:47