Skip to content
AI-grafen
CBuilderMathematics· about 30 min· fundamentals that rarely change· verified 2026-09-20· EN

Distance calculation and Pythagoras' theorem

Calculate distances between points and understand Pythagoras' theorem in the plane.

Practise in Mattegrafen ↗ · Geometri

Prerequisites

Intuition

Two points in a coordinate system: (1, 2) and (4, 6). How far apart are they?

Draw a right-angled triangle: the horizontal side is 4 − 1 = 3, the vertical side is 6 − 2 = 4.

Pythagoras' theorem: a² + b² = c² → 3² + 4² = 9 + 16 = 25 → c = √25 = 5.

The general formula:

d = √((x₂ − x₁)² + (y₂ − y₁)²)

Code

import math

def avstand(p, q):
    return math.sqrt(sum((a - b) ** 2 for a, b in zip(p, q)))

print(avstand((1, 2), (4, 6)))          # 5.0
print(round(avstand((0, 0), (1, 1)), 3))  # 1.414 = √2

# The same formula works in any number of dimensions:
print(round(avstand((1, 2, 3), (4, 6, 3)), 1))    # 5.0

Why this matters in AI: when words, images, or users are described as points (vectors), “resembling each other” is the same as “being close to each other”. Nearest neighbour, recommendations, and semantic search are all distance calculations — just in hundreds of dimensions instead of two. The formula is exactly the same.

Mastery means

  • Calculates the distance between two points using Pythagoras' theorem
  • Interprets distance as a measure of similarity

Sign in to do the exercises and build your mastery up.

Sources

All the sources and licences