-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector2.js
56 lines (49 loc) · 984 Bytes
/
vector2.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
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
class Vector2 {
constructor(x = 0, y = 0) {
this.x = x;
this.y = y;
}
add(value) {
this.x += value.x;
this.y += value.y;
}
static sum(a, b) {
return new Vector2(a.x + b.x, a.y + b.y);
}
subtract(value) {
this.x -= value.x;
this.y -= value.y;
}
static difference(a, b) {
return new Vector2(a.x - b.x, a.y - b.y);
}
times(value) {
this.x *= value;
this.y *= value;
}
static product(a, value) {
return new Vector2(a.x * value, a.y * value);
}
divide(value) {
this.x /= value;
this.y /= value;
}
static fraction(a, value) {
return new Vector2(a.x / value, a.y / value);
}
magnitude() {
return Math.sqrt(this.x ** 2 + this.y ** 2);
}
normalize() {
this.divide(this.magnitude());
}
copy() {
return new Vector2(this.x, this.y);
}
squaredHypotenuse() {
return this.x ** 2 + this.y ** 2;
}
hypotenuse() {
return Math.sqrt(this.x ** 2 + this.y ** 2);
}
}