From bc6f21d78a7e3d139ffa0f36b4c0f0547f7ad89d Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Fri, 8 Dec 2017 16:09:05 +0200 Subject: [PATCH] Distance between two points --- README.md | 10 ++++++++++ snippets/distance-between-two-points.md | 8 ++++++++ 2 files changed, 18 insertions(+) create mode 100644 snippets/distance-between-two-points.md diff --git a/README.md b/README.md index e0bfd8aa8..782f34c86 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ * [Capitalize first letter](#capitalize-first-letter) * [Count occurences of a value in array](#count-occurences-of-a-value-in-array) * [Current URL](#current-url) +* [Distance between two points](#distance-between-two-points) * [Even or odd number](#even-or-odd-number) * [Factorial](#factorial) * [Fibonacci array generator](#fibonacci-array-generator) @@ -85,6 +86,15 @@ Use `window.location.href` to get current URL. var currentUrl = _ => window.location.href; ``` +### Distance between two points + +Use `Math.pow()` and `Math.sqrt()` to calculate the Euclidean distance between two points. + +```js +var distance = x0, y0, x1, y1 => + Math.sqrt(Math.pow(x1-x0, 2) + Math.pow(y1 - y0, 2)) +``` + ### Even or odd number Use `Math.abs()` to extend logic to negative numbers, check using the modulo (`%`) operator. diff --git a/snippets/distance-between-two-points.md b/snippets/distance-between-two-points.md new file mode 100644 index 000000000..e8c91eafd --- /dev/null +++ b/snippets/distance-between-two-points.md @@ -0,0 +1,8 @@ +### Distance between two points + +Use `Math.pow()` and `Math.sqrt()` to calculate the Euclidean distance between two points. + +```js +var distance = x0, y0, x1, y1 => + Math.sqrt(Math.pow(x1-x0, 2) + Math.pow(y1 - y0, 2)) +```