The distance between two points in a coordinate plane is calculated using the distance formula d = sqrt((x2 - x1)^2 + (y2 - y1)^2), which is derived from the Pythagorean theorem. This formula is foundational in coordinate geometry and has direct applications in mapping, game development, physics, and robotics.
Quick Answer
2D: d = sqrt((x2 - x1)^2 + (y2 - y1)^2)
3D: d = sqrt((x2 - x1)^2 + (y2 - y1)^2 + (z2 - z1)^2)
Plug in the coordinates, compute the differences, square them, add, and take the square root. Use the distance formula calculator for instant results.
Derivation from the Pythagorean Theorem
The distance formula is not an arbitrary equation -- it comes directly from the Pythagorean theorem.
Given two points (x1, y1) and (x2, y2):
- Draw a horizontal line from (x1, y1) to (x2, y1). Its length is |x2 - x1|.
- Draw a vertical line from (x2, y1) to (x2, y2). Its length is |y2 - y1|.
- These two lines form the legs of a right triangle. The line segment connecting the two original points is the hypotenuse.
By the Pythagorean theorem (a^2 + b^2 = c^2):
d^2 = (x2 - x1)^2 + (y2 - y1)^2
d = sqrt((x2 - x1)^2 + (y2 - y1)^2)
This derivation means the distance formula works for any two points, regardless of their position -- even when coordinate differences are negative, because squaring eliminates the sign. Use the Pythagorean calculator to explore this relationship.
Worked Example 1: Basic 2D Distance
Points: (1, 2) and (7, 10)
Step 1: Calculate differences.
- x2 - x1 = 7 - 1 = 6
- y2 - y1 = 10 - 2 = 8
Step 2: Square each difference.
- 6^2 = 36
- 8^2 = 64
Step 3: Add the squares.
- 36 + 64 = 100
Step 4: Take the square root.
- d = sqrt(100) = 10 units
This is a 6-8-10 right triangle (a multiple of the 3-4-5 Pythagorean triple).
Worked Example 2: Negative Coordinates
Points: (-3, 4) and (5, -2)
Step 1: Calculate differences.
- x2 - x1 = 5 - (-3) = 8
- y2 - y1 = -2 - 4 = -6
Step 2: Square each difference.
- 8^2 = 64
- (-6)^2 = 36
Step 3: Add and take the square root.
- d = sqrt(64 + 36) = sqrt(100) = 10 units
Negative differences do not affect the result because squaring makes them positive. The order of the points also does not matter -- the distance from A to B equals the distance from B to A.
Worked Example 3: 3D Distance
Points: (1, 2, 3) and (4, 6, 8)
The 3D formula extends the same principle with a third component:
d = sqrt((x2 - x1)^2 + (y2 - y1)^2 + (z2 - z1)^2)
Step 1: Calculate differences.
- x: 4 - 1 = 3
- y: 6 - 2 = 4
- z: 8 - 3 = 5
Step 2: Square each.
- 3^2 = 9
- 4^2 = 16
- 5^2 = 25
Step 3: Add and take the square root.
- d = sqrt(9 + 16 + 25) = sqrt(50) = 7.07 units
The 3D distance formula is used in physics (distance between particles), computer graphics (3D rendering), and aviation (distance accounting for altitude).
Special Cases
Horizontal Distance
When two points share the same y-coordinate, the distance simplifies:
Points (2, 5) and (9, 5): d = sqrt((9 - 2)^2 + (5 - 5)^2) = sqrt(49 + 0) = 7 units
This is simply |x2 - x1|.
Vertical Distance
When two points share the same x-coordinate:
Points (4, 1) and (4, 8): d = sqrt((4 - 4)^2 + (8 - 1)^2) = sqrt(0 + 49) = 7 units
This is simply |y2 - y1|.
Identical Points
When both points are the same: d = sqrt(0 + 0) = 0. Distance zero means the points coincide.
Manhattan Distance vs. Euclidean Distance
The distance formula gives the Euclidean distance -- the straight-line ("as the crow flies") distance. An alternative metric is Manhattan distance (also called taxicab distance or L1 distance):
Manhattan distance: d = |x2 - x1| + |y2 - y1|
This measures distance along grid lines, as if navigating city blocks where diagonal travel is not possible.
Comparison for points (1, 2) and (7, 10):
- Euclidean: sqrt(36 + 64) = sqrt(100) = 10
- Manhattan: |6| + |8| = 14
Manhattan distance is always greater than or equal to Euclidean distance. They are equal only when the points differ in exactly one coordinate (horizontal or vertical alignment).
When to use each:
| Metric | Use when | Examples |
|---|---|---|
| Euclidean | Straight-line travel is possible | Drone flight, open-field robots, physics |
| Manhattan | Movement is restricted to a grid | City navigation, chessboard problems, warehouse routing |
Connection to the Midpoint Formula
The midpoint of two points is the point exactly halfway between them:
Midpoint = ((x1 + x2) / 2, (y1 + y2) / 2)
Example: Points (2, 4) and (8, 10).
Midpoint = ((2 + 8) / 2, (4 + 10) / 2) = (5, 7)
You can verify this: the distance from (2, 4) to (5, 7) should equal the distance from (5, 7) to (8, 10).
- d1 = sqrt((5-2)^2 + (7-4)^2) = sqrt(9 + 9) = sqrt(18) = 4.24
- d2 = sqrt((8-5)^2 + (10-7)^2) = sqrt(9 + 9) = sqrt(18) = 4.24
Both distances are equal, confirming (5, 7) is the midpoint. The midpoint calculator handles this instantly.
Connection to Slope
The slope between two points describes the line's steepness:
Slope = (y2 - y1) / (x2 - x1)
Slope and distance are related but measure different things. Two points can have the same distance but different slopes, or the same slope but different distances. Together, distance and slope fully characterize the relationship between two points.
Real-World Applications
GPS and Mapping
For short distances (under a few kilometers), the flat-plane distance formula gives acceptable results. For longer distances, Earth's curvature matters. The Haversine formula calculates the great-circle distance between two latitude/longitude points:
a = sin^2(dlat/2) + cos(lat1) x cos(lat2) x sin^2(dlon/2)
d = 2 x R x arctan2(sqrt(a), sqrt(1 - a))
where R is Earth's radius (approximately 6,371 km). Mapping applications and GPS devices use this formula automatically. The flat distance formula would overestimate long distances because it ignores the curvature.
Game Development
Distance calculations are among the most frequent operations in game engines:
- Collision detection: Two objects collide when the distance between their centers is less than the sum of their radii.
- Range checks: An enemy attacks when the player is within a certain Euclidean distance.
- Pathfinding: Algorithms like A* use distance estimates (heuristics) to find shortest paths. Manhattan distance is commonly used as the heuristic on grid maps.
A performance trick: when only comparing distances (not computing exact values), skip the square root and compare squared distances instead. This is faster because sqrt is computationally expensive.
Robotics and Path Planning
Robots use distance formulas for navigation, obstacle avoidance, and determining the shortest path between waypoints. In 3D space, drones calculate distance to targets using the 3D formula, often combining it with heading and altitude data.
Physics
Distance between charges determines electric force (Coulomb's law). Distance between masses determines gravitational force (Newton's law of gravitation). Both laws use r^2 in the denominator, where r is the Euclidean distance between objects. Source: MathWorld distance formula reference.
Common Mistakes
Forgetting to square before adding. The formula requires squaring each difference before summing. Computing sqrt((x2-x1) + (y2-y1)) is wrong; it must be sqrt((x2-x1)^2 + (y2-y1)^2).
Taking the square root of each term separately. sqrt(a^2 + b^2) is NOT equal to a + b. For example, sqrt(9 + 16) = sqrt(25) = 5, not 3 + 4 = 7.
Mixing up 2D and 3D formulas. In 3D problems, forgetting the z-component underestimates the true distance. Always check whether your problem is 2D or 3D.
Confusing distance with displacement. Distance is always positive (a scalar). Displacement is a vector with direction. The distance formula gives the magnitude of the displacement vector.
Frequently Asked Questions
What is the distance formula?
The distance formula calculates the straight-line distance between two points in a coordinate plane: d = sqrt((x2 - x1)^2 + (y2 - y1)^2). It is derived from the Pythagorean theorem by treating the horizontal and vertical differences as legs of a right triangle.
How is the distance formula related to the Pythagorean theorem?
The distance formula is the Pythagorean theorem applied to coordinates. The horizontal distance (x2 - x1) and vertical distance (y2 - y1) form the two legs of a right triangle. The straight-line distance is the hypotenuse: c = sqrt(a^2 + b^2).
What is Manhattan distance?
Manhattan distance (taxicab distance) is the sum of the absolute horizontal and vertical differences: d = |x2 - x1| + |y2 - y1|. It measures distance along grid lines, like navigating city blocks, and is always greater than or equal to the Euclidean distance.
How do I find distance on a map?
For short distances on a flat map, use the distance formula with the map's coordinate system and scale factor. For long distances on Earth, use the Haversine formula, which accounts for Earth's curvature. Most mapping applications handle this automatically.
Does the distance formula work in 3D?
Yes. Add a z-component: d = sqrt((x2 - x1)^2 + (y2 - y1)^2 + (z2 - z1)^2). The same Pythagorean principle extends to any number of dimensions.
What is the midpoint between two points?
The midpoint is the point exactly halfway between two endpoints. Its coordinates are the averages: midpoint = ((x1 + x2) / 2, (y1 + y2) / 2). For points (2, 4) and (8, 10), the midpoint is (5, 7).
Can the distance between two points be negative?
No. Distance is always zero or positive. The squaring in the formula ensures that negative coordinate differences become positive, and the square root returns a non-negative value.
What is the distance between two points on a number line?
On a 1D number line, distance simplifies to the absolute difference: d = |x2 - x1|. For points at positions -3 and 7, d = |7 - (-3)| = 10.
How is the distance formula used in game development?
Game developers use it for collision detection (checking if two objects overlap), range checks (triggering events when a player is close enough), and pathfinding heuristics (estimating remaining distance in algorithms like A*). Manhattan distance is preferred for grid-based games.
What is the Haversine formula?
The Haversine formula calculates the great-circle distance between two points on a sphere (such as Earth) using their latitudes and longitudes. It accounts for curvature, making it accurate for geographic distances where the flat-plane distance formula would introduce significant error.