2020-03-08 19:25:16 -07:00
|
|
|
import { PointerCoords } from "./types";
|
2020-02-21 08:17:20 -05:00
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
export const getCenter = (pointers: Map<number, PointerCoords>) => {
|
2020-03-08 19:25:16 -07:00
|
|
|
const allCoords = Array.from(pointers.values());
|
2020-02-21 08:17:20 -05:00
|
|
|
return {
|
2021-01-31 10:47:43 +01:00
|
|
|
x: sum(allCoords, (coords) => coords.x) / allCoords.length,
|
|
|
|
y: sum(allCoords, (coords) => coords.y) / allCoords.length,
|
2020-02-21 08:17:20 -05:00
|
|
|
};
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|
2020-02-21 08:17:20 -05:00
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
export const getDistance = ([a, b]: readonly PointerCoords[]) =>
|
|
|
|
Math.hypot(a.x - b.x, a.y - b.y);
|
2020-02-21 08:17:20 -05:00
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
const sum = <T>(array: readonly T[], mapper: (item: T) => number): number =>
|
|
|
|
array.reduce((acc, item) => acc + mapper(item), 0);
|