function Vec2(x, y) { this.x = x || 0; this.y = y || 0; return this; } Vec2.prototype.clone = function() { return new Vec2(this.x, this.y); } Vec2.prototype.length = function() { return Math.sqrt((this.x * this.x) + (this.y * this.y)); } Vec2.prototype.set = function(vec) { this.x = vec.x; this.y = vec.y; return this; } Vec2.prototype.add = function(vec) { this.x += vec.x; this.y += vec.y; return this; } Vec2.prototype.sub = function(vec) { this.x -= vec.x; this.y -= vec.y; return this; } Vec2.prototype.scale = function(num) { this.x *= num; this.y *= num; return this; } Vec2.prototype.normalize = function() { var len = this.length(); if (len === 0) { this.x = 1; this.y = 0; } else { this.scale(1 / len); } return this; } Vec2.prototype.rotate = function(rad) { var x = this.x; var y = this.y; this.x = x * Math.cos(rad) - y * Math.sin(rad); this.y = y * Math.cos(rad) + x * Math.sin(rad); return this; } Vec2.prototype.angle = function() { return Math.atan2(this.y, this.x); }