Node.js examples for Data Structure:Vector
Create Vector Class
Vec2 = function (x, y) { this.x = x || 0;/*from w w w. j av a2 s .c o m*/ this.y = y || 0; }; Vec2.prototype.set = function(x, y) { this.x = x; this.y = y; return this; }; Vec2.prototype.copy = function(vec) { this.x = vec.x; this.y = vec.y; return this; }; Vec2.prototype.translate = function(x, y) { this.x += x; this.y += y; return this; }; Vec2.prototype.scale = function(x, y) { this.x *= x; this.y *= y; return this; }; Vec2.prototype.rotate = function(rads) { var c = Math.cos(rads); var s = Math.sin(rads); this.set(c*this.x-s*this.y, s*this.x+c*this.y); return this; }; Vec2.prototype.normalize = function() { var mag = Math.sqrt(this.x*this.x + this.y*this.y); if (mag > 0) { this.x /= mag; this.y /= mag; } return this; };