Skip to content

Instantly share code, notes, and snippets.

@skiano
Last active December 23, 2017 09:08
Show Gist options
  • Save skiano/91d794e0484051c050c734cd5b63c26c to your computer and use it in GitHub Desktop.
Save skiano/91d794e0484051c050c734cd5b63c26c to your computer and use it in GitHub Desktop.
n-dimension distance
// not really about performance
// but fun to make it one line
// this discussion looked interesting re estimating
// https://stackoverflow.com/questions/3693514/very-fast-3d-distance-check
const distance = (a, b) => Math.sqrt(a.reduce((total, _, i) => total + Math.pow(b[i] - a[i], 2), 0))
const _2d_ = distance([1, 2], [0, 4])
const _3d_ = distance([1, 2, 3], [0, 4, 5])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment