Distance Formula

d=(x2x1)2+(y2y1)2d = \sqrt{(x_2-x_1)^2 + (y_2-y_1)^2}

Description

The Distance Formula is a direct application of the Pythagorean Theorem used to find the straight-line distance between any two points in a Cartesian coordinate system. It allows us to measure "how far" two locations are from each other, even if the path is diagonal.

The formula is derived by creating a right-angled triangle where: * The **horizontal leg** is the change in x ($x_2 - x_1$). * The **vertical leg** is the change in y ($y_2 - y_1$). * The **hypotenuse** is the distance $d$ between the points.

This concept is fundamental not just in math class, but in computer science (collision detection in games), navigation (GPS), and physics. While this specific formula measures "Euclidean distance" (straight line), other metrics like "Manhattan distance" (grid-like paths) exist for different contexts.

History & Origins

The ability to calculate diagonal distances is tied to the history of the Pythagorean Theorem. Pythagoras (c. 570 BC): The underlying principle ($a^2 + b^2 = c^2$) was known to Babylonians and Indians long before, but the Greeks formalized it as a geometric truth. René Descartes (1637): The specific algebraic form we use today—using coordinates $(x,y)$—was made possible by Descartes' invention of analytic geometry. By merging algebra with geometry, he allowed distances to be calculated purely from numerical coordinates rather than by physical measurement.

Derivation using Pythagorean Theorem

We can build a right triangle between any two points to find the hypotenuse.

1

Let the two points be $P_1(x_1, y_1)$ and $P_2(x_2, y_2)$.

2

Draw a horizontal line from $P_1$ and a vertical line from $P_2$. They meet at a point $C(x_2, y_1)$.

3

The distance of the horizontal side (leg a) is $|x_2 - x_1|$.

4

The distance of the vertical side (leg b) is $|y_2 - y_1|$.

5

By the Pythagorean Theorem ($a^2 + b^2 = c^2$), the distance squared is $(x_2 - x_1)^2 + (y_2 - y_1)^2$.

6

Take the square root of both sides to get $d = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2}$.

Variables

Symbol Meaning
d Distance
(x₁, y₁) Coordinates of the first point
(x₂, y₂) Coordinates of the second point

Examples

Basic Calculation

Problem: Find the distance between (1, 2) and (4, 6)

Solution:

d = 5

Determining Radius

Problem: A circle has its center at (0, 0) and passes through the point (3, 4). What is the radius?

Solution: 5

  1. Identify points: Center $(x_1, y_1) = (0, 0)$, Edge $(x_2, y_2) = (3, 4)$.
  2. Substitute into formula: $r = \sqrt{(3-0)^2 + (4-0)^2}$.
  3. Simplify: $r = \sqrt{3^2 + 4^2}$.
  4. Calculate: $r = \sqrt{9 + 16} = \sqrt{25}$.
  5. Result: Radius = 5.

Video Game Collision

Problem: A player is at (10, 10) with a collision radius of 2. An enemy is at (12, 10). Are they colliding?

Solution: Yes

  1. Calculate distance: $d = \sqrt{(12-10)^2 + (10-10)^2}$.
  2. Simplify: $d = \sqrt{2^2 + 0^2} = \sqrt{4} = 2$.
  3. Compare to radius: The distance (2) is exactly equal to the collision radius.
  4. Conclusion: They are touching (colliding).

Common Mistakes

❌ Mistake

Subtracting in wrong order

✅ Correction

While $(x_2-x_1)^2$ is the same as $(x_1-x_2)^2$, it is good practice to be consistent. However, be careful not to mix x and y: $(x_2 - y_1)$ is wrong.

❌ Mistake

Forgetting to Square

✅ Correction

A common error is writing $\sqrt{(x_2-x_1) + (y_2-y_1)}$. You MUST square the differences before adding them.

❌ Mistake

Subtracting the Squares

✅ Correction

The formula is $a^2 + b^2$. Do not subtract them ($a^2 - b^2$) inside the root.

Real-World Applications

Machine Learning (KNN)

In AI, the "K-Nearest Neighbors" algorithm classifies data points based on which known groups they are closest to. It calculates the "Distance" between data points in multi-dimensional space to decide if an image is a cat or a dog.

Game Development

Games calculate distance thousands of times per second to check if a bullet hit a target, if a player is close enough to open a door, or to render 3D objects at the correct size based on how far away they are.

Frequently Asked Questions

Does the order of points matter?

No. Because you square the difference, $(5-2)^2 = 3^2 = 9$ and $(2-5)^2 = (-3)^2 = 9$. The result is the same.

What is Manhattan Distance?

Euclidean distance is "as the crow flies" (diagonal). Manhattan distance is like driving on city blocks: $|x_2 - x_1| + |y_2 - y_1|$. It is used in grid-based pathfinding.