-
Notifications
You must be signed in to change notification settings - Fork 4
/
helpers.js
31 lines (28 loc) · 876 Bytes
/
helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
export function getMidpoint(a, b, direction) {
if (!direction) direction = 'x';
const x1 = a.position[direction];
const x2 = b.position[direction];
return (x1 + x2) / 2;
}
/* Returns a dictionary where keys = name of keypoint, value = keypoint object */
export function getKeypoints(pose, parts) {
const keypoints = {};
if (parts) {
parts.forEach((part) => {
keypoints[part] = getKeypoint(pose, part);
})
} else {
pose.keypoints.forEach((keypoint) => {
keypoints[keypoint.part] = getKeypoint(pose, keypoint.part);
});
}
return keypoints;
}
export function getKeypoint(pose, part) {
return pose.keypoints.filter((keypoint) => keypoint.part === part )[0];
}
export function getDistance(a, b) {
const distX = a.position.x - b.position.x;
const distY = a.position.y - b.position.y;
return Math.sqrt(distX**2 + distY**2);
}