Menu

gitpiper

euclideanDistance javascript Code Snippet in 2024

mathalgorithmintermediate

Last Updated: 17 September 2024

Calculates the distance between two points in any number of dimensions.

  • Use Object.keys() and Array.prototype.map() to map each coordinate to its difference between the two points.
  • Use Math.hypot() to calculate the Euclidean distance between the two points.
const euclideanDistance = (a, b) => Math.hypot(...Object.keys(a).map(k => b[k] - a[k]));
euclideanDistance([1, 1], [2, 3]); // ~2.2361 euclideanDistance([1, 1, 1], [2, 3, 2]); // ~2.4495

javascript snippet similar to euclideanDistance For You in September 2024

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2024 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️