Spherical Coordinates

From Notes
Jump to navigation Jump to search

Spherical coordinates define a point in 3D space (may be generalized to higher dimensions) by using a radial distance from the origin and angles from reference axes. It is a higher-dimensional equivalent to 2D polar coordinates.

Conventions

There are several conventions for representing spherical coordinates. Each is discussed here.

Latitude

Latitude angles are measured from an equatorial plane passing through the origin (canonically the xy-plane). This type of measurement is used typically in geospatial contexts.

Given spherical coordinates with radius, longitude, and latitude r;θ;ϕ, the corresponding point in Cartesian coordinates is given by:

x=rcosϕcosθy=rcosϕsinθz=rsinϕ

This convention will be used for other areas of this page unless otherwise specified.

Colatitude

Colatitude angles are measured from the z-axis. This convention is seldom used outside the realm of mathematics. Even so, it is used in conjunction with other conventions.

Given spherical coordinates with radius, longitude, and colatitude r;θ;φ, the corresponding point in Cartesian coordinates is given by:

x=rsinφcosθy=rsinφsinθz=rcosφ

Angle Between Vectors

Theorem. Given vectors v1=r1;θ1;ϕ1 and v2=r2;θ2;ϕ2, the angle ψ between them is:

ψ=arccos(cosϕ1cosϕ2cos(Δθ)+sinϕ1sinϕ2)

Where Δθ=|θ2θ1|.

Proof. Note that v1=r1 and v2=r2. The dot product is given by:

v1v2=r1r2cosψ=x1x2+y1y2+z1z2

Where x1,y1,z1 and x2,y2,z2 are the Cartesian coordinates of v1 and v2, respectively. Equating both definitions and solving for ψ gives:

cosψ=x1x2+y1y2+z1z2r1r2ψ=arccos(x1x2+y1y2+z1z2r1r2)

Substituting the above formulae for these coordinates into the alternate dot product definition gives:

ψ=arccos(r1r2cosϕ1cosϕ2cosθ1cosθ2+r1r2cosϕ1cosϕ2sinθ1sinθ2+r1r2sinϕ1sinϕ2r1r2)=arccos(cosϕ1cosϕ2cosθ1cosθ2+cosϕ1cosϕ2sinθ1sinθ2+sinϕ1sinϕ2)=arccos(cosϕ1cosϕ2(cosθ1cosθ2+sinθ1sinθ2)+sinϕ1sinϕ2)=arccos(cosϕ1cosϕ2cos(θ1θ2)+sinϕ1sinϕ2)

Note the use of the subtractive trigonometric identity for cosine. Since cosine is an even function (i.e. cos(θ)=cos(θ), the order of subtraction does not matter; only the difference between θ1 and θ2 matters.

quod erat demonstrandum

Computational Formula

In situations where floating point rounding errors pose a challenge, the following alternate formula may be used (from wikipedia:Great-circle distance):

ψ=arctan(cosϕ2sin(Δθ))2+(cosϕ1sinϕ2sinϕ1cosϕ2cos(Δθ))2sinϕ1sinϕ2+cosϕ1cosϕ2cos(Δθ)

Below is a Python implementation (using the NumPy library) of this formula.

import numpy as np

def angle_between(lng1, lat1, lng2, lat2):
    s1 = np.sin(lat1)
    c1 = np.cos(lat1)
    s2 = np.sin(lat2)
    c2 = np.cos(lat2)
    dlng = lng2 - lng1
    c2cdlng = c2 * np.cos(dlng)

    return np.arctan2(
        np.sqrt((c2*np.sin(dlng))**2 + (c1*s2 - s1*c2cdlng)**2),
        s1*s2 + c1*c2cdlng
    )