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 v→1=⟨r1;θ1;ϕ1⟩ and v→2=⟨r2;θ2;ϕ2⟩, the angle ψ between them is:

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

Where Δθ=|θ2−θ1|.

Proof. Note that ‖v→1‖=r1 and ‖v→2‖=r2. The dot product is given by:

v→1⋅v→2=r1r2cos⁡ψ=x1x2+y1y2+z1z2

Where ⟨x1,y1,z1⟩ and ⟨x2,y2,z2⟩ are the Cartesian coordinates of v→1 and v→2, 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⁡ϕ2−sin⁡ϕ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
    )