-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.js
More file actions
96 lines (81 loc) · 1.73 KB
/
util.js
File metadata and controls
96 lines (81 loc) · 1.73 KB
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
89
90
91
92
93
94
95
96
export class Vec2 {
/**
*
* @param {number} x
* @param {number} y
*/
constructor (x, y) {
this.x = x;
this.y = y;
}
copy () {
return new Vec2(this.x, this.y);
}
/**
*
* @param {Vec2} v
*/
addSelf (v) {
this.x += v.x;
this.y += v.y;
return this;
}
add(v) {
return new Vec2(this.x + v.x, this.y + v.y);
}
sub(v) {
return new Vec2(this.x - v.x, this.y - v.y);
}
rotateSelf(rad) {
const c = Math.cos(rad);
const s = Math.sin(rad);
const {x, y} = this;
this.x = x * c + y * -s;
this.y = x * s + y * c;
return this;
}
rotate(rad) {
const c = Math.cos(rad);
const s = Math.sin(rad);
const {x, y} = this;
return new Vec2(x * c + y * -s, x * s + y * c);
}
scaleSelf(length) {
this.x *= length;
this.y *= length;
return this;
}
scale(length) {
return new Vec2(this.x * length, this.y * length);
}
normalizeSelf () {
const length = Math.hypot(this.x, this.y);
this.x /= length;
this.y /= length;
return this;
}
normalize() {
const length = Math.hypot(this.x, this.y);
return new Vec2(this.x / length, this.y / length);
}
/**
*
* @param {Vec2} vec
*/
dot (vec) {
return this.x * vec.x + this.y * vec.y;
}
/**
*
* @param {Vec2} vec
*/
cross (vec) {
return this.x * vec.y - vec.x * this.y;
}
get dir () {
return Math.atan2(this.y, this.x);
}
mag() {
return Math.sqrt(this.x ** 2 + this.y ** 2);
}
}