← Back

Coordinate Maths

Interactive calculators for the formulas underpinning location-aware applications — Haversine distance, bearing, destination point, tile addressing, and viewport projection.

Haversine distance

Great-circle distance between two lat/lng points on a sphere. Accurate to within 0.5 % globally (Earth is slightly oblate, but the spherical model is more than sufficient for game-scale distances).

Formula. a = sin²(Δlat/2) + cos(lat₁)·cos(lat₂)·sin²(Δlng/2)
d = 2R · atan2(√a, √(1−a)) where R = 6 371 000 m

Bearing & destination point

Forward azimuth (initial bearing) from A to B. Also computes the destination point reached by travelling a given distance and bearing from A — used for spawning world-space objects at known offsets from the player.

Tile coordinate calculator

Given a lat/lng and zoom level, compute the tile address, the pixel offset within that tile, and the number of tiles needed to cover a given radius — for pre-caching.

Viewport projection

Converting a world lat/lng to a canvas pixel, given a centre position, zoom level, and canvas size. This is the core transform needed to render any world object onto a flat 2D map.

Web Mercator. px = (lng + 180) / 360 × tileSize × 2^Z
py = (1 − ln(tan(lat·π/180) + sec(lat·π/180)) / π) / 2 × tileSize × 2^Z
To get screen coords: subtract the centre's world pixel and add half the canvas size.