CALCULATORCASTLE

Distance Calculator

Calculate the distance between two points using the distance formula.

About

Distance Calculator

Four calculators sit above this. Two of them work in a coordinate system, on a plane or in space, and return an exact answer. The other two work on the Earth's surface, one from latitude and longitude you type in and one from pins you drop on a map. The maths behind each pair is different, and the difference matters once the distance gets long enough for the curve of the planet to count.

Distance in a 2D coordinate plane

The distance between two points on a plane comes from the distance formula, which is the Pythagorean theorem with the two sides written as coordinate differences:

d =(x₂ − x₁)² + (y₂ − y₁)²

Draw the horizontal and vertical gaps between the two points and you have a right triangle whose hypotenuse is the distance you want. That is the whole derivation.

The order of the points does not matter, as long as you stay consistent. Take (1, 5) and (3, 2). Treating (1, 5) as the first point gives √(2² + (−3)²) = √13. Treating (3, 2) as the first point gives √((−2)² + 3²) = √13 as well. Both differences change sign, and squaring removes the sign, so the answer is identical either way. That also means the formula can never return a negative distance, and if yours does, something has been squared incorrectly.

The calculator also reports the slope and angle of the line joining the points, plus its equation. Slope is ΔY divided by ΔX, and the angle is the arctangent of that ratio measured from the positive x-axis. Where ΔX is zero the line is vertical, the slope is undefined rather than infinite, and the equation collapses to x equals a constant. The Slope Calculator covers that side in more depth.

Distance in a 3D coordinate space

Adding a third axis adds a third squared term and changes nothing else:

d =(x₂ − x₁)² + (y₂ − y₁)² + (z₂ − z₁)²

Order is again irrelevant provided the coordinates are paired correctly. From (1, 3, 7) to (2, 4, 8) each difference is 1, so the distance is √(1 + 1 + 1) = √3, about 1.7320508075689. The pattern extends to any number of dimensions: square every coordinate difference, add them, take the root. That generalisation is the Euclidean distance, and it is the standard measure of similarity in machine learning, where the coordinates are features rather than positions in space.

Both coordinate calculators show the exact surd where one exists. A distance of √18 is displayed as 3√2 alongside the decimal, because 3√2 is exact and 4.2426406871193 is rounded. In geometry work the surd is usually the form to carry forward, since rounding early lets error accumulate through later steps.

Distance between two points on the Earth

Coordinate formulas assume flat space, so they cannot be used on latitude and longitude. A degree of latitude is roughly 111 km everywhere, but a degree of longitude shrinks from about 111 km at the equator to nothing at the poles, because the meridians converge. Treating those as flat coordinates gives badly wrong answers away from the equator.

What you want instead is the great-circle distance, the shortest path across the surface of a sphere. A great circle is the largest circle that can be drawn on a sphere, formed where a plane through the centre cuts the surface. The equator is one, and so is every line of longitude. Lines of latitude other than the equator are not, which is why long-haul flights between two cities at the same latitude curve toward the pole rather than running straight along the parallel. That curve is the shorter route.

The haversine formula

The haversine formula gives the great-circle distance between two points on a sphere from their latitudes and longitudes:

d = 2r · arcsin(√sin²((φ₂−φ₁)/2) + cos φ₁ · cos φ₂ · sin²((λ₂−λ₁)/2))

Here r is the radius of the sphere, φ₁ and φ₂ are the two latitudes, and λ₁ and λ₂ the two longitudes, all in radians. It is popular because it is short, numerically stable over small distances, and easy to implement.

Its limitation is the assumption underneath it. The Earth is not a sphere. It is an oblate spheroid, flattened at the poles by its own rotation: about 6,378 km in radius at the equator against about 6,357 km at the poles, a difference of some 21 km. Treating it as a perfect sphere introduces an error of up to roughly 0.5%, which is around 5 km on a 1,000 km trip.

Lambert's formula, which this calculator uses

Lambert's formula corrects for that flattening by working on an ellipsoid rather than a sphere, and it is accurate to the order of 10 metres over thousands of kilometres. Both the coordinate calculator and the map above use it.

d = a(σ −f2(X + Y))

In it, a is the equatorial radius, f is the flattening, and σ is the central angle between the two points, found with something like the haversine relation. X and Y are correction terms built from the reduced latitudes β, which are the ordinary latitudes adjusted for flattening through tan β = (1 − f) · tan φ. Writing P = (β₁ + β₂)/2 and Q = (β₂ − β₁)/2, the corrections are:

X = (σ − sin σ) · sin²P · cos²Q / cos²(σ/2)
Y = (σ + sin σ) · cos²P · sin²Q / sin²(σ/2)

The practical size of the difference: from Washington DC to Philadelphia, Lambert gives 200.0 km where the haversine gives 199.8 km. Over a 6,500 km intercontinental route the gap grows to several kilometres. For a rough answer the haversine is fine; for anything that has to be right, the ellipsoidal formula earns its extra complexity.

Neither formula is exact, and neither can be. Both model the Earth as a smooth mathematical figure, and the real surface has mountains, valleys, and local variation in shape that no closed formula accounts for. Vincenty's formulae are more precise again, converging iteratively to within a fraction of a millimetre on the ellipsoid, at the cost of failing to converge for nearly antipodal points.

Great-circle distance is not travel distance

This is the distinction most likely to matter in practice. Every formula on this page returns the straight-line distance across the surface, which is what an aircraft approximates and what "as the crow flies" means. It is not how far you would drive.

Road distance is typically 20% to 40% longer than the great-circle distance, depending on terrain and how the road network runs, and mountains or water can push it well beyond that. Flight distance sits closer to the great-circle figure but still exceeds it, because aircraft follow airways, avoid restricted airspace, and route around weather. If you need driving distance or travel time, a routing service that knows the actual roads is the right tool and this calculator is not.

Coordinates, and the signs people get wrong

Latitude runs from −90° at the South Pole to +90° at the North Pole. Longitude runs from −180° to +180°, measured from the prime meridian at Greenwich. Southern latitudes and western longitudes are negative, and forgetting that minus sign is the single most common input error. Washington DC is 38.8976, −77.0366; entering it as 77.0366 puts it in western China.

Coordinates come in two notations. Decimal degrees give a single number such as 38.8976. Degrees-minutes-seconds splits the same value into 38° 53′ 51.36″, where a minute is a sixtieth of a degree and a second a sixtieth of a minute. The lat/long calculator accepts both, with a tab for each. To convert by hand, divide the minutes by 60 and the seconds by 3600, then add them to the whole degrees.

Precision decides how many decimals you need. At the equator, one decimal place of latitude is about 11 km, three places about 110 m, and five places about 1.1 m. Four decimal places, roughly 11 m, is plenty for locating a building, and beyond six you are below the accuracy of most consumer GPS receivers.

Using the map

The map takes either approach. Search for a city, address, or postcode and pick from the results, or tap the map to drop a pin. Two pins give the distance, and either can be dragged to adjust it. Place names are resolved through OpenStreetMap's geocoding service, and the map tiles come from OpenStreetMap as well.

Distances on the map use the same Lambert formula as the coordinate calculator, so a place found by name and the same place typed in as numbers return the same answer. For the related geometry, the Pythagorean Theorem Calculator covers the right-triangle relation the 2D formula rests on, and the Area Calculator handles flat figures once you have their dimensions.

Common questions

Frequently asked questions

For two points on a plane, d = √((x₂ − x₁)² + (y₂ − y₁)²). It is the Pythagorean theorem with the triangle sides written as coordinate differences. In 3D a third squared term is added for the z coordinates. The order of the points makes no difference, because squaring removes the sign.

No. Both coordinate differences change sign if you swap the points, and squaring cancels that out. Using (1, 5) then (3, 2) gives √13, and using (3, 2) then (1, 5) gives √13 as well. What does matter is pairing each x with its own y.

Because it assumes flat space. A degree of latitude is about 111 km everywhere, but a degree of longitude shrinks from 111 km at the equator to zero at the poles as the meridians converge. Treating them as flat coordinates gives badly wrong answers away from the equator, so a great-circle formula is needed instead.

The haversine treats the Earth as a perfect sphere, which is simple but can be off by up to about 0.5%. Lambert treats it as the flattened ellipsoid it actually is and is accurate to around 10 metres over thousands of kilometres. This calculator uses Lambert: Washington to Philadelphia is 200.0 km by Lambert against 199.8 km by haversine.

No. Every formula here gives the straight-line distance across the Earth's surface, the great-circle or "as the crow flies" figure. Road distance is usually 20% to 40% longer and can be far more where mountains or water intervene. For driving distance or travel time you need a routing service that knows the roads.

As negative numbers. Latitude runs from −90° at the South Pole to +90° at the North Pole, and longitude from −180° to +180° either side of Greenwich. In the decimal tab type a minus sign; in the degrees-minutes-seconds tab tick the S/W box. A missing minus sign is the commonest input mistake here.

At the equator one decimal place of latitude is roughly 11 km, three places about 110 m, and five places about 1.1 m. Four decimal places, around 11 m, is enough to locate a building. Beyond six places you are below the accuracy of most consumer GPS receivers.

Because it is exact and the decimal is not. A distance of √18 simplifies to 3√2, which is precise, where 4.2426406871193 is rounded. In geometry work the surd is usually the form to carry forward, since rounding early lets error accumulate through later steps.