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 25 26 27 28 29 30 31 | 7x 4x 2x 2x 2x 2x 2x 2x | import { Point } from "../types"
import { isPoint } from "./is-point"
import { isPoint3D } from "./is-point-3d"
import { isNum } from "./inc"
const distance1D = (a: number, b: number) => Math.abs(a - b)
/*
Distance
Returns the distance between two n dimensional points.
@param [object/number]: x and y or just x of point A
@param [object/number]: (optional): x and y or just x of point B
@return [number]: The distance between the two points
*/
export function distance<P extends Point | number>(a: P, b: P): number {
if (isNum(a) && isNum(b)) {
// 1D dimensions
return distance1D(a, b)
} else Eif (isPoint(a) && isPoint(b)) {
// Multi-dimensional
const xDelta = distance1D(a.x, b.x)
const yDelta = distance1D(a.y, b.y)
const zDelta = isPoint3D(a) && isPoint3D(b) ? distance1D(a.z, b.z) : 0
return Math.sqrt(xDelta ** 2 + yDelta ** 2 + zDelta ** 2)
}
}
|