-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector2D.js
129 lines (125 loc) · 2.6 KB
/
Vector2D.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
Simple 2D JavaScript Vector Class
Hacked from evanw's lightgl.js
https://github.com/evanw/lightgl.js/blob/master/src/vector.js
*/
function Vector(x, y) {
this.x = x || 0;
this.y = y || 0;
}
/* INSTANCE METHODS */
Vector.prototype = {
negative: function() {
this.x = -this.x;
this.y = -this.y;
return this;
},
add: function(v) {
if (v instanceof Vector) {
this.x += v.x;
this.y += v.y;
} else {
this.x += v;
this.y += v;
}
return this;
},
subtract: function(v) {
if (v instanceof Vector) {
this.x -= v.x;
this.y -= v.y;
} else {
this.x -= v;
this.y -= v;
}
return this;
},
multiply: function(v) {
if (v instanceof Vector) {
this.x *= v.x;
this.y *= v.y;
} else {
this.x *= v;
this.y *= v;
}
return this;
},
divide: function(v) {
if (v instanceof Vector) {
if(v.x != 0) this.x /= v.x;
if(v.y != 0) this.y /= v.y;
} else {
if(v != 0) {
this.x /= v;
this.y /= v;
}
}
return this;
},
equals: function(v) {
return this.x == v.x && this.y == v.y;
},
dot: function(v) {
return this.x * v.x + this.y * v.y;
},
cross: function(v) {
return this.x * v.y - this.y * v.x
},
length: function() {
return Math.sqrt(this.dot(this));
},
normalize: function() {
return this.divide(this.length());
},
min: function() {
return Math.min(this.x, this.y);
},
max: function() {
return Math.max(this.x, this.y);
},
toAngles: function() {
return -Math.atan2(-this.y, this.x);
},
angleTo: function(a) {
return Math.acos(this.dot(a) / (this.length() * a.length()));
},
toArray: function(n) {
return [this.x, this.y].slice(0, n || 2);
},
clone: function() {
return new Vector(this.x, this.y);
},
set: function(x, y) {
this.x = x; this.y = y;
return this;
}
};
/* STATIC METHODS */
Vector.negative = function(v) {
return new Vector(-v.x, -v.y);
};
Vector.add = function(a, b) {
if (b instanceof Vector) return new Vector(a.x + b.x, a.y + b.y);
else return new Vector(a.x + b, a.y + b);
};
Vector.subtract = function(a, b) {
if (b instanceof Vector) return new Vector(a.x - b.x, a.y - b.y);
else return new Vector(a.x - b, a.y - b);
};
Vector.multiply = function(a, b) {
if (b instanceof Vector) return new Vector(a.x * b.x, a.y * b.y);
else return new Vector(a.x * b, a.y * b);
};
Vector.divide = function(a, b) {
if (b instanceof Vector) return new Vector(a.x / b.x, a.y / b.y);
else return new Vector(a.x / b, a.y / b);
};
Vector.equals = function(a, b) {
return a.x == b.x && a.y == b.y;
};
Vector.dot = function(a, b) {
return a.x * b.x + a.y * b.y;
};
Vector.cross = function(a, b) {
return a.x * b.y - a.y * b.x;
};