UNPKG

@rawify/vector3

Version:

The RAW JavaScript 3D Vector library

2 lines (1 loc) 4.21 kB
!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):t.Vector=n()}(this,function(){"use strict";function t(t,n){this.x=t||0,this.y=n||0}function n(t){this.vector=t,this.__idx=0}return t.displayName="Vector",t.from=function(n){return new t(n[0],n[1])},t.fromAngle=function(n,i){return new t(i*Math.cos(n),i*Math.sin(n))},t.parse=function(n){return t.from(n.trim().replace(","," ").split(/\s+/).map(parseFloat))},t.add=function(t,n){return n.add(t)},t.prototype.add=function(n){return new t(this.x+n.x,this.y+n.y)},t.iadd=function(t,n){return n.iadd(t)},t.prototype.iadd=function(t){return this.x+=t.x,this.y+=t.y,this},t.sub=function(t,n){return n.sub(t)},t.prototype.sub=function(n){return new t(this.x-n.x,this.y-n.y)},t.isub=function(t,n){return n.isub(t)},t.prototype.isub=function(t){return this.x-=t.x,this.y-=t.y,this},t.mul=function(t,n){return n.mul(t)},t.prototype.mul=function(n){return new t(this.x*n,this.y*n)},t.imul=function(t,n){return n.imul(t)},t.prototype.imul=function(t){return this.x*=t,this.y*=t,this},t.div=function(t,n){return n.div(t)},t.prototype.div=function(n){return new t(this.x/n,this.y/n)},t.idiv=function(t,n){return n.idiv(t)},t.prototype.idiv=function(t){return this.x/=t,this.y/=t,this},t.lerp=function(t,n,i){return t.lerp(n,i)},t.prototype.lerp=function(n,i){var r=(1-i)*this.x+i*n.x,e=(1-i)*this.y+i*n.y;return new t(r,e)},t.normalized=function(t){return t.normalized()},t.prototype.normalized=function(){var n=this.x,i=this.y,r=Math.sqrt(n*n+i*i);return r>0?new t(n/r,i/r):new t(0,0)},t.normalize=function(t){return t.normalize()},t.prototype.normalize=function(){var t=this.x,n=this.y,i=Math.sqrt(t*t+n*n);return i>0&&(this.x=t/i,this.y=n/i),this},t.magnitude=function(t){return t.magnitude()},t.prototype.magnitude=function(){return Math.sqrt(this.x*this.x+this.y*this.y)},t.dot=function(t,n){return n.dot(t)},t.prototype.dot=function(t){return this.x*t.x+this.y*t.y},t.distance=function(t,n){return n.distance(t)},t.prototype.distance=function(t){var n=this.x-t.x,i=this.y-t.y;return Math.sqrt(n*n+i*i)},t.angleOf=function(t){return t.angleOf()},t.prototype.angleOf=function(){return Math.atan2(this.y,this.x)},t.angleTo=function(t,n){return n.angleTo(t)},t.prototype.angleTo=function(t){return Math.acos(this.dot(t)/this.magnitude()*t.magnitude())},t.reset=function(t,n){return n.reset(t)},t.prototype.reset=function(t){return this.x=t.x,this.y=t.y,this},t.zero=function(t){return t.zero()},t.prototype.zero=function(){return this.x=0,this.y=0,this},t.set=function(t,n,i){return i.set(t,n)},t.prototype.set=function(t,n){return this.x=t||0,this.y=n||0,this},t.copy=function(t){return t.copy()},t.prototype.copy=function(){return new t(this.x,this.y)},t.toJSON=function(t){return t.toJSON()},t.prototype.toJSON=function(){return[this.x,this.y]},t.toString=function(t){return t?t.toString():Function.prototype.toString.call(this)},t.prototype.toString=function(){return this.x.toFixed(3)+" "+this.y.toFixed(3)},"undefined"!=typeof Symbol&&Symbol.toStringTag&&(t.prototype[Symbol.toStringTag]="Vector"),t.toArray=function(t){return t.toArray()},t.prototype.toArray=function(){return[this.x,this.y]},t.equals=function(t,n){return t.equals(n)},t.prototype.equals=function(t){return this.x===t.x&&this.y===t.y},t.compare=function(t,n){return t.compare(n)},t.prototype.compare=function(t){var n=this.magnitude(),i=t.magnitude();return(n>i)-(i>n)},Object.defineProperties(t.prototype,{xx:{configurable:!0,get:function(){return new t(this.x,this.x)},set:function(t){this.x=t.x,this.y=t.x}},xy:{configurable:!0,get:function(){return new t(this.x,this.y)},set:function(t){this.x=t.x,this.y=t.y}},yx:{configurable:!0,get:function(){return new t(this.y,this.x)},set:function(t){this.x=t.y,this.y=t.x}},yy:{configurable:!0,get:function(){return new t(this.y,this.y)},set:function(t){this.x=t.y,this.y=t.y}}}),n.prototype.next=function(){return 0===this.__idx?(this.__idx++,{done:!1,value:this.vector.x}):1===this.__idx?(this.__idx++,{done:!1,value:this.vector.y}):{done:!0,value:void 0}},"undefined"!=typeof Symbol&&Symbol.iterator&&(t.prototype[Symbol.iterator]=function(){return new n(this)}),t});