Merge pull request #880 from Garebake/master

Added Midpoint.js
This commit is contained in:
Angelos Chalaris
2018-12-07 18:25:11 +02:00
committed by GitHub
3 changed files with 51 additions and 0 deletions

16
snippets/midpoint.md Normal file
View File

@ -0,0 +1,16 @@
### midpoint
Calculates the midpoint between two pairs of (x,y) points.
Destructure the array to get `x1`, `y1`, `x2` and `y2`, calculate the midpoint for each dimension by dividing the sum of the two endpoints by `2`.
```js
const midpoint = ([x1, y1], [x2, y2]) => [(x1 + x2) / 2, (y1 + y2) / 2];
```
```js
midpoint([2, 2], [4, 4]); // [3, 3]
midpoint([4, 4], [6, 6]); // [5, 5]
midpoint([1, 3], [2, 4]); // [1.5, 3.5]
```