/
Vector2.js
37 lines (28 loc) · 845 Bytes
/
Vector2.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
var App = App || {};
App.Vector2 = (function() {
function Vector2(x, y) {
this.x = x;
this.y = y;
}
Vector2.prototype.length = function() {
return Math.sqrt(this.x * this.x + this.y * this.y);
};
Vector2.prototype.scale = function(scale){
this.x = this.x * scale;
this.y = this.y * scale;
return this;
};
Vector2.prototype.add = function(vec){
return new Vector2(this.x + vec.x, this.y + vec.y);
};
Vector2.prototype.sub = function(vec){
return new Vector2(this.x - vec.x, this.y - vec.y);
};
Vector2.add = function(vec1, vec2) {
return new Vector2(vec1.x+vec2.x, vec1.y + vec2.y);
};
Vector2.sub = function(vec1, vec2) {
return new Vector2(vec1.x-vec2.x, vec1.y-vec2.y);
};
return Vector2;
})();