diginext-utils
Version:
README.md
21 lines • 634 B
JavaScript
/**
* Calculates the Euclidean distance between two points in 2D space.
* Returns 0 if the result is not finite.
*
* @param x1 - X coordinate of first point
* @param y1 - Y coordinate of first point
* @param x2 - X coordinate of second point
* @param y2 - Y coordinate of second point
* @returns The distance between the two points
*
* @example
* ```ts
* distance(0, 0, 3, 4); // 5
* distance(1, 1, 4, 5); // 5
* ```
*/
export function distance(x1, y1, x2, y2) {
const dist = Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
return Number.isFinite(dist) ? dist : 0;
}
//# sourceMappingURL=distance.js.map