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

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

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 distance between the two points, in the same unit as the coordinates; zero only when the points coincide.
Midpoint x
2.5
Midpoint y
4
Slope (m)
1.3333
Vertical line? (1 = yes)
0
Angle (degrees)
53.1301

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.

  1. 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.
  2. 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.
  3. Read the distance in the same unit as your coordinates, and the midpoint as the pair of values midpoint x and midpoint y.
  4. 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.
  5. 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.

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

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.

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.

y12
x11
y26
x24

Frequently asked questions.

Why square the differences instead of just adding them?
Adding the absolute differences gives a different measure, the taxicab or Manhattan distance: how far you travel on a street grid that only allows moves along the axes. For (1, 2) and (4, 6) that is 3 + 4 = 7, against a straight-line 5. Chessboard distance, the larger of the two differences, gives 4. Only the squared version matches a ruler.
Does it matter which point I enter first?
Not for the distance, the midpoint or the slope, because reversing the points flips the sign of both differences and those results either square them or divide one by the other. The angle does change: from (1, 2) to (4, 6) it is 53.1301°, while from (4, 6) back to (1, 2) it is −126.8699°, the opposite direction, 180° round.
What does a slope of ∞ mean?
The two points have the same x-coordinate, so the line is vertical and rise over run divides by zero. From (3, 1) to (3, 7) the slope shows ∞ and the angle 90°; from (3, 7) down to (3, 1) it shows −∞ and −90°. In both cases the distance is 6, the flag is 1, and the line's equation is x = 3.
How do I convert the angle to a compass bearing?
The angle is measured anticlockwise from the positive x-axis, as in mathematics; a compass bearing is measured clockwise from north. If x points east and y points north, bearing = 90° − angle, adding 360° to any negative result. For the example, 90° − 53.1301° = 36.8699°, between north and north-east and closer to north-east.
Can I put latitude and longitude straight into the formula?
Not without adjustment. A degree of latitude is roughly 111 km everywhere, but a degree of longitude shrinks with the cosine of the latitude, to about half that at 60°. For short distances you can multiply the longitude difference by the cosine of the latitude before using the formula; over long distances the Earth's curvature matters and a great-circle method is needed instead.
How do I get the equation of the line through the two points?
Use the slope from the calculator and one of the points in y = mx + b, solving for b = y₁ − m × x₁. For (1, 2) and (4, 6), b = 2 − 1.3333 × 1 = 0.6667, so the line is y = (4/3)x + 2/3. If the vertical-line flag is 1, there is no b to find; the equation is x = x₁.
How do I find every point that is equally far from both points?
Those points form the perpendicular bisector: the line through the midpoint at right angles to the segment, with slope −1 ÷ m. For (1, 2) and (4, 6) it passes through (2.5, 4) with slope −0.75, so it is y − 4 = −0.75(x − 2.5). Where the bisectors of a triangle's sides meet is the centre of the circle through all three corners.
How do I find a point part of the way along the segment?
Move the same fraction of each difference: the point a fraction t of the way from P₁ to P₂ is (x₁ + t × Δx, y₁ + t × Δy), and it lies t × d from P₁. The midpoint is the case t = 0.5. One third of the way from (1, 2) to (4, 6) is (1 + 1, 2 + 1.3333) = (2, 3.3333), which is 5 ÷ 3 = 1.6667 units from the start.
What changes in three dimensions?
Add a third squared difference: d = √((x₂ − x₁)² + (y₂ − y₁)² + (z₂ − z₁)²). It is the same right-triangle argument used twice, once in the floor plane and once upward. From (1, 2, 0) to (4, 6, 12) the floor distance is 5 and the height is 12, so the distance is √(9 + 16 + 144) = 13.
Do negative coordinates need any special handling?
No. Each difference is squared, so a negative gap counts the same as a positive one. From (−3, 4) to (2, −8) the differences are 5 and −12, the distance is 13, the midpoint is (−0.5, −2) and the slope is −2.4. The angle, −67.3801°, is negative because the second point lies below the first.

How this page was produced

Published by
Quanta Calculator
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
Coming soon