-
Notifications
You must be signed in to change notification settings - Fork 6
/
Vector.js
68 lines (53 loc) · 1.36 KB
/
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
function Vector (magnitude, angle) {
var m, a;
this.getX = function () {
return m * Math.cos(a);
};
this.setX = function (x) {
var y = m * Math.sin(a);
m = Math.sqrt((x * x) + (y * y));
a = Math.atan2(y, x);
};
this.getY = function () {
return m * Math.sin(a);
};
this.setY = function (y) {
var x = m * Math.cos(a);
m = Math.sqrt((x * x) + (y * y));
a = Math.atan2(y, x);
};
this.getMag = function () {
return m;
};
this.setMag = function (magnitude) {
m = magnitude;
};
this.getAngle = function () {
return a;
};
this.setAngle = function (angle) {
a = angle;
};
this.add = function (v) {
return Vector.add(this, v);
};
this.subtract = function (v) {
return Vector.subtract(this, v);
};
this.setMag(magnitude);
this.setAngle(angle);
}
Vector.rectangular = function (x, y) {
var m = Math.sqrt(x * x + y * y),
a = Math.atan2(y, x);
return new Vector(m, a);
};
Vector.polar = function (m, a) {
return new Vector(m, a);
};
Vector.add = function (v1, v2) {
return Vector.rectangular(v1.getX() + v2.getX(), v1.getY() + v2.getY());
};
Vector.subtract = function (v1, v2) {
return Vector.rectangular(v1.getX() - v2.getX(), v1.getY() - v2.getY());
};