Files
30-seconds-of-code/javascript/snippets/midpoint.md
2023-05-01 22:35:56 +03:00

23 lines
523 B
Markdown

---
title: Midpoint
type: snippet
tags: [math]
cover: curve
dateModified: 2020-10-21T21:54:53+03:00
---
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]
```