Coordinate Distance Made Simple: Finding the Distance Between Two Points
How to measure distance between two points, from the flat Euclidean formula to the Haversine great-circle method for latitude and longitude on a real, curved Earth.
Coordinate Distance Made Simple: Finding the Distance Between Two Points
"How far apart are these two points?" sounds like one question, but it has two answers depending on what your points are. If they sit on a flat plane, like pixels on a screen or marks on graph paper, you reach for the Pythagorean theorem. If they sit on the surface of the Earth as latitude and longitude, a flat formula will lie to you, and you reach for Haversine instead. This post walks through both, with worked numbers you can check by hand, and points to the coordinate distance calculator when you want the answer without the arithmetic.
The flat case: Euclidean distance and Pythagoras
When two points live on a plane, distance is the straight line between them. Label the first point (x1, y1) and the second (x2, y2). Draw the horizontal gap and the vertical gap, and they form the two legs of a right triangle whose hypotenuse is the distance you want. The Pythagorean theorem gives it directly:
distance = sqrt((x2 - x1)^2 + (y2 - y1)^2)
That square root of summed squares is all there is to Euclidean distance. Take (1, 2) and (4, 6). The horizontal gap is 4 - 1 = 3, the vertical gap is 6 - 2 = 4, and sqrt(3^2 + 4^2) = sqrt(9 + 16) = sqrt(25) = 5. The 3-4-5 triangle every geometry class drills is exactly this formula in disguise.
This works wherever your coordinate grid is genuinely flat: CAD drawings, game maps, image pixels, a chessboard, a scatter plot. The same shape of formula extends to three dimensions by adding a (z2 - z1)^2 term under the root. What it cannot do is measure across the surface of a globe, because longitude lines are not evenly spaced as you move toward the poles. One degree of longitude is about 111 km at the equator and shrinks to nothing at the pole, so treating lat/long like a flat (x, y) grid quietly inflates or deflates every answer.
The round case: Haversine and great-circle distance
For points described by latitude and longitude, the honest distance follows the curve of the planet. The shortest path along a sphere between two points lies on a great circle, the largest circle you can draw on the sphere, like the equator or any line of longitude. The Haversine formula computes that great-circle distance from two lat/long pairs and a sphere radius.
The Earth is treated as a sphere with a mean radius of 6371 km. The formula is:
a = sin^2(Δlat / 2) + cos(lat1) * cos(lat2) * sin^2(Δlong / 2)
c = 2 * atan2(sqrt(a), sqrt(1 - a))
distance = 6371 km * c
where Δlat and Δlong are the differences in latitude and longitude, all angles in radians. The name comes from the haversine of an angle, which is sin^2(angle / 2). The reason this formula is preferred over a naive law-of-cosines version is numerical stability: for the small angles that real, nearby point pairs produce, the cosine version loses precision, while Haversine stays accurate.
Haversine assumes a perfect sphere, and the real Earth is a slightly flattened ellipsoid. That introduces a worst-case error of roughly 0.3 to 0.5 percent compared with a full ellipsoidal model like Vincenty. On a 1000 km leg that is a few kilometres, which is fine for travel planning, geocaching, and general mapping. If you need survey-grade precision, use an ellipsoidal calculator.
A worked example: Beijing to Shanghai
Let me run two real points through it. Beijing sits at latitude 39.9042, longitude 116.4074. Shanghai sits at latitude 31.2304, longitude 121.4737. Plug both pairs into the coordinate distance calculator and the great-circle distance comes back at about 1067 km. That lines up with the published city-to-city figure within a percent, which is a reassuring sign the formula is behaving.
The same calculation hands you two extras for free. The initial bearing, the compass heading you would set off on from Beijing to reach Shanghai along the great circle, is reported in degrees and snapped to a 16-point compass label like SE or SSE. Bearing is measured clockwise from due north: 0 is north, 90 is east, 180 is south, 270 is west, and it changes as you travel, which is why it is called the initial bearing. The third output is the great-circle midpoint, returned as its own lat/long pair, which is the geographic halfway point you would paste into a map to find a place to meet in the middle.
I reach for this most often as a sanity check while coding. When I am building a feature that measures distance between GPS points, I want a trusted reference before I trust my own loop. I drop my fixture coordinates into the tool, copy the metre and kilometre output, and assert my implementation lands within a percent. More than once that has caught a bug where I had latitude and longitude swapped in my own arguments, and the reference number simply refused to match until I fixed the order.
Reading the answer: units and the edge cases
A single metre result feeds four units at once: kilometres, miles, metres, and nautical miles. Pick by context. Kilometres and miles suit road and travel distances, metres suit short hops where decimals of a kilometre read awkwardly, and nautical miles, where 1 nmi equals exactly 1852 m, are the standard for aviation and marine navigation. For example, 1000 m shows as 1 km, 0.621 mi, and 0.540 nmi simultaneously, so you never convert by hand. When you need to move between other unit families, such as feet to metres or pounds to kilograms, the unit converter covers the broader set.
Two edge cases deserve a mention because they are where a flat formula breaks outright. Crossing the equator is ordinary: 1 degree south to 1 degree north spans about 222 km, no surprises. The antimeridian near 180 longitude is the trap. From 179 east to 179 west is only 2 degrees of longitude apart, roughly 222 km at the equator, not most of the way around the planet. Haversine takes the short way automatically, and a well-built midpoint normalises its longitude back into the -180 to 180 range so it does not land on the wrong side of the globe.
Avoiding the classic input mistakes
Most wrong distances trace back to the input, not the math. Three traps catch people repeatedly:
- Swapping latitude and longitude. Latitude runs -90 to 90 and is conventionally written first; longitude runs -180 to 180. A value like 116 in the latitude slot is impossible and gets flagged, but 40 in both slots silently points somewhere wrong. Always confirm latitude before longitude.
- Mixing degrees-minutes-seconds with decimal degrees. Decimal degrees are what these formulas want, so 40 degrees 42 minutes north is 40.7, not 40.42. Typing the minutes straight after the decimal point inflates the position and throws the distance off by hundreds of kilometres.
- Dropping the sign. West longitude and south latitude are negative. Writing a Los Angeles longitude as 118.24 instead of -118.24 puts the point in the wrong hemisphere and balloons the distance.
Whichever distance you are after, the recipe is the same shape: find the gaps between your coordinates and turn them into a length. On a flat plane that is the square root of summed squares; on a sphere that is Haversine over a great circle. Match the formula to the surface your points actually live on, double-check the order and signs of your inputs, and the number you get out is one you can trust.
Made by Toolora · Updated 2026-06-13