-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector.js
46 lines (39 loc) · 856 Bytes
/
Vector.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
class Vector {
static squareDist(a, b) {
return Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2);
}
constructor(x, y) {
this.x = x;
this.y = y;
}
divideScalar(scalar) {
if (scalar !== 0) {
this.x /= scalar;
this.y /= scalar;
} else {
this.x = 0;
this.y = 0;
}
}
length() {
return Math.sqrt(this.lengthSq());
}
lengthSq() {
return this.x * this.x + this.y * this.y;
}
normalize() {
var length = this.length();
if (length === 0) {
this.x = 1;
this.y = 0;
} else {
this.divideScalar(length);
}
return this;
}
multiplyScalar(scalar) {
this.x *= scalar;
this.y *= scalar;
return this;
}
}