-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.js
51 lines (44 loc) · 928 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
47
48
49
50
51
class Vector {
/**
*
* @param {Number} x
* @param {Number} y
*/
constructor(x, y) {
this.x = x; this.y = y;
}
get length() {
return Math.hypot(this.x, this.y);
}
get taxicab_length() {
return Math.abs(this.x) + Math.abs(this.y);
}
get clone() {
return new Vector(this.x, this.y);
}
/**
* @param {Vector} other
*/
add(other) {
return new Vector(this.x + other.x, this.y + other.y);
}
/**
* @param {Vector} other
*/
subtract(other) {
return new Vector(this.x - other.x, this.y - other.y);
}
/**
* @param {Number} factor
*/
multiply(factor) {
return new Vector(this.x * factor, this.y * factor);
}
/**
*
* @param {Number} new_length
*/
scale(new_length) {
return this.multiply(new_length / this.length);
}
}