Audited ·Last updated 27 Jul 2026·6 citations·Tier 1·0 uses

2D Distance Calculator

Free distance formula calculator: enter two points (x1, y1) and (x2, y2) and get the Euclidean distance, midpoint, slope, and angle with worked steps.

2D Distance Calculator

The horizontal coordinate of the first point. Any real number — positive, negative, or zero — works.
The vertical coordinate of the first point. Make sure x₁ and y₁ use the same unit (both metres, both pixels, etc.).
The horizontal coordinate of the second point. The order of the two points does not change the distance — d(P₁,P₂) = d(P₂,P₁).
The vertical coordinate of the second point. The calculator returns distance, midpoint, slope, and bearing angle in one pass.
Distance (d)
5
The straight-line (Euclidean) distance between the two points, equal to √((x₂−x₁)² + (y₂−y₁)²). Always non-negative; zero only when the two points are identical.
Midpoint x
2.5
Midpoint y
4
Slope (m)
1.3333
Vertical line? (1 = yes)
0
Angle (degrees)
53.1301

Background.

The Quanta distance formula calculator takes any two points on the Cartesian plane — (x₁, y₁) and (x₂, y₂) — and returns the straight-line Euclidean distance between them, together with the midpoint, the slope of the line they define, and the bearing angle of the segment, all in one pass. The underlying equation, d = √((x₂ − x₁)² + (y₂ − y₁)²), is one of the most-used identities in all of applied mathematics: it is the Pythagorean theorem dressed up in coordinate notation, and it is the single building block from which essentially every notion of "how far apart" in modern science is constructed.

Computer graphics pipelines call it every time a ray is tested against a sphere, every time a particle's velocity is integrated, every time a UI hit-test asks whether the cursor is within a button. Geographic information systems (GIS) use it as the planar approximation for short-range distances on the Earth before switching to the more expensive great-circle (haversine) formula for longer hops. Machine-learning libraries use it as the Euclidean L² norm — the default distance metric for k-nearest-neighbours, k-means clustering, t-SNE, UMAP, and almost every embedding-based recommender system in production. Robotics path planners, video-game collision detection, image processing (where it powers Euclidean distance transforms and chamfer matching), digital-signal-processing windowing, GPS multilateration, surveying, and computer-aided design all reduce, at the lowest level, to the same √(Δx² + Δy²).

The formula itself is older than coordinate geometry: it follows in two lines from Euclid's Elements Book I, Proposition 47, written around 300 BCE, where the squared lengths on the legs of a right triangle were shown to sum to the squared length on the hypotenuse. What was missing for nearly two thousand years was the algebraic notation to write "a right triangle" using only numbers, and that is precisely what René Descartes supplied in his 1637 treatise La Géométrie, the founding document of analytic geometry. Once you place coordinate axes on the plane, every pair of points defines a right triangle whose legs are the horizontal and vertical differences Δx = x₂ − x₁ and Δy = y₂ − y₁, and whose hypotenuse is the distance between the points — which is why the distance formula is, in the most literal sense, the Pythagorean theorem written in Cartesian clothing.

This page walks through the derivation from a right triangle on the Cartesian plane, the midpoint and slope shortcuts that fall out of the same setup, the standard worked example using the 3-4-5 triangle (the points (1,2) and (4,6) sit exactly four units apart horizontally and three vertically, giving a distance of 5), the edge cases the calculator handles for you — vertical lines where the slope is undefined, zero-length segments where both points coincide, very large coordinates where naive squaring can overflow — and the relationships between the planar Euclidean distance, the Manhattan (taxicab) distance metric, and the spherical great-circle distance you need when the two points are on opposite sides of a planet rather than next to each other on a flat plane.

Whether you arrived for a coordinate-geometry homework problem, a GIS calculation, a graphics or game-dev question, or a machine-learning sanity check, the calculator and explainer below cover every variant of the two-point distance problem in the plane.

What is 2d distance calculator?

The 2D distance between two points P₁ = (x₁, y₁) and P₂ = (x₂, y₂) in the Cartesian plane is the length of the straight line segment joining them. Algebraically: d = √((x₂ − x₁)² + (y₂ − y₁)²). This is called the Euclidean distance because it is the notion of length that holds in Euclidean (flat) geometry — the same geometry Euclid axiomatised in the Elements around 300 BCE. The derivation is short and visual: drop a horizontal line from P₂ and a vertical line from P₁ and they meet at the corner point (x₂, y₁), forming a right triangle whose horizontal leg has length |x₂ − x₁|, whose vertical leg has length |y₂ − y₁|, and whose hypotenuse is the segment from P₁ to P₂. By the Pythagorean theorem, hypotenuse² = horizontal² + vertical², so d² = (x₂ − x₁)² + (y₂ − y₁)². Taking the positive square root gives the formula. The result is symmetric — d(P₁, P₂) = d(P₂, P₁), because (x₂ − x₁)² = (x₁ − x₂)² — and is always non-negative, with d = 0 if and only if the two points coincide. The same setup yields two more useful quantities: the midpoint of the segment, M = ((x₁ + x₂)/2, (y₁ + y₂)/2), which is simply the coordinate-wise average of the two endpoints, and the slope of the line through them, m = (y₂ − y₁) / (x₂ − x₁), which is the rise divided by the run. The slope is undefined when x₁ = x₂ — a vertical line, where the run is zero — and the calculator flags this case rather than returning a misleading number. In higher dimensions the same formula generalises directly: in three dimensions d = √((x₂−x₁)² + (y₂−y₁)² + (z₂−z₁)²), and in n dimensions the Euclidean distance is the square root of the sum of squared coordinate differences — also known as the L² norm of the difference vector.

How to use this calculator.

  1. Enter the coordinates of your first point in the x₁ and y₁ fields. Any real numbers work — positive, negative, decimal, or zero. Make sure x and y are in the same unit (both metres, both pixels, both arbitrary grid squares).
  2. Enter the coordinates of your second point in the x₂ and y₂ fields. The order of the two points does not matter for distance — swapping P₁ and P₂ returns the same number — but it does flip the sign of the angle output, so use consistent ordering if you care about direction.
  3. Read the primary output, distance (d). This is the straight-line Euclidean distance between the two points, in the same unit as your input coordinates.
  4. Use the midpoint outputs (midpointX, midpointY) when you need the centre of the segment — for example, to place a label halfway between two map markers, or to find a balance point in computational geometry.
  5. Use the slope output when you need the gradient of the line for a line-equation problem (y = mx + b form). If the vertical-line flag returns 1, the slope is undefined — the line is vertical and you should write its equation as x = x₁ instead.
  6. Use the angle output when you need the bearing of the segment relative to the positive x-axis. The angle is measured counter-clockwise in the range (−180°, 180°]; convert to a compass bearing (clockwise from north) by computing 90° − angleDegrees if needed.
  7. For three-dimensional distance, use the Quanta 3D distance calculator instead — this calculator works only in the plane. For great-circle distance between latitude/longitude pairs on the Earth, use the haversine calculator.

The formula.

d = √[ (x₂−x₁)² + (y₂−y₁)² ]

The distance formula is the Pythagorean theorem rewritten with coordinates. Start with two points P₁ = (x₁, y₁) and P₂ = (x₂, y₂) and drop perpendiculars: a horizontal line from P₁ and a vertical line from P₂. The two perpendiculars meet at the corner point Q = (x₂, y₁), and the three points P₁, Q, P₂ form a right triangle with the right angle at Q. The horizontal leg has length |x₂ − x₁|, the vertical leg has length |y₂ − y₁|, and the hypotenuse is the segment P₁P₂ — which has length d, the distance we want. By the Pythagorean theorem (Euclid's Elements, Book I, Prop. 47): d² = (x₂ − x₁)² + (y₂ − y₁)². Squaring removes the need for absolute values because the square of any real number is non-negative. Taking the positive square root gives the distance formula: d = √((x₂ − x₁)² + (y₂ − y₁)²). The two auxiliary outputs share the same right-triangle setup. The midpoint M of the segment P₁P₂ is the point exactly halfway between the endpoints along both axes; its coordinates are the arithmetic means of the endpoint coordinates: M = ((x₁ + x₂)/2, (y₁ + y₂)/2). This is the unique point equidistant from P₁ and P₂ that lies on the segment, and it follows directly from the fact that a midpoint divides each coordinate difference in half. The slope m of the line through P₁ and P₂ is the ratio of vertical change to horizontal change — "rise over run": m = (y₂ − y₁) / (x₂ − x₁) = Δy / Δx. When Δx = 0 the line is vertical and the slope is undefined (algebraically it would require division by zero), so the calculator returns a vertical-line flag of 1 and signals ±Infinity rather than a numeric slope. When Δy = 0 the line is horizontal and the slope is 0. The bearing angle is computed with atan2(Δy, Δx), the two-argument arctangent that respects the signs of both deltas and so returns the correct quadrant in the range (−180°, 180°]. Internally the calculator computes Δx and Δy in standard IEEE-754 double-precision arithmetic, then squares, sums, and takes the square root using Decimal.js for the final rounding step — this avoids the rounding drift that occasionally surfaces when (Δx² + Δy²) is a perfect square and the square root "should" be exact (the 3-4-5 case is the canonical example, where any sloppy floating-point pipeline can return 4.999999999999 instead of 5).

A worked example.

Example

Take the two points P₁ = (1, 2) and P₂ = (4, 6). The horizontal change is Δx = 4 − 1 = 3, and the vertical change is Δy = 6 − 2 = 4. Plug into the distance formula: d = √(3² + 4²) = √(9 + 16) = √25 = 5. This is the famous 3-4-5 right triangle in disguise — the segment from (1, 2) to (4, 6) is the hypotenuse of a right triangle whose legs are three units long and four units tall, so the distance between the points is exactly 5 units. The midpoint is M = ((1 + 4)/2, (2 + 6)/2) = (2.5, 4) — the unique point lying halfway along the segment, equidistant from both endpoints. The slope is m = (6 − 2) / (4 − 1) = 4/3 ≈ 1.333, meaning the line rises four units for every three units it moves to the right. The bearing angle is atan2(4, 3) × (180/π) ≈ 53.13° — the segment points slightly above the positive x-axis, consistent with the 3-4-5 triangle's well-known interior angles of 90°, 53.13°, and 36.87°. Every output here is exact in rational arithmetic, which is why the 3-4-5 worked example is the standard sanity check for any distance-formula implementation: if your code returns d = 5.0, midpoint (2.5, 4), and slope 1.333…, the formula and your floating-point rounding are both behaving correctly.

y12
x11
y26
x24

Frequently asked questions.

What is the distance formula in coordinate geometry?
The distance between two points P₁ = (x₁, y₁) and P₂ = (x₂, y₂) in the Cartesian plane is d = √((x₂ − x₁)² + (y₂ − y₁)²). It is the Pythagorean theorem applied to the right triangle whose horizontal leg is the difference in x-coordinates and whose vertical leg is the difference in y-coordinates — the hypotenuse of that triangle is the segment joining the two points, and its length is the distance you want.
What is the difference between Euclidean distance and Manhattan distance?
Euclidean distance — the formula this calculator uses — is the straight-line "as the crow flies" distance, d = √(Δx² + Δy²). Manhattan distance (also called taxicab or L¹ distance) is the sum of the absolute coordinate differences, d = |Δx| + |Δy|, which is the distance a taxi would actually drive on a city grid where it can only move along streets, never diagonally through buildings. For the same two points, Manhattan distance is always greater than or equal to Euclidean distance, with equality only when one of the deltas is zero. Machine-learning models often pick between them based on which assumption better fits the underlying data: Euclidean for continuous geometric quantities, Manhattan for grid-like or high-dimensional sparse data.
Why does the distance formula square the differences instead of using absolute values?
Both work for ensuring the deltas are non-negative, but squaring is what makes the formula land on the right answer — the hypotenuse of a right triangle. The Pythagorean theorem says hypotenuse² = leg₁² + leg₂², so to recover the hypotenuse you square the legs, add them, then square-root the total. If you replaced the squares with absolute values you would just compute |Δx| + |Δy|, which is the Manhattan distance, not the straight-line distance. The squaring also makes the formula naturally differentiable, which is why gradient-based optimisers in machine learning prefer squared-distance loss functions (mean squared error) over absolute-distance loss functions.
What happens to the slope when the two points have the same x-coordinate?
The slope formula m = (y₂ − y₁) / (x₂ − x₁) divides by zero when x₁ = x₂, so the slope is mathematically undefined for vertical lines. The calculator catches this case and sets the vertical-line flag (slopeIsVertical) to 1, while emitting +Infinity or −Infinity for the slope output depending on the sign of Δy (and +Infinity by convention when the two points are identical, a degenerate zero-length segment). To describe the line algebraically you cannot use the slope-intercept form y = mx + b; instead you write x = x₁, which means "all points whose x-coordinate is x₁ regardless of y". The distance, midpoint, and angle outputs are still well-defined and correctly computed even in this case.
How do I compute distance between two points in 3D?
Extend the formula by adding a z-term: d = √((x₂ − x₁)² + (y₂ − y₁)² + (z₂ − z₁)²). The derivation is the same right-triangle argument applied twice — first inside the xy-plane to find the planar component, then again with that planar distance and the z-difference to find the 3D hypotenuse. The Quanta 3D distance calculator handles this directly. The same pattern generalises to any number of dimensions n: the Euclidean distance in n-space is the square root of the sum of squared coordinate differences, also called the L² norm of the difference vector — the standard distance metric used throughout physics, statistics, and machine learning.
When should I use Euclidean distance versus great-circle (spherical) distance?
Use Euclidean distance when the two points lie on a flat plane — a printed map, a computer screen, a small-area GIS layer, a local game grid. Use great-circle (also called orthodromic, haversine, or spherical) distance when the points lie on the surface of a sphere and the separation is large enough that the curvature of the sphere matters — typically anything over a few tens of kilometres on Earth. For two cities on opposite sides of a continent, the Euclidean distance computed from their latitude/longitude as if they were planar coordinates underestimates the true ground distance because it cuts a chord through the interior of the planet rather than tracing the curved surface. For short distances — anything under ~10 km on Earth — the Euclidean approximation in a local projected coordinate system is within ~0.1 % of the great-circle distance, which is why GIS systems usually project to a flat coordinate system first and then use this very formula.
Why does the calculator sometimes show the angle as negative?
The angle output uses atan2(Δy, Δx), which returns the bearing of the segment from P₁ to P₂ in the range (−180°, 180°] measured counter-clockwise from the positive x-axis. A negative angle simply means the segment points below the x-axis — for example, going from (0, 0) to (1, −1) returns −45°, while going from (0, 0) to (1, 1) returns +45°. Swap the order of the two points and the sign of the angle flips, because Δy and Δx both flip sign. If you want a non-negative compass-style bearing in [0°, 360°), add 360° to any negative angle output.
Does the unit of measurement matter for the distance formula?
No — the formula is unit-agnostic, as long as both coordinates of both points are in the same unit. If you enter coordinates in metres, the distance comes back in metres; if you enter them in pixels, the distance is in pixels; if you mix units (say x in metres and y in feet) the result is geometric nonsense. The formula does not know or care which unit you used. This is why the distance formula is the same in pure mathematics, computer graphics, GIS, navigation, and physics — it is a statement about the geometry of the plane, not about any particular measurement system.
Who invented the distance formula?
The Pythagorean theorem on which it rests has been known for at least 3,800 years — the Babylonian tablet Plimpton 322 (~1800 BCE) lists fifteen Pythagorean triples — and was rigorously proved by Euclid around 300 BCE (Elements, Book I, Prop. 47). The translation of that theorem into the algebraic distance formula required coordinate geometry, which was introduced by René Descartes in La Géométrie (1637) and independently by Pierre de Fermat in unpublished manuscripts of the same era. Once the (x, y) coordinate system existed, the distance formula followed in two lines from the Pythagorean theorem. The L² generalisation to n dimensions is implicit in 19th-century work on Euclidean space and was formalised in modern notation in the early 20th-century development of metric spaces by Maurice Fréchet (1906) and Felix Hausdorff (1914).
Why does my calculator return 4.999999999999 instead of exactly 5 for the 3-4-5 triangle?
This is a floating-point rounding artefact, not a bug in the formula. IEEE-754 double precision cannot represent every real number exactly, and the intermediate value (3² + 4²) = 25 plus the subsequent square root √25 can pick up a sub-ULP of error depending on the order of operations and the rounding mode of the underlying CPU. The Quanta calculator runs the final square-root step through Decimal.js and rounds to 15 significant figures (the maximum precision of an IEEE double) before emitting the result, which is enough to return exactly 5 for the 3-4-5 case and to avoid spurious trailing nines for every other rationally-rooted triple (5-12-13, 8-15-17, and so on). If you ever see the artefact in your own code, the standard fix is exactly this: do the arithmetic in arbitrary precision (or use a numerically stable hypotenuse routine like the hypot() function in C/Python/JavaScript) for the final rounding step.

In this category

Embed

Quanta Pro

Paid features are coming later.

  • All 313 calculators remain free
  • No billing is enabled
Coming soon