Distance between two points

This commit is contained in:
Angelos Chalaris
2017-12-08 16:09:05 +02:00
parent 86b6c8b75d
commit e38dc0c7bc
2 changed files with 18 additions and 0 deletions

View File

@ -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.

View File

@ -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))
```