MapMath

CHAPTER 14

Advanced topics — Vincenty, ECEF, and affine transforms

Sub-meter ellipsoidal accuracy with Vincenty, 3D Earth-Centered coordinates, pan/zoom transforms, and rhumb lines.

3 min read

Haversine vs Vincenty — accuracy comparison

Point A

Point B

Haversine (sphere)

10840.289 km

fast, ~0.5% error

Vincenty (ellipsoid)

10826.694 km

accurate to mm

Difference

13594.8 m

0.126%

Vincenty's formulae (ellipsoidal accuracy)

Haversine assumes a sphere; Vincenty works on the WGS84 ellipsoid and is accurate to ~0.5 mm. It's iterative and slow, but used for surveying, aviation, and any time you need sub-meter accuracy over long distances.

The Earth is not a perfect sphere — it's an oblate spheroid, with a radius at the equator (~6,378 km) that is 21 km larger than at the poles (~6,357 km). This difference is small (~0.3%) but accumulates: over a 10,000 km flight, Haversine and Vincenty can disagree by up to ~70 m. For most web apps this is irrelevant. For geodetic survey or precision GPS work, it's not.

The forward and inverse Vincenty formulas are too long to reproduce here, but every major language has a library. Use it when accuracy matters more than speed.

ECEF coordinates

Earth-Centered, Earth-Fixed is a 3D Cartesian system with origin at Earth's center, X through (0, 0), Y through (0, 90° E), Z through the North Pole.

X=(N+h)cosφcosλX = (N + h) \cdot \cos\varphi \cdot \cos\lambda Y=(N+h)cosφsinλY = (N + h) \cdot \cos\varphi \cdot \sin\lambda Z=(N(1e2)+h)sinφZ = (N \cdot (1 - e^2) + h) \cdot \sin\varphi

Where:

  • hh is the height above the ellipsoid
  • N=a/1e2sin2φN = a / \sqrt{1 - e^2 \sin^2\varphi} is the radius of curvature in the prime vertical
  • e2e^2 is the squared eccentricity (≈ 0.00669437999 for WGS84)
  • aa is the equatorial radius

ECEF is essential for GPS, sensor fusion, and 3D map engines (Cesium, Google Earth).

The radius of curvature NN is latitude-dependent — it varies from aa at the equator (6,378,137 m) to a/(1e2)a/(1-e^2) at the poles (~6,399,594 m). This is what distinguishes the ellipsoidal formulas from the spherical ones.

Affine transforms (2D map view)

When you pan and zoom a map, you're applying a 2D affine transform to world pixel coordinates:

[xy1]=[abtxcdty001][xy1]\begin{bmatrix} x' \\ y' \\ 1 \end{bmatrix} = \begin{bmatrix} a & b & t_x \\ c & d & t_y \\ 0 & 0 & 1 \end{bmatrix} \begin{bmatrix} x \\ y \\ 1 \end{bmatrix}

For pure pan (no rotation, no scale change): a=d=1a = d = 1, b=c=0b = c = 0, tx/tyt_x/t_y = offset. For zoom: a=d=scalea = d = \text{scale}, plus translation to keep the zoom anchor stationary.

If your map rotates by angle α: a=cosαa = \cos\alpha, b=sinαb = -\sin\alpha, c=sinαc = \sin\alpha, d=cosαd = \cos\alpha.

Chapter 14 · Paid content

Continue reading "Advanced topics — Vincenty, ECEF, and affine transforms"

You've reached the end of the free preview. Unlock all 22 paid chapters, including distance math, bearings, polygons, spatial indexing, and 3D map rendering — plus a downloadable PDF and the companion code repo.

  • All 22 paid chapters with worked examples
  • Downloadable PDF for offline reading
  • Companion GitHub repo (JavaScript + Python)
  • Free updates for life

Multiple payment options including Wise, PayPal, and bank transfer.