2D Distance Calculator
Distance between two points (x₁, y₁) and (x₂, y₂) on a flat plane, plus the midpoint, the slope of the line through them and its direction angle.
2D Distance Calculator
Background.
Two points on a grid, (1, 2) and (4, 6), are 3 units apart across and 4 units apart up. Those two gaps are the legs of a right triangle, and the straight line joining the points is its hypotenuse, so the distance between them is √(3² + 4²) = 5. Every use of the distance formula, from a homework exercise to a CAD drawing to the nearest-neighbour searches inside recommendation software, is that triangle with different numbers in it.
The geometric fact is Euclid's. Proposition 47 of Book I of the Elements proves that the squares on the two legs of a right triangle add up to the square on the hypotenuse. Turning that into a formula you can apply to any pair of points needed coordinates, which arrived with René Descartes's La Géométrie in 1637; Pierre de Fermat developed the same idea independently at about the same time. Once axes are drawn, the legs are just the difference in x and the difference in y, and their signs stop mattering because squaring removes them.
The same two differences give three more results, all returned here: the midpoint of the segment, the slope of the line through the points, and the direction from the first point to the second as an angle. One case needs a word of warning. When both points share an x-coordinate the line is vertical, the run is zero and the slope has no finite value. It is shown as ∞ (or −∞ when the second point is the lower one), with a vertical-line flag of 1, while the distance, midpoint and angle still come out normally.
The formula assumes a flat plane with the same unit on both axes: graph paper, screen pixels, a floor plan, a projected map grid in metres. Raw latitude and longitude do not qualify. A degree of longitude shrinks toward the poles, to roughly half the length of a degree of latitude at 60° north or south, so the two coordinates are not in the same unit.
What is 2d distance calculator?
The 2D distance, or Euclidean distance, between P₁ = (x₁, y₁) and P₂ = (x₂, y₂) is the length of the straight segment joining them, d = √((x₂ − x₁)² + (y₂ − y₁)²). It behaves the way length should. It is never negative, it is zero only when the two points coincide, it does not depend on which point you name first, and it obeys the triangle inequality: going from A straight to C is never longer than going by way of B. It is also the length of the vector from P₁ to P₂, which is why linear algebra calls it the Euclidean or L² norm, and it extends to three or more dimensions by adding one squared difference for each extra coordinate. Other measures of separation exist, such as taxicab and chessboard distance, but this is the one a ruler on flat paper agrees with.
How to use this calculator.
- Enter the first point as x₁ and y₁. Negative numbers, zero and decimals are all fine; what matters is that both axes use the same unit, whether metres, pixels or grid squares.
- Enter the second point as x₂ and y₂. Swapping the two points leaves the distance, midpoint and slope unchanged but turns the angle through 180°, so put the starting point first if you care about direction.
- Read the distance in the same unit as your coordinates, and the midpoint as the pair of values midpoint x and midpoint y.
- Read the slope as rise over run. If the vertical-line flag shows 1, the slope appears as ∞ or −∞ and the line's equation is simply x = x₁; if the two points are identical, the distance is 0 and the flag is also 1.
- Read the angle in degrees, measured anticlockwise from the positive x-axis and running from just above −180° to 180°. If x points east and y points north, the compass bearing is 90° minus that angle, plus 360° when the result is negative.
The formula.
Draw a horizontal line through P₁ and a vertical line through P₂. They meet at the corner Q = (x₂, y₁), and P₁, Q and P₂ form a right triangle with the right angle at Q. Its horizontal leg is |x₂ − x₁|, its vertical leg is |y₂ − y₁|, and its hypotenuse is the segment you want. By Pythagoras, d² = (x₂ − x₁)² + (y₂ − y₁)², and taking the positive square root gives d. Squaring makes the absolute-value bars unnecessary, which is also why the order of the points cannot change the answer.
Writing Δx = x₂ − x₁ and Δy = y₂ − y₁, the other outputs follow from the same two numbers. Midpoint: M = ((x₁ + x₂) ÷ 2, (y₁ + y₂) ÷ 2), the average of the endpoints along each axis. Slope: m = Δy ÷ Δx. When Δx = 0 the division has no finite answer; the result is ∞ with the sign of Δy, and the vertical-line flag is set to 1. If the points coincide, the slope is shown as ∞ by convention, the flag is 1 and the angle is 0°. Angle: θ = atan2(Δy, Δx), converted to degrees. The two-argument arctangent looks at the signs of both differences, so it can tell a segment heading up and to the right from one heading down and to the left. Those two have the same slope, since m = tan θ, but angles 180° apart.
The larger difference dominates. With Δx = 100 and Δy = 1 the distance is √10,001 = 100.005, only half a hundredth more than the horizontal gap alone, because a small sideways offset adds roughly its square divided by twice the long side. A point 1 unit off a 100-unit line is therefore barely farther away than one on it.
The square root never changes the order of two distances. When software only needs to know which point is nearest, it can compare d² values and skip the root entirely.
A worked example.
Take P₁ = (1, 2) and P₂ = (4, 6). The run is Δx = 4 − 1 = 3 and the rise is Δy = 6 − 2 = 4. Squaring and adding gives 3² + 4² = 9 + 16 = 25, and √25 = 5, so the points are exactly 5 units apart: the 3-4-5 right triangle. The midpoint averages each coordinate: (1 + 4) ÷ 2 = 2.5 and (2 + 6) ÷ 2 = 4, giving (2.5, 4). As a check, the midpoint is 2.5 units from each end, since √(1.5² + 2²) = √6.25 = 2.5. The slope is 4 ÷ 3 = 1.3333, so the line climbs 4 units for every 3 it moves right. The direction is atan2(4, 3) = 0.9273 radians, which is 53.1301°, the larger acute angle of a 3-4-5 triangle; the other is 90° − 53.1301° = 36.8699°. Those numbers also give the line's equation. With y = mx + b and the point (1, 2), b = 2 − 4/3 × 1 = 0.6667, so the line is y = (4/3)x + 2/3, which passes through (4, 6) as it should, since 16/3 + 2/3 = 6.
Frequently asked questions.
Why square the differences instead of just adding them?
Does it matter which point I enter first?
What does a slope of ∞ mean?
How do I convert the angle to a compass bearing?
Can I put latitude and longitude straight into the formula?
How do I get the equation of the line through the two points?
How do I find every point that is equally far from both points?
How do I find a point part of the way along the segment?
What changes in three dimensions?
Do negative coordinates need any special handling?
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.
How this page was produced
- Published by
- Quanta Calculator
- Overseen by
- Daniel, Quanta Calculator Researcher
- Primary sources
- 6 cited below
- Method
- d = √[ (x₂−x₁)² + (y₂−y₁)² ]
- Published
- Last verified
Built with AI assistance and verified by automated tests against the cited sources — every worked example on this page is computed by the same code that runs the calculator. How we build and check calculators.
In this category
Embed
Quanta Pro
Paid features are coming later.
- All 1560 calculators remain free
- No billing is enabled