-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.js
More file actions
86 lines (73 loc) · 1.47 KB
/
vector.js
File metadata and controls
86 lines (73 loc) · 1.47 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
class vector {
constructor() {
this._x = 1;
this._y = 0;
}
static create(x, y) {
var obj = new vector();
obj.setX(x);
obj.setY(y);
return obj;
}
setX(value) {
this._x = value;
}
getX() {
return this._x;
}
setY(value) {
this._y = value;
}
getY() {
return this._y;
}
setAngle(angle) {
let length = this.getLength();
this._x = Math.cos(angle) * length;
this._y = Math.sin(angle) * length;
}
getAngle() {
console.log('angle to ', Math.atan2(this._y, this._x)*(180/Math.PI));
return Math.atan2(this._y, this._x);
}
setLength(length) {
let angle = this.getAngle();
this._x = Math.cos(angle) * length;
this._y = Math.sin(angle) * length;
}
getLength() {
return Math.sqrt(this._x * this._x + this._y * this._y);
}
add(v2) {
return vector.create(this._x + v2.getX(), this._y + v2.getY());
}
subtract(v2) {
return vector.create(this._x - v2.getX(), this._y - v2.getY());
}
//scalar multiplication
multiply(val) {
return vector.create(this._x *val, this._y *val);
}
//scalar division
divide(val) {
return vector.create(this._x/val, this._y/val);
}
addTo(v2) {
this._x+=v2.getX();
this._y+=v2.getY();
}
subtractFrom(v2) {
this._x-=v2.getX();
this._y-=v2.getY();
}
//scalar multiplication
multiplyTo(val) {
this._x*=val;
this._y*=val;
}
//scalar division
divideFrom(val) {
this._x/=val;
this._y/=val;
}
}