Skip to main content
ilovecalcs logoilovecalcs.

Math · Live

Distance Calculator — 2D & 3D distance formula.

Calculate the Euclidean distance between two points in 2D or 3D space. Shows the step-by-step formula derivation, a coordinate plane visualisation with Δx and Δy legs, midpoint, slope, angle, line equation, Manhattan distance, and unit vector.

How it worksReal-time

Inputs

Two points

d = √((x₂−x₁)² + (y₂−y₁)²)
P1
(1, 2)
P2
(4, 6)
Distance
5

Euclidean distance

5

Between (1, 2) and (4, 6)

Distance (d)
5
Manhattan
7
Midpoint x
2.5
Midpoint y
4

Visualisation

Coordinate plane

d = 5Δx=3Δy=4P2(4, 6)P1(1, 2)

Step-by-step

Calculation breakdown

  1. 1d = √((x₂−x₁)² + (y₂−y₁)²)
  2. 2d = √((4−1)² + (6−2)²)
  3. 3d = √((3)² + (4)²)
  4. 4d = √(9 + 16)
  5. 5d = √25
  6. 6d = 5

Properties

Derived values

Δx (x₂ − x₁)
3
Δy (y₂ − y₁)
4
Midpoint
(2.5, 4)
Manhattan distance
7 (|Δx| + |Δy|)
Slope (m)
1.333333
Angle
53.1301°
Line equation
y = 1.333333x + 0.666667
Unit vector
(0.6, 0.8)

Distance guide

The distance formula: Pythagoras in coordinate geometry.

The distance between two points in a coordinate plane is one of the most fundamental results in all of geometry. It appears in algebra, calculus, physics, computer graphics, data science, and machine learning — wherever you need to quantify how far apart two things are in a structured space. Everything starts with the Pythagorean theorem.

Deriving the 2D distance formula

Given two points P₁ = (x₁, y₁) and P₂ = (x₂, y₂), we can construct a right triangle:

  • The horizontal leg has length |Δx| = |x₂ − x₁|
  • The vertical leg has length |Δy| = |y₂ − y₁|
  • The hypotenuse is the distance d we want

Applying the Pythagorean theorem (a² + b² = c²) with a = |Δx|, b = |Δy|, c = d:

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

The squares eliminate the need for absolute values, squaring a negative number gives a positive result.

Worked example (2D)

Find the distance between P₁ = (1, 2) and P₂ = (4, 6):

d = √((4 − 1)² + (6 − 2)²)
d = √(9 + 16) = √25 = 5

This is a 3-4-5 right triangle. The horizontal and vertical legs are 3 and 4 units respectively, and the hypotenuse — the actual distance — is exactly 5.

Extending to 3D

Adding a third dimension is straightforward: the distance in 3D is the hypotenuse of a right triangle whose legs are the 2D distance in the xy-plane and the vertical separation Δz:

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

This extends naturally to any number of dimensions n:

d = √(Σᵢ (b_i − a_i)²)

In machine learning, this formula computes the Euclidean distance between two data points in high-dimensional feature space, a key operation in k-nearest neighbours, k-means clustering, and SVMs.

Midpoint formula

The midpoint M between P₁ and P₂ is the arithmetic mean of the coordinates:

M = ((x₁+x₂)/2 , (y₁+y₂)/2)

Geometrically, M lies on the line segment P₁P₂ exactly halfway between the two points. In 3D, add (z₁+z₂)/2 for the z-coordinate of the midpoint.

Slope and the line equation (2D)

Once you know two points, you can determine the equation of the line passing through them using the slope formula:

m = (y₂ − y₁) / (x₂ − x₁) = Δy / Δx

and then the slope-intercept form with one of the points:

y − y₁ = m(x − x₁) → y = mx + b, where b = y₁ − mx₁

Special cases: when Δx = 0 the line is vertical (x = x₁, slope is undefined) and when Δy = 0 the line is horizontal (y = y₁, slope = 0).

Angle of the line

The angle θ that the line makes with the positive x-axis is:

θ = atan2(Δy, Δx)

This uses the two-argument arctangent function, which correctly handles all four quadrants and returns an angle in (−180°, +180°]. The slope is related to the angle by m = tan(θ).

Manhattan distance

Also called taxicab geometry, L₁ distance, orcity-block distance, the Manhattan distance sums the absolute differences rather than using squares:

d₁ = |x₂−x₁| + |y₂−y₁|

This is the distance you'd travel between two city blocks if you can only move along streets (no diagonals). The Manhattan distance is always ≥ the Euclidean distance, with equality when the two points share a coordinate (Δx = 0 or Δy = 0). In sparse high-dimensional spaces (e.g., text data), L₁ distance sometimes outperforms L₂.

Unit vector (direction cosines)

The unit vector from P₁ to P₂ gives the direction of travel without encoding the distance:

û = (Δx/d, Δy/d, Δz/d)

Its components are called direction cosines in 3D — the cosines of the angles the vector makes with each axis. The length (magnitude) of a unit vector is always 1.

Real-world applications

FieldApplication
PhysicsDisplacement, work done, force components
Computer graphicsCollision detection, ray tracing, camera frustum
Machine learningk-NN, clustering, loss functions (RMSE)
GPS / mappingHaversine formula (distance on a sphere) builds on 2D distance
RoboticsPath planning, inverse kinematics in 3D workspace
Game developmentCharacter movement, level-of-detail scaling
Data scienceFeature similarity, anomaly detection