Last active
August 29, 2015 14:23
-
-
Save DataKinds/173f0a46444b44254e32 to your computer and use it in GitHub Desktop.
3d vector library
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
var Vector = {}; | |
Vector.Rect = function(x, y, z) { | |
this.magnitude = Math.sqrt(x^2, y^2, z^2); | |
this.toRect = function() { return this; } | |
this.toPolar = function() { return Vector.Polar(magnitude, Math.arccos(z/magnitude), Math.arcsin(y/magnitude)) } | |
this.x = x; | |
this.y = y; | |
this.z = z; | |
} | |
Vector.Polar = function(magnitude, rx, rz) { | |
this._vec = new Vector.Rect(-magnitude * Math.cos(rz), magnitude * Math.sin(rz), magnitude * Math.cos(rx)); | |
this.magnitude = magnitude; | |
this.toRect = function() { return this._vec; } | |
this.toPolar = function() { return this; } | |
this.rx = rx; | |
this.rz = rz; | |
} | |
Vector.add = function(v1, v2) { | |
return new Vector.Rect(v1.toRect().x + v2.toRect().x, v1.toRect().y + v2.toRect().y, v1.toRect().z + v2.toRect().z); | |
} | |
Vector.subtract = function(v1, v2) { | |
return new Vector.Rect(v1.toRect().x - v2.toRect().x, v1.toRect().y - v2.toRect().y, v1.toRect().z - v2.toRect().z); | |
} | |
Vector.product = function(s,v) { | |
return new Vector.Rect(s * v.toRect().x, s * v.toRect().y, s * v.toRect().z); | |
} | |
Vector.dotProduct = function(v1, v2) { | |
return (v1.toRect().x * v2.toRect().x + v1.toRect().y * v2.toRect().y + v1.toRect().z * v2.toRect().z); | |
} | |
Vector.crossProduct = function(v1, v2) { | |
return new Vector.Rect(v1.toRect().y * v2.toRect().z - v1.toRect().z * v2.toRect().y, v1.toRect().z * v2.toRect().x - v1.toRect().x * v2.toRect().z, v1.toRect().x * v2.toRect().y - v1.toRect().y * v2.toRect().x); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment