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