Objective
Learning objectives
- State the Pythagorean theorem and identify the hypotenuse as straight-line distance
- Derive the distance formula from the Pythagorean theorem
- Implement a distance function for 2D points in Python
- Generalize the same distance formula to points with more than two coordinates
Hook
An AI model represents a word as a list of numbers — an embedding — often 768 of them at once. Two words with similar meanings end up as two points that are close together in that 768-number space. That should sound strange: what does "close together" even mean once you're past three dimensions, let alone 768? Set that aside for a moment and start smaller — with just two ordinary points on a flat plane. The full answer turns out not to need anything new once you get there.
See it
Take two points, (0, 0) and (3, 4). Draw a horizontal line from the first point out to 3, then a vertical line up to 4 — that traces two legs of a right triangle, with lengths 3 and 4. The straight-line distance between the two original points is exactly the triangle's third side: the hypotenuse, cutting diagonally across.
This is why it's called the distance formula, not a distance formula
Any two points, anywhere, can be connected by exactly this construction — a horizontal leg, a vertical leg, and the straight-line distance as the hypotenuse between them. There's only one relationship between a right triangle's three sides, so there's only one formula for straight-line distance. It isn't a special case; it's the general case, every time.
Name it
The Pythagorean theorem relates the three sides of any right triangle: a² + b² = c², where a and b are the two legs and c is the hypotenuse — the side opposite the right angle, always the longest of the three.
The distance formula is the Pythagorean theorem solved for c, with the legs written as coordinate differences: for two points (x1, y1) and (x2, y2), the horizontal leg is x2 - x1, the vertical leg is y2 - y1, and the distance is:
distance = √((x2 - x1)² + (y2 - y1)²)
For (0, 0) and (3, 4): legs 3 and 4, so distance = √(9 + 16) = √25 = 5.
Code it
import math
def distance_2d(p1, p2):
return math.sqrt((p2[0] - p1[0])**2 + (p2[1] - p1[1])**2)
print(distance_2d((0, 0), (3, 4)))
print(distance_2d((1, 1), (4, 5)))
5.0
5.0
Both pairs are 5.0 apart, even though the second pair sits nowhere near the origin — the formula only cares about the difference between the points, never their absolute position. Now generalize it. Nothing about the underlying idea changes when there are more coordinates — only the number of terms being summed before the square root:
def distance(p1, p2):
return math.sqrt(sum((b - a)**2 for a, b in zip(p1, p2)))
print(distance((0, 0), (3, 4)))
print(distance((1, 2, 2), (4, 6, 2)))
5.0
5.0
distance() works identically on the same 2D points as distance_2d() — and just as well on a 3D point pair, with zero changes to the idea, only to how many coordinate differences get squared and summed. This is the direct answer to the Hook: a 768-number embedding distance is this exact same function, called on two 768-number tuples instead of two 2D ones. Nothing new needs to be learned to get there — only more terms in the same sum.
print(distance((2, 0, 0, 1), (2, 3, 4, 1)))
The first and last coordinates match exactly (2 and 1), contributing zero to the sum — only the middle two differ, by 3 and 4. That's √(0² + 3² + 4² + 0²) = √(9 + 16) = √25 = 5.0: the same 3-4-5 triangle from See It, just carried inside a 4-coordinate point where two of the coordinates happen to agree.