Created
October 17, 2013 15:36
-
-
Save grifdail/7027134 to your computer and use it in GitHub Desktop.
Vector Manipulation in JS made easy
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 characters
| function Vector (x,y) { | |
| this.x = x; | |
| this.y = y; | |
| } | |
| Vector.prototype.length = function() { | |
| return Math.sqrt(this.x*this.x + this.y*this.y); | |
| }; | |
| Vector.prototype.add = function(vector) { | |
| this.x += vector.x; | |
| this.y += vector.y; | |
| return this; | |
| }; | |
| Vector.prototype.factor = function(factor) { | |
| this.x *= factor; | |
| this.y *= factor; | |
| return this; | |
| }; | |
| Vector.prototype.unit = function() { | |
| var length = this.length(); | |
| return new Vector(this.x/length, this.y/length); | |
| }; | |
| Vector.prototype.clone = function() { | |
| return new Vector(this.x, this.y); | |
| }; | |
| Vector.prototype.angle = function() { | |
| var unit = this.unit(); | |
| if (unit.y < 0) { | |
| return - Math.acos(unit.x); | |
| } else { | |
| return Math.acos(unit.x); | |
| } | |
| }; | |
| Vector.prototype.to = function(vector) { | |
| return new Vector(vector.x-this.x, vector.y-this.y) | |
| }; | |
| Vector.prototype.set = function(x,y) { | |
| this.x = x; | |
| this.y = y; | |
| return this; | |
| }; | |
| Vector.direction = function(x1, y1, x2, y2) { | |
| return new Vector(x2-x1, y2-y1); | |
| } | |
| Vector.fromAngle = function(alpha) { | |
| return new Vector(Math.cos(alpha), Math.sin(alpha)); | |
| } | |
| Vector.prototype.addAngle = function(alpha) { | |
| var length = this.length(); | |
| alpha += this.angle(); | |
| this.x = Math.cos(alpha)*length; | |
| this.y = Math.sin(alpha)*length; | |
| return this | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment