liveBook · Manning

Euclidean distance

As explained in Classic Computer Science Problems in Swift: Essential techniques for practicing programmers

Euclidean distance is the straight-line distance between two points calculated from the square root of the sum of squared coordinate differences.

Euclidean distance is the straight-line distance between two points. For maze locations, treat columns as x-coordinates and rows as y-coordinates: distance = √((column difference)² + (row difference)²). The formula follows from the Pythagorean theorem. section 2.2

func euclideanDistance(ml: MazeLocation) -> Float {
    let xdist = ml.col - goal.col
    let ydist = ml.row - goal.row
    return sqrt(Float((xdist * xdist) + (ydist * ydist)))
}

This Swift function measures a maze location’s distance to the goal. It squares the row and column differences, adds them, and takes the square root. section 2.2

In A* maze search, this distance serves as a heuristic: a straight line cannot be longer than a route around walls. An admissible heuristic never overestimates the remaining distance. For a maze that permits only horizontal and vertical moves, Manhattan distance follows the allowed movement more closely and can guide the search through fewer states. section 2.2

The same idea extends beyond two dimensions. To measure two data points, square the difference in each corresponding dimension, sum those squares, and take the square root. This supplies a distance measure commonly used with k-means clustering. section 6.1

extension DataPoint {
    // Euclidean distance
    func distance<PointType: DataPoint>(to: PointType) -> Double {
        return sqrt(zip(dimensions, to.dimensions).map({
         pow(($0.1 - $0.0), 2) }).sum)
    }
}

The DataPoint extension implements that sequence with Swift’s zip(), map(), sum, and sqrt(), so one method works across different numbers of dimensions. section 6.1

A three-dimensional check

For (2, 1, 1) and (2, 2, 5), the coordinate differences are 0, 1, and 4. Their Euclidean distance is √(0² + 1² + 4²) = √17, approximately 4.123. section 6.1

How Classic Computer Science Problems in Swift: Essential techniques for practicing programmers covers it

How deep it goes
Derives the two-dimensional formula from geometry and implements it for maze locations and data points with any number of dimensions.
Examples
Uses a Swift maze-location function and a three-dimensional point pair whose distance is approximately 4.123.
What it assumes
Assumes familiarity with coordinates, the Pythagorean theorem, and Swift functions; introduces the multidimensional calculation step by step.
What you can do after
Build it from scratch as a Swift distance function for maze locations or multidimensional DataPoint values.
How it connects
Links the Pythagorean theorem to A* search, admissible heuristics, Manhattan distance, and k-means clustering.

Also explained in

Compare how 8 books explain Euclidean distance →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Classic Computer Science Problems in Swift: Essential techniques for practicing programmers. The section links point to the parts of the book it draws on.