-
Notifications
You must be signed in to change notification settings - Fork 13
/
utils.ts
88 lines (83 loc) · 1.92 KB
/
utils.ts
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
export type RectSide = 'top' | 'right' | 'bottom' | 'left'
export type Vec2 = { x: number; y: number }
export type Box = {
x: number
y: number
w: number
h: number
}
/** Calculate the distance of two points. */
export function distanceOf(p1: Vec2, p2: Vec2): number {
return Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2))
}
/**
* @param box
* @param size Add `size` to all edges.
*/
export function growBox(box: Box, size: number): Box {
return {
x: box.x - size,
y: box.y - size,
w: box.w + 2 * size,
h: box.h + 2 * size,
}
}
export function isPointInBox(point: Vec2, box: Box): boolean {
return (
point.x > box.x &&
point.x < box.x + box.w &&
point.y > box.y &&
point.y < box.y + box.h
)
}
/** Calculate the control point. */
export function controlPointOf(
target: Vec2,
another: Vec2,
sideOfTarget: RectSide,
/**
* The distance a control point must be far away from the target in the
* direction of leaving the target.
*/
minDistanceToTarget: number = 50
) {
switch (sideOfTarget) {
case 'top': {
return {
x: target.x,
y: Math.min((target.y + another.y) / 2, target.y - minDistanceToTarget),
}
}
case 'bottom': {
return {
x: target.x,
y: Math.max((target.y + another.y) / 2, target.y + minDistanceToTarget),
}
}
case 'left': {
return {
x: Math.min((target.x + another.x) / 2, target.x - minDistanceToTarget),
y: target.y,
}
}
case 'right': {
return {
x: Math.max((target.x + another.x) / 2, target.x + minDistanceToTarget),
y: target.y,
}
}
}
}
/** Return the entering angle of a rectangle side. */
export function angleOf(enteringSide: RectSide): number {
switch (enteringSide) {
case 'left':
return 0
case 'top':
return 90
case 'right':
return 180
case 'bottom':
return 270
}
}