All files manhattan-distance.ts

100% Statements 3/3
100% Branches 1/1
100% Functions 1/1
100% Lines 3/3

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24                                        1x 9x 9x  
import { type Cartesian } from './@types/geometry.ts';
 
/**
 * Calculates the Manhattan Distance between two points in Cartesian coordinates.
 *
 * The Manhattan Distance is based on the number of blocks that one would have
 * to walk in Manhattan to get from one point to another.   It is assumed that
 * "Manhattan" is an orthogonal grid where no diagonal movement is allowed.
 *
 * Manhattan distance is often used in grid-based path-finding algorithms.
 * @param a - The first point.
 * @param b - The second point.
 * @returns The Manhattan distance between the two points.
 * @example
 * ```typescript
 * manhattanDistance({ x: 1, y: 2 }, { x: 4, y: 6 }); // 7
 * ```
 * @group Geometry
 * @category Point
 */
export function manhattanDistance(a: Cartesian, b: Cartesian): number {
  return Math.abs(a.x - b.x) + Math.abs(a.y - b.y);
}