Distance between two points
This commit is contained in:
10
README.md
10
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.
|
||||
|
||||
8
snippets/distance-between-two-points.md
Normal file
8
snippets/distance-between-two-points.md
Normal 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))
|
||||
```
|
||||
Reference in New Issue
Block a user