-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector2.js
37 lines (37 loc) · 918 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
class Vector2 {
constructor(x, y) {
this.x = x;
this.y = y;
}
length() {
return Math.sqrt(this.x * this.x + this.y * this.y);
}
normalize() {
const l = this.length();
if (l == 0)
return new Vector2(0, 0);
return new Vector2(this.x / l, this.y / l);
}
scale(value) {
return new Vector2(this.x * value, this.y * value);
}
add(other) {
return new Vector2(this.x + other.x, this.y + other.y);
}
sub(other) {
return new Vector2(this.x - other.x, this.y - other.y);
}
div(other) {
return new Vector2(this.x / other.x, this.y / other.y);
}
mul(other) {
return new Vector2(this.x * other.x, this.y * other.y);
}
distanceTo(other) {
return other.sub(this).length();
}
array() {
return [this.x, this.y];
}
}
export default Vector2;