Summary
When working on problems in three dimensional space it is often required convert between two or more co-ordinate systems. This article presents the formulae to convert between Cartesian and Spherical co-ordinate systems.
Definitions
| $r$ | : | Radial distance |
| $\varphi$ | : | Zenith angle ( $0 \le \varphi \le \pi$ ) |
| $\theta$ | : | Azimuth angle ( $0 \le \theta \le 2\pi$ ) |
Introduction
A common procedure when operating on 3D objects is the conversion between spherical and Cartesian co-ordinate systems. This is a rather simple operation however it often results in some confusion.
The spherical coordinates system defines a point in 3D space using three parameters, which may be described as follows:
- The radial distance from the origin (O) to the point (P), r.
- The zenith angle, $\varphi$ between the zenith reference direction (z-axis) and the line OP with $0 \leq \varphi \leq \pi$ .
- The azimuth angle, $\theta$ between the azimuth reference direction (x-axis) and the orthogonal projection of the line OP of the reference plane (x-y plane) with $0 \leq \theta \leq 2\pi$ .
Conversion from Cartesian to Spherical
Conversion Formula
We may convert a given a point in Cartesian co-ordinates (x,y,z) to spherical co-ordinates using the following formulas:
$$ \displaystyle \begin{aligned} r &= \sqrt{ (x^2 + y^2 + z^2) } \\ \\ \varphi &= \arccos\left(y/r\right) \\ \\ \theta &= \arctan\left(y/x\right) \end{aligned} $$
Notes on Computational Implementations
One must take care when implementing this conversion as using a standard atan function will only yield the correct spherical coordinates if the point is in the first or fourth quadrant (positive x values).
Instead the function atan2 should be used which takes the coordinates x and y as parameters and returns atan(y/x) taking into account the quadrant x and y lie in by adjusting the values as follows:
$$ \displaystyle atan2(y,x) = \left\{ \begin{array}{l l} arctan(y/x) & \quad x \gt 0\\ \\ \pi + arctan(y/x) & \quad y \leq 0, x \lt 0\\ \\ -\pi +arctan(y/x) & \quad y \lt 0, x \lt 0 \\ \\ \pi/2 & \quad y \gt 0, x = 0 \\ \\ -\pi/2 & \quad y \lt 0, x = 0 \\ \\ \text{undefined} & \quad y = 0, x = 0 \end{array} \right. $$
However take note of the parameter order of this function on your platform as it can differ. For example in C++ the interface is atan2(y,x) while in excel it is atan2(x,y).
Conversion from Spherical to Cartesian
Conversion Formula
Conversely spherical coordinates may be converted to Cartesian coordinates using the following formulas:
$$ \displaystyle \begin{aligned} x &= r\sin\left(\varphi\right) \cos\left(\theta\right) \\ \\ y &= r\sin\left(\varphi\right) \sin\left(\theta\right) \\ \\ z &= r\cos\left(\varphi\right) \end{aligned} $$
Article Tags