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







