-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.js
More file actions
43 lines (39 loc) · 907 Bytes
/
vector.js
File metadata and controls
43 lines (39 loc) · 907 Bytes
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
class Vec {
vector;
constructor(v1) {
this.vector = v1;
}
//vector addition
static add(v1, v2) {
//assuming lenghts are equal
var result = [];
v1.vector.map((x, index) => {
result[index] = v1.vector[index] + v2.vector[index];
});
return new Vec(result);
}
add(v1) {
this.vector.map((x, index) => {
this.vector[index] += v1.vector[index];
});
}
//multiplying scalar and vector
static scale(v1, s1) {
var result = [];
v1.vector.map((x, index) => {
result[index] = x * s1;
});
return new Vec(result);
}
static cross(v1, v2) {
return v1.vector[0] * v2.vector[1] - v2.vector[0] * v1.vector[1];
}
static subract(v1, v2) {
//assuming lenghts are equal
var result = [];
v1.vector.map((x, index) => {
result[index] = v1.vector[index] - v2.vector[index];
});
return new Vec(result);
}
}