-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector.js
More file actions
80 lines (76 loc) · 1.54 KB
/
Vector.js
File metadata and controls
80 lines (76 loc) · 1.54 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
var Vector = function (x,y) {
this.x = x;
this.y = y;
};
Vector.prototype = {
getX : function () {
return this.x;
},
getY : function () {
return this.y;
},
getOffsets : function () {
return {x : this.x, y : this.y};
},
getScalar : function () {
var x = this.x,
y = this.y;
return Math.sqrt((x*x) + (y*y));
},
getTheta : function () {
var theta = Math.atan(this.y / this.x);
if (this.x < 0) {
theta += Math.PI;
}
return theta;
},
getScalarAndTheta : function () {
return {
theta : this.getTheta(),
scalar : this.getScalar()
};
},
setScalarAndTheta : function (scalar,theta) {
this.x = scalar * Math.cos(theta);
this.y = scalar * Math.sin(theta);
return this;
},
setTheta : function (theta) {
var scalar = this.getScalar();
this.setScalarAndTheta(scalar,theta);
return this;
},
setScalar : function (scalar) {
var theta = this.getTheta();
this.setScalarAndTheta(scalar,theta);
return this;
},
setX : function (x) {
this.x = x;
return this;
},
setY : function (y) {
this.y = y;
return this;
},
add : function (otherVector) {
this.x += otherVector.x;
this.y += otherVector.y;
return this;
},
getClone : function () {
return new Vector(this.x,this.y);
},
flipHorizontally : function () {
this.y = this.y * -1;
},
flipVertically : function () {
this.x = this.x * -1;
}
};
var getVectorFromScalarAndTheta = function (scalar,theta) {
return new Vector(scalar * Math.cos(theta),scalar * Math.sin(theta));
};
var getVectorFromOffsets = function (x,y) {
return new Vector(x,y);
};