Bearings
Great circle on a sphere, FM 3-25.26 and Bowditch · checked against GeographicLib's GeodSolve
What it is
A bearing is only meaningful with its north. A map has three: true north along the meridian, grid north along the map’s grid lines, and magnetic north where the compass points. Converting between them means adding or subtracting the angles between them, and the sign of each angle is where code goes wrong.
- Grid convergence is true to grid. It’s zero on a UTM zone’s centre line and grows toward the zone’s edges and the poles.
- Declination is true to magnetic. It comes from the magnetic model.
- The G-M angle is grid to magnetic: declination minus convergence. Military maps print it in the margin.
Operations
| Operation | Gives |
|---|---|
inverse |
The distance and initial bearing between two pointsIn: fromLatitudeInDegrees, fromLongitudeInDegrees, toLatitudeInDegrees, toLongitudeInDegreesOut: distanceInMeters, bearingInDegrees |
destination |
The point at a distance and bearing from anotherIn: fromLatitudeInDegrees, fromLongitudeInDegrees, distanceInMeters, bearingInDegreesOut: toLatitudeInDegrees, toLongitudeInDegrees |
backAzimuth |
The reverse of a bearingIn: bearingInDegreesOut: backAzimuthInDegrees |
turn |
The shortest turn from one bearing to anotherIn: fromBearingInDegrees, toBearingInDegreesOut: turnInDegrees, direction |
convertNorth |
A bearing converted between true, magnetic and grid northIn: bearingInDegrees, fromNorth, toNorth, magneticDeclinationInDegrees, convergenceInDegreesOut: convertedBearingInDegrees |
formatBearing |
A bearing written in whole degrees or milsIn: bearingInDegrees, angleUnitOut: bearingText |
compassPoint |
The nearest of 4, 8, 16 or 32 compass pointsIn: bearingInDegrees, pointCountOut: compassPointText |
Fields
| Field | Meaning |
|---|---|
fromLatitudeInDegrees | The start, −90 to 90 |
fromLongitudeInDegrees | The start, −180 to 180 |
toLatitudeInDegrees | The end, −90 to 90 |
toLongitudeInDegrees | The end, −180 to 180 |
distanceInMeters | Along the great circle, on the sphere; 0 or more |
bearingInDegrees | 0 to 360, clockwise from north. inverse leaves it out where it's undefined: the same point, antipodes, a pole, or due north, where 0 and 360 are both right. |
backAzimuthInDegrees | The bearing plus 180°, in 0 up to 360, so the back azimuth of 180° is 0° |
fromBearingInDegrees | The heading now, 0 to 360 |
toBearingInDegrees | The heading wanted, 0 to 360 |
turnInDegrees | The smaller turn, 0 to 180 |
direction | right, left, or none for no turn. A turn of exactly 180° is right. |
fromNorth | true, magnetic or grid |
toNorth | true, magnetic or grid |
magneticDeclinationInDegrees | True to magnetic, east positive, from the magnetic model: true = magnetic + declination |
convergenceInDegrees | True to grid, east positive, from UTM: true = grid + convergence. The G-M angle is declination minus convergence. |
convertedBearingInDegrees | 0 up to 360 |
angleUnit | degrees, or mils at 6400 to the circle |
bearingText | Degrees rounded half up to a whole degree, three digits and °, 000° to 359°; or mils rounded half up, four digits, 0000 to 6399. Round first, then wrap: 359.6° is 000°. |
pointCount | 4, 8, 16 or 32 |
compassPointText | Bowditch's abbreviation for the nearest point, like N, NNE or NE by N. Half-way between two points goes clockwise. |
Edge cases
| Case | What it tests |
|---|---|
edge-inverse-across-antimeridianinverse | 179.5° to −179.5° is 110 km east, not most of the way round. |
edge-inverse-due-northinverse | Bearing left out: 0 and 360 are both right. |
edge-inverse-same-pointinverse | Distance 0; the bearing is undefined. |
edge-inverse-antipodesinverse | Half the circumference; every bearing is right, so none is compared. |
edge-inverse-from-north-poleinverse | From a pole the bearing depends on the convention, so only distance is compared. |
edge-inverse-one-metreinverse | Short distances keep their precision. |
edge-destination-over-the-poledestination | Due north past the pole comes down the other side: longitude −170. |
fm-3-25-26-figure-6-10convertNorth | FM 3-25.26: magnetic 210° with a 12° east G-M angle is grid 222°. |
fm-3-25-26-figure-6-12convertNorth | FM 3-25.26: grid 2° with a 16° east G-M angle wraps to magnetic 346°. |
fm-3-25-26-figure-6-15convertNorth | FM 3-25.26: magnetic 5° with a 12° west G-M angle wraps to grid 353°. |
hand-convert-north-1convertNorth | True = magnetic + declination. |
hand-convert-north-15convertNorth | True = grid + convergence. |
hand-convert-north-rounds-to-360convertNorth | A hair under 360° rounds to 360°, which is 0°: answers run 0 up to 360. |
bowditch-points-4-359-9compassPoint | Just west of north is still N. |
bowditch-points-16-11-25compassPoint | Half-way between N and NNE goes clockwise: NNE. |
bowditch-points-32-11-25compassPoint | Bowditch's abbreviation: N by E. |
hand-back-azimuth-180backAzimuth | The back azimuth of 180° is 0°, not 360°. |
hand-turn-350-to-10turn | Across north: 20° right, not 340° left. |
hand-turn-0-to-180turn | Exactly 180° is right, by this file's rule. |
hand-turn-90-to-90turn | No turn: direction none. |
hand-format-degrees-0-5formatBearing | Half up: 0.5° is 001°. |
hand-format-degrees-359-6formatBearing | Round, then wrap: 359.6° is 000°, never 360°. |
hand-format-mils-359-98formatBearing | 6399.64 mils rounds to 6400, which wraps to 0000. |
invalid-format-bearing-nanformatBearing | NaN has no bearing to write: outOfRange, not a crash or NaN°. |
Their inputs and expected values are in vectors.json; search for the case's id.
For agents
Measure on a sphere of radius 6,371,008.8 m, not the ellipsoid, so distances match Turf and the map: use the haversine or spherical law of cosines for distance, and the standard spherical formulas for initial bearing and destination. Normalize longitudes to −180 to 180 and bearings to 0 to 360. In convertNorth, true = magnetic + declination and true = grid + convergence, both east positive. In formatBearing, round half up first and wrap after, so 359.6° writes as 000°. For compassPoint, use Bowditch’s abbreviations, with ties going clockwise. Pass every case in vectors.json; text must match exactly.
Test data
vectors.json holds 824 cases, published 2026-11-04. Earth model: Sphere, radius 6,371,008.8 m, the mean radius Turf uses. How to read it: Test data format.
| Operation | Cases |
|---|---|
inverse |
313311 reference · 11 edge · 2 invalid |
destination |
306304 reference · 4 edge · 2 invalid |
backAzimuth |
87 hand · 3 edge · 1 invalid |
turn |
109 hand · 8 edge · 1 invalid |
convertNorth |
346 published · 26 hand · 2 edge · 2 invalid |
formatBearing |
2320 hand · 6 edge · 3 invalid |
compassPoint |
130128 hand · 68 edge · 2 invalid |
Tags say where each value came from: what they mean.
| Field | Tolerance |
|---|---|
distanceInMeters | ± 0.001 |
bearingInDegrees | ± 0.000001 |
toLatitudeInDegrees | ± 0.0000001 |
toLongitudeInDegrees | ± 0.0000001 |
backAzimuthInDegrees | ± 0.000001 |
turnInDegrees | ± 0.000001 |
convertedBearingInDegrees | ± 0.000001 |
Every other field is compared exactly.
Implementations
| Implementation | Status |
|---|---|
| gshaw/algorithms-swiftSwift | Passes |
| gshaw/algorithms-typescriptTypeScript | Passes |
Each implementation's CI grades it against this page's test data on every push, and again when the data changes. The code isn't public yet. See how implementations work.
Source
| Reference program | GeodSolve, GeographicLib 2.7, on a sphere (-e 6371008.8 0)Charles Karney. Every call and its output is in /bearings/sources/geodsolve.txt · MIT |
| Mils, back azimuths and the G-M angle | FM 3-25.26, Map Reading and Land Navigation (2001)Headquarters, Department of the Army. Approved for public release. Excerpts and figures 6-10 to 6-15 in /bearings/sources/ · Public domain |
| Compass points | The American Practical Navigator (Bowditch), Pub. 9, Volume II, Appendix BNational Geospatial-Intelligence Agency. The table is in /bearings/sources/bowditch-appendix-b.txt · Public domain |