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
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.
- 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).
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
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.
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.
Frequently asked questions.
What is the distance formula in coordinate geometry?
What is the difference between Euclidean distance and Manhattan distance?
Why does the distance formula square the differences instead of using absolute values?
What happens to the slope when the two points have the same x-coordinate?
How do I compute distance between two points in 3D?
When should I use Euclidean distance versus great-circle (spherical) distance?
Why does the calculator sometimes show the angle as negative?
Does the unit of measurement matter for the distance formula?
Who invented the distance formula?
Why does my calculator return 4.999999999999 instead of exactly 5 for the 3-4-5 triangle?
References& sources.
- [1]Euclid, Elements, Book I, Proposition 47 — the foundational proof of the Pythagorean theorem on which the distance formula directly rests (Heath translation, Cambridge University Press, 1908).
- [2]Descartes, René. La Géométrie (1637) — the founding work of analytic geometry, introducing coordinate axes and turning geometric problems into algebraic ones; reprinted with Smith & Latham translation, Dover, 1954.
- [3]NIST Digital Library of Mathematical Functions, §1.7 — analytic geometry definitions including Cartesian coordinates and the Euclidean distance metric.
- [4]Coxeter, H. S. M. Introduction to Geometry, 2nd edition. Wiley, 1969 — classic reference geometry textbook covering Cartesian coordinates, the distance formula, midpoint, slope, and the relationship to vector norms.
- [5]Stewart, James. Calculus: Early Transcendentals, 8th edition. Cengage, 2015 — undergraduate calculus reference that derives the distance formula in §12.1 and uses it as the foundation of vector geometry and the Euclidean L² norm.
- [6]Wolfram MathWorld — "Distance" entry covering Euclidean distance in n dimensions, alternative metrics (Manhattan, Chebyshev, Minkowski), and the relationship to vector norms.
In this category
Embed
Quanta Pro
Paid features are coming later.
- All 313 calculators remain free
- No billing is enabled