As evident by the name, the spherical coordinate systems are 3D in nature and apply to spheres (for example, Earth, Sun, etcetera). Uniquely determining the location of a point in space requires three measurements in spherical coordinate systems as well. However, unlike the Cartesian coordinates, they do not have the same units for each measurement.
The polar coordinates are the 2D case of spherical coordinates and require ((r, phi)) measurements to locate a point on the plane. The spherical coordinates require three measurements ((r, theta, phi)) to determine the unique location of a point.
For spherical coordinates, it is essential to specify that the units of angular measurements be the same (either degrees or radians). One should also consider that the coordinates of spherical coordinates have a range after which they change their direction/sign. Similar to the Cartesian coordinate system, the distance for the spherical coordinate systems can be determined using a distance formula
Table of Contents
Distance formula
Similar to the Cartesian coordinate system, the distance between any two given points in the spherical coordinates can be determined by a standard distance formula.
Derivation
We know that, for Cartesian coordinates,
\begin{equation} d = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2 + (z_2 - z_1)^2} \end{equation}
And the spherical equivalents for the Cartesian coordinates are,
\small \begin{equation} x = r\sin(\phi)\cos(\theta) \end{equation}
\small \begin{equation} y = r\sin(\phi)\sin(\theta) \end{equation}
\small \begin{equation} z = r\cos(\phi) \end{equation}
Solving \((2)\), \((3)\), and \((4)\) for \((1)\) we get,
(x_2 - x_1)^2 = \color{red} r_2^2\sin^2(\phi_2)\cos^2(\theta_2) \color{black} + \color{blue} r_1^2\sin^2(\phi_1)\cos^2(\theta_1) \color{black} - \color{green} 2r_1r_2\sin(\phi_1)\sin(\phi_2)\cos(\theta_1)\cos(\theta_2)
(y_2 - y_1)^2 = \color{red} r_2^2\sin^2(\phi_2)\sin^2(\theta_2) \color{black} + \color{blue} r_1^2\sin^2(\phi_1)\sin^2(\theta_1) \color{black} - \color{green} 2r_1r_2\sin(\phi_1)\sin(\phi_2)\sin(\theta_1)\sin(\theta_2)
(z_2 - z_1)^2 = \color{red} r_2^2\cos^2(\phi_2) \color{black} + \color{blue} r_1^2\cos^2(\phi_1) \color{black} - \color{green} 2r_1r_2\cos(\phi_1)\cos(\phi_2)
Collecting the colored terms together we get,
(x_2 - x_1)^2 + (y_2 - y_1)^2 + (z_2 - z_1)^2 = \newline \color{blue} r_1^2\left[\sin^2(\phi_1)\left\{\cos^2(\theta_1) + \sin^2(\theta_1)\right\} + \cos^2(\phi_1)\right] \color{black} + \color{red} r_2^2\left[\sin^2(\phi_2)\left\{\cos^2(\theta_2) + \sin^2(\theta_2)\right\} + \cos^2(\phi_2)\right] \color{black} - \newline \color{green} 2r_1r_2\left[\sin(\phi_1)\sin(\phi_2)\left\{\cos(\theta_1)\cos(\theta_2) + \sin(\theta_1)\sin(\theta_2)\right\} + \cos(\phi_1)\cos(\phi_2)\right]
We can simplify the above expression,
(x_2 - x_1)^2 + (y_2 - y_1)^2 + (z_2 - z_1)^2 = \newline \color{blue} r_1^2 \color{black} + \color{red} r_2^2 \color{black} - \color{green} 2r_1r_2\left(\sin(\phi_1)\sin(\phi_2)\cos(\theta_2 - \theta_1) + \cos(\phi_1)\cos(\phi_2)\right)
Taking the square root, we get the expression for the distance formula in spherical coordinates.
\begin{equation} d = \sqrt{r_1^2 + r_2^2 - 2r_1r_2\left[\sin(\phi_1)\sin(\phi_2)\cos(\theta_2 - \theta_1) + cos(\phi_1)\cos(\phi_2)\right]} \end{equation}
Python code
For the ease of writing the python code, we can split the long formula into smaller chunks,
d = \sqrt{\color{red} r_1^2 + r_2^2 \color{black} - \color{blue} 2r_1r_2 \color{black}\left[\color{green}\sin(\phi_1)\sin(\phi_2)\cos(\theta_2-\theta_1) \color{black} + \color{maroon} \cos(\phi_1)\cos(\phi_2) \color{black}\right]}
import numpy as np def distance_formula(initial_coordinates, final_coordinates=None, deg_rad: str = 'rad'): """ Calculates the distance between two given points in spherical coordinate system Parameters ---------- initial_coordinates: Reference coordinates of the point final_coordinates: Final coordinates of the point to which the distance is to be calculated deg_rad: Whether the specified theta and phi arguments are in degree or radians Returns ---------- float: Distance between two points in spherical coordinates """ r1, theta1, phi1 = initial_coordinates r2, theta2, phi2 = final_coordinates theta1, phi1 = np.radians([theta1, phi1]) if deg_rad == 'deg' else theta2, phi1 theta2, phi2 = np.radians([theta2, phi2]) if deg_rad == 'deg' else theta2, phi2 p1 = r1**2 + r2**2 _comp1 = np.sin(theta1) * np.sin(theta2) * np.cos(phi1 - phi2) _comp2 = np.cos(theta1) * np.cos(theta2) p2 = 2 * r1 * r2 * (_comp1 + _comp2) return np.sqrt(p1 - p2)
Very well explained. Love the 3D space image. Really appreciable efforts.. God bless you.
Thank you for the feedback.