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.
Inputs
Two points
- P1
- (1, 2)
- P2
- (4, 6)
- Distance
- 5
Euclidean distance
Between (1, 2) and (4, 6)
Visualisation
Coordinate plane
Step-by-step
Calculation breakdown
- 1d = √((x₂−x₁)² + (y₂−y₁)²)
- 2d = √((4−1)² + (6−2)²)
- 3d = √((3)² + (4)²)
- 4d = √(9 + 16)
- 5d = √25
- 6d = 5
Properties
Derived values
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
dwe want
Applying the Pythagorean theorem (a² + b² = c²) with a = |Δx|, b = |Δy|, c = d:
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 = √(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:
This extends naturally to any number of dimensions n:
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:
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:
and then the slope-intercept form with one of the points:
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:
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:
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:
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
| Field | Application |
|---|---|
| Physics | Displacement, work done, force components |
| Computer graphics | Collision detection, ray tracing, camera frustum |
| Machine learning | k-NN, clustering, loss functions (RMSE) |
| GPS / mapping | Haversine formula (distance on a sphere) builds on 2D distance |
| Robotics | Path planning, inverse kinematics in 3D workspace |
| Game development | Character movement, level-of-detail scaling |
| Data science | Feature similarity, anomaly detection |