Last active
August 29, 2015 14:25
-
-
Save asarode/120f0f923ab31ad8e730 to your computer and use it in GitHub Desktop.
Revisions
-
asarode revised this gist
Jul 21, 2015 . No changes.There are no files selected for viewing
-
asarode created this gist
Jul 21, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,43 @@ /* * Initial steps of the mateogianolio/vectorious library * https://github.com/mateogianolio/gravity/blob/master/js/vector.js */ function Vector(x, y) { this.x = x; this.y = y; } // Addition Vector.prototype.add = function(vector) { return new Vector(this.x + vector.x, this.y + vector.y); }; // Subtraction Vector.prototype.subtract = function(vector) { return new Vector(this.x - vector.x, this.y - vector.y); }; // Dot product Vector.prototype.dot = function(vector) { return (this.x * vector.x + this.y * vector.y); } // Scaling Vector.prototype.scale = function(c) { return new Vector(this.x * c, this.y * c); } // Calculation of magnitude Vector.prototype.magnitude = function() { return Math.sqrt(this.x * this.x + this.y * this.y); } // Calculation of unit vector Vector.prototype.unitvector = function() { var mag = this.magnitude(); if(mag != 0) return new Vector(this.x / mag, this.y / mag); return new Vector(0, 0); }