Files
30-seconds-of-code/snippets/euclideanDistance.md
2022-05-02 18:51:12 +03:00

25 lines
703 B
Markdown

---
title: Euclidean distance
tags: math,algorithm
expertise: intermediate
author: chalarangelo
cover: blog_images/ancient-greek-building.jpg
firstSeen: 2020-12-28T13:41:19+02:00
lastUpdated: 2020-12-28T13:41:19+02:00
---
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.
```js
const euclideanDistance = (a, b) =>
Math.hypot(...Object.keys(a).map(k => b[k] - a[k]));
```
```js
euclideanDistance([1, 1], [2, 3]); // ~2.2361
euclideanDistance([1, 1, 1], [2, 3, 2]); // ~2.4495
```