Created
July 6, 2016 05:25
-
-
Save Deftwun/79f2f3de728bc8986388c796fe1b60e3 to your computer and use it in GitHub Desktop.
This file contains 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
//2d Vector class (based on Vector.js) | |
var Vector = function(x,y){ this.x = x || 0; this.y = y || 0; }; | |
Vector.prototype={ | |
rot: function(a) { | |
var theta = a * Math.PI/180, | |
cs = Math.cos(theta), | |
sn = Math.sin(theta), | |
px = this.x * cs - this.y * sn; | |
py = this.x * sn + this.y * cs; | |
this.x = px; | |
this.y = py; | |
return this; | |
}, | |
add: function(x,y) { return new Vector(this.x + x, this.y + y); }, | |
sub: function(x,y) { return new Vector(this.x - x, this.y - y) }, | |
mul: function(x,y) { return new Vector(this.x * x, this.y * y) }, | |
neg: function() { return new Vector(-this.x, -this.y);}, | |
angle: function() { return this.y / this.length();}, | |
clone: function() { return new Vector(this.x,this.y);}, | |
dot: function(v) { return this.x * v.x + this.y * v.y; }, | |
len: function() { return Math.sqrt(this.dot(this)) }, | |
unit: function() { return this.mul(1 / this.len()) }, | |
angleTo: function(v) { return Math.acos(this.dot(a) / (this.length() * a.length())); } | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment