Get xyz coordinate on surface of a sphere

645 Views Asked by At

I'm no mathematician so please take that into account in replies.

Lets say we have a sphere located at 0, 0, 0,

The radius is 0.5m

We want the surface origin to be x=0, y=0.5, z=0

I want to be able to add an angle in degrees to the x/y plane and add an angle in degrees to the z-plane and get back the xyz coordinate.

For example starting at the origin x=0, y=0.5, z=0 + 90 degrees(xy) = x=0.5, y=0, z=0 adding 90 degree to the z gives the coordinate x=0, y=0, z=0.5.

What is the equation for translating this?

In Python I've tried the following but it is not delivering the desired result:

def location_on_sphere(radius, rotation_horizontal, rotation_vertical):

    elevation = radians(rotation_vertical)
    alpha = radians(rotation_horizontal)
    r = radius

    x = r * cos(elevation) * sin(alpha)
    y = r * cos(elevation) * cos(alpha)
    z = r * sin(elevation)
    print(str(x) + ' ' + str(y) + ' ' + str(z))

location_on_sphere(radius=0.5, rotation_horizontal=0, rotation_vertical=0)

Edit: I've updated the above code relative to comments

Results:

location_on_sphere(radius=0.5, rotation_horizontal=0, rotation_vertical=0) : x=0.0 y=0.5 z=0.0 Correct

location_on_sphere(radius=0.5, rotation_horizontal=90, rotation_vertical=0) : x=0.5 y=3.061616997868383e-17 z=0.0 Nearly correct, correct would be x=0.5, y=0, z=0, give the power to -17 then the 3.06... is just a residue, so I think this now works? I'll run some tests.

2

There are 2 best solutions below

5
On

The first operation is a rotation about the $z$ axis, the second about the $y$ axis. Set the angles as $α=toradians(alpha)$ and $beta=toradians(polar)$. In total the chain of operations assembles as $$ \pmatrix{x\\y\\z} = R_y(alpha)R_z(polar)\pmatrix{0\\r\\0}\\ =\pmatrix{\cosα&0&\sinα\\0&1&0\\\sinα&0&\cosα} \pmatrix{\cosβ&-\sinβ&0\\\sinβ&\cosβ&0\\0&0&1} \pmatrix{0\\r\\0}\\ =\pmatrix{\cosα&0&\sinα\\0&1&0\\\sinα&0&\cosα} \pmatrix{-r\sinβ\\r\cosβ\\0}\\ =\pmatrix{-r\cosα\sinβ\\r\cosβ\\-r\sinα\sinβ}\\ $$

3
On

After reading your question and comments, the choices of directions and variable names are still ambiguous to me. In particular, the variables alpha and polar only appear in code but not defined elsewhere, and alpha is also not defined in your Math Insight "solution" (mentioned in your comment).

The following is my interpretation, and is different from the existing answer.

Consider the first rotation in your question: to rotate point $(x=0, y=r, z=0)$ on the $xy$-plane by an azimuthal angle $\alpha$. Keeping the $z$-coordinate fixed, this is a rotation on a 2D polar coordinate system. But less conventionally, here a positive $\alpha$ is when rotating from the positive $y$-direction towards the positive $x$-direction, according to your example. The intermediate result is

$$\begin{align*} \pmatrix {x'\\y'\\z'} &= \pmatrix{\cos \alpha &\sin \alpha & 0\\-\sin \alpha & \cos \alpha & 0\\0&0&1}\pmatrix{0\\r\\0} = \pmatrix{r\sin\alpha\\r\cos\alpha\\0} \end{align*}$$

(Check by putting $r=0.5$ and $\alpha = +90^\circ$, as in your comment.)

Then the second rotation in your question: to rotate the intermediate point "to the $z$-plane", which is not well-defined to me. I interpret this as to "add" an elevation angle (or latitude angle) to the intermediate point on the $xy$-plane, towards the positive $z$-direction. But note that the elevation angle is $90^\circ$ minus the polar angle, and I will not call the elevation angle polar below.

Considering the new $x$- and $y$-coordinates only, radially the final point is scaled by $\cos (\text{elevation})$. The new $z$-coordinate becomes $r\sin(\text{elevation})$ after rotation.

$$\begin{align*} \pmatrix {x\\y\\z} &= \pmatrix{r\cos(\text{elevation})\sin\alpha\\r\cos(\text{elevation})\cos\alpha\\r\sin(\text{elevation})} \end{align*}$$

(Check by putting $r=0.5$, $\alpha = +90^\circ$, $\text{elevation}=+90^\circ$, as in your question.)