Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- window.MathHelper = {};
- MathHelper.PiOver180 = window.Math.PI / 180;
- MathHelper.PiOver2 = window.Math.PI / 2;
- MathHelper.PiOver4 = window.Math.PI / 4;
- MathHelper.TwoPi = window.Math.PI * 2;
- MathHelper.ToRadians = function(Degrees)
- {
- return this.PiOver180 * Degrees;
- };
- MathHelper.ToDegrees = function(Radians)
- {
- return 180 / window.Math.PI * Radians;
- };
- function VectorObject()
- {
- this.LengthSquared = function()
- {
- return this.X * this.X + this.Y * this.Y + (this.Z ? this.Z * this.Z : 0);
- };
- this.Length = function()
- {
- return Math.sqrt(this.LengthSquared());
- };
- this.Normalize = function()
- {
- var Length = this.Length();
- this.X != 0 ? this.X /= Length : null;
- this.Y != 0 ? this.Y /= Length : null;
- this.Z != undefined ? this.Z != 0 ? this.Z /= Length : null : null;
- return this;
- };
- this.CreateVector = function(V)
- {
- if(V.length == 1)
- return V[0];
- return V[2] == undefined ? new Vector2(V[0], V[1]) : new Vector3(V[0], V[1], V[2]);
- };
- this.Dot = function()
- {
- var B = this.CreateVector(arguments);
- return this.X * B.X + this.Y * B.Y + (this.Z ? this.Z * B.Z : 0);
- };
- this.Cross = function()
- {
- var B = this.CreateVector(arguments);
- var C = new Vector3(this.Y * B.Z - this.Z * B.Y, this.Z * B.X - this.X * B.Z, this.X * B.Y - this.Y * B.X);
- this.X = C.X;
- this.Y = C.Y;
- this.Z = C.Z;
- return this;
- };
- this.Angle = function()
- {
- if(arguments.length == 0)
- return Math.atan(this.Y / this.X);
- var B = this.CreateVector(arguments);
- return Math.acos(this.Dot(B) / this.Length() / B.Length());
- };
- this.Add = function()
- {
- var B = this.CreateVector(arguments);
- this.X += B.X;
- this.Y += B.Y;
- B.Z != undefined ? this.Z += B.Z : null;
- return this;
- };
- this.Subtract = function()
- {
- var B = this.CreateVector(arguments);
- this.X -= B.X;
- this.Y -= B.Y;
- B.Z != undefined ? this.Z -= B.Z : null;
- return this;
- };
- this.Scale = function(S)
- {
- this.X *= S;
- this.Y *= S;
- this.Z != undefined ? this.Z *= S : null;
- return this;
- };
- this.Negate = function()
- {
- this.X *= -1;
- this.Y *= -1;
- this.Z != undefined ? this.Z *= -1 : null;
- return this;
- };
- this.Clone = function()
- {
- return this.Z == undefined ? new Vector2(this.X, this.Y) : new Vector3(this.X, this.Y, this.Z);
- };
- this.toString = function(Base)
- {
- var out = [this.X.toString(Base), this.Y.toString(Base)];
- this.Z != undefined ? out.push(this.Z.toString(Base)) : null;
- return out.join(',');
- };
- this.ToArray = function()
- {
- var out = [this.X, this.Y];
- this.Z != undefined ? out.push(this.Z) : null;
- return out;
- };
- this.Equals = function()
- {
- var B = this.CreateVector(arguments);
- if((this.Z == undefined && B.Z != undefined) || (B.Z == undefined && this.Z != undefined))
- return false;
- return this.X != B.X || this.Y != B.Y || (this.Z != undefined && this.Z != B.Z) ? false : true;
- };
- this.IsNull = function()
- {
- return this.X == 0 && this.Y == 0 && (this.Z != undefined ? this.Z == 0 : true) ? true : false;
- };
- }
- function Vector2(X, Y)
- {
- if(arguments.length == 1 && arguments[0].IsVector)
- {
- Vector2.call(this, arguments[0].X, arguments[0].Y);
- return;
- }
- VectorObject.call(this);
- this.X = X ? X : 0;
- this.Y = Y ? Y : 0;
- this.IsVector = true;
- }
- function Vector3(X, Y, Z)
- {
- if(arguments.length == 1 && arguments[0].IsVector)
- {
- Vector3.call(this, arguments[0].X, arguments[0].Y, arguments[0].Z != undefined ? arguments[0].Z : 0);
- return;
- }
- VectorObject.call(this);
- this.X = X ? X : 0;
- this.Y = Y ? Y : 0;
- this.Z = Z ? Z : 0;
- this.IsVector = true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement