Distance Formula

Calculate the distance between two points in 2D and 3D space using the distance formula derived from the Pythagorean theorem. Includes worked examples with sqrt.

Geometry

Detailed Explanation

Distance Formula

The distance between two points is derived from the Pythagorean theorem and is one of the most used formulas in geometry and programming.

2D Distance

For points (x1, y1) and (x2, y2):

d = sqrt((x2 - x1)^2 + (y2 - y1)^2)

Example: Distance from (1, 2) to (4, 6):

sqrt((4-1)^2 + (6-2)^2) = sqrt(9 + 16) = sqrt(25) = 5

3D Distance

For points (x1, y1, z1) and (x2, y2, z2):

d = sqrt((x2-x1)^2 + (y2-y1)^2 + (z2-z1)^2)

Example: Distance from (0, 0, 0) to (3, 4, 12):

sqrt(3^2 + 4^2 + 12^2) = sqrt(9+16+144) = sqrt(169) = 13

Manhattan Distance

Sometimes you need the "city block" distance (sum of absolute differences):

d = abs(x2 - x1) + abs(y2 - y1)

Example: From (1, 3) to (4, 7):

abs(4-1) + abs(7-3) = 3 + 4 = 7

Midpoint Formula

The midpoint between two points:

midpoint_x = (x1 + x2) / 2
midpoint_y = (y1 + y2) / 2

Example: Midpoint of (2, 8) and (6, 4):

(2+6)/2 = 4
(8+4)/2 = 6

Midpoint is (4, 6).

Programming Use Cases

  • Collision detection: Check if distance between two objects < threshold
  • Nearest neighbor: Find the closest point in a dataset
  • Path length: Sum distances between consecutive waypoints

Use Case

A game developer calculating distance between two character positions for collision detection, or a data scientist computing Euclidean distances for clustering.

Try It — Math Expression Evaluator

Open full tool