@equinor/videx-vector2
Version:
Vector2 class library written in javascript.
2 lines (1 loc) • 5.76 kB
JavaScript
!function(global,factory){"object"==typeof exports&&"undefined"!=typeof module?module.exports=factory():"function"==typeof define&&define.amd?define(factory):(global="undefined"!=typeof globalThis?globalThis:global||self)["videx-vector2"]=factory()}(this,(function(){"use strict";function add(a,b,target=a){for(let i=0;i<a.length;i++)target[i]=a[i]+b[i];return target}function sub(a,b,target=a){for(let i=0;i<a.length;i++)target[i]=a[i]-b[i];return target}function scale(a,factor,target=a){for(let i=0;i<a.length;i++)target[i]=a[i]*factor;return target}function magnitude(a){const sq=function(a){let sum=0;for(let i=0;i<a.length;i++)sum+=a[i]**2;return sum}(a);return 0===sq?sq:Math.sqrt(sq)}function normalize(a,target=a){const len=magnitude(a);return 0===len?function(value,target){for(let i=0;i<target.length;i++)target[i]=value;return target}(0,target):scale(a,1/len,target)}function isZeroVector(a,epsilon=0){void 0===epsilon&&(epsilon=0);for(let i=0;i<a.length;i++)if(Math.abs(a[i])>epsilon)return!1;return!0}const RAD2DEG=180/Math.PI,DEG2RAD=Math.PI/180;function rotate(v,rad,target){const cr=Math.cos(rad),sr=Math.sin(rad),x=v[0];return target[0]=cr*x-sr*v[1],target[1]=sr*x+cr*v[1],target}function angleRight(v){return Math.atan2(v[1],v[0])}function signedAngle(a,b){let phi=Math.atan2(b[1],b[0])-Math.atan2(a[1],a[0]);return phi>Math.PI?phi-=2*Math.PI:phi<=-Math.PI&&(phi+=2*Math.PI),phi}class Vector2{constructor(a,...b){this.isMutating=!1,this.length=2,"number"==typeof a?"number"==typeof b[0]?(this[0]=a,this[1]=b[0]):(this[0]=a,this[1]=a):"x"in a&&"y"in a?(this[0]=a.x,this[1]=a.y):(this[0]=a[0],this[1]=a[1])}get x(){return this[0]}set x(value){this[0]=value}get y(){return this[1]}set y(value){this[1]=value}get magnitude(){return magnitude(this)}set magnitude(val){scale(this,val/magnitude(this),this)}get mutable(){return this.isMutating=!0,this}get immutable(){return this.isMutating=!1,this}set(a,b){return"number"==typeof a?"number"==typeof b?(this[0]=a,this[1]=b):(this[0]=a,this[1]=a):(this[0]=a[0],this[1]=a[1]),this}add(a,b=0){return"number"==typeof a?this.isMutating?add(this,[a,b]):add(this.clone(),[a,b]):this.isMutating?add(this,a):add(this.clone(),a)}static add(a,b){return add(new Vector2(a),b)}sub(a,b=0){return"number"==typeof a?this.isMutating?sub(this,[a,b]):sub(this.clone(),[a,b]):this.isMutating?sub(this,a):sub(this.clone(),a)}static sub(a,b){return sub(new Vector2(a),b)}subFrom(a,b=0){return"number"==typeof a?this.isMutating?(this[0]=a-this[0],this[1]=b-this[1],this):new Vector2(a-this[0],b-this[1]):this.isMutating?(this[0]=a[0]-this[0],this[1]=a[1]-this[1],this):new Vector2(a[0]-this[0],a[1]-this[1])}static divide(v,n){return scale(new Vector2(v),1/n)}static multiply(v,n){return scale(new Vector2(v),n)}scale(n){return scale(this,n,this.isMutating?this:Vector2.zero)}rescale(n){const len=magnitude(this);return len<=0?this.isMutating?this:Vector2.zero:scale(this,n/len,this.isMutating?this:Vector2.zero)}clampMagnitude(n){const len=magnitude(this);return len>n?scale(this,n/len,this.isMutating?this:Vector2.zero):this.isMutating?this:this.clone()}rotate(rad){return rotate(this,rad,this.isMutating?this:Vector2.zero)}rotateDeg(deg){return rotate(this,deg*DEG2RAD,this.isMutating?this:Vector2.zero)}rotate90(){return function(v,target){const x=v[0];return target[0]=-v[1],target[1]=x,target}(this,this.isMutating?this:Vector2.zero)}rotate180(){return v=this,(target=this.isMutating?this:Vector2.zero)[0]=-v[0],target[1]=-v[1],target;var v,target}rotate270(){return function(v,target){const x=v[0];return target[0]=v[1],target[1]=-x,target}(this,this.isMutating?this:Vector2.zero)}normalize(){return normalize(this)}normalized(){return normalize(this,Vector2.zero)}static distance(a,b){return function(a,b){let sq=0;for(let i=0;i<a.length;i++)sq+=(b[i]-a[i])**2;return 0===sq?sq:Math.sqrt(sq)}(a,b)}static dot(a,b){return function(a,b){let sum=0;for(let i=0;i<a.length;i++)sum+=a[i]*b[i];return sum}(a,b)}static cross(a,b){return function(a,b){return a[0]*b[1]-a[1]*b[0]}(a,b)}static angleRight(v){return angleRight(v)}static angleRightDeg(v){return angleRight(v)*RAD2DEG}static angle(a,b){return Math.abs(signedAngle(a,b))}static angleDeg(a,b){return Math.abs(signedAngle(a,b))*RAD2DEG}static signedAngle(a,b){return signedAngle(a,b)}static signedAngleDeg(a,b){return signedAngle(a,b)*RAD2DEG}static lerp(a,b,t){return function(a,b,t,target=a){const n=function(value,min=0,max=1){return value<min?min:value>max?max:value}(t,0,1),m=1-n;for(let i=0;i<a.length;i++)target[i]=a[i]*m+b[i]*n;return target}(new Vector2(a),b,t)}static lerpRot(a,b,t){return function(a,b,n,target=a){return rotate(a,n*signedAngle(a,b),target)}(a,b,t,Vector2.zero)}clone(){return new Vector2(this[0],this[1])}static equals(a,b,epsilon=0){return!(Math.abs(a[0]-b[0])>epsilon)&&!(Math.abs(a[1]-b[1])>epsilon)}equals(vector,epsilon=0){return Vector2.equals(this,vector,epsilon)}static isZeroVector(vector,epsilon=0){return isZeroVector(vector,epsilon)}isZeroVector(epsilon=0){return isZeroVector(this,epsilon)}toArray(){return[this[0],this[1]]}modify(modifier){return function(a,modifier,target=a){for(let i=0;i<a.length;i++)target[i]=modifier(a[i],i);return target}(this,modifier)}[Symbol.iterator](){let i=0;return{next:()=>{switch(i++){case 0:return{value:this[0],done:!1};case 1:return{value:this[1],done:!1};default:return{value:-1,done:!0}}}}}static get zero(){return new Vector2(0,0)}static get one(){return new Vector2(1,1)}static get positiveInfinity(){return new Vector2(1/0,1/0)}static get negativeInfinity(){return new Vector2(-1/0,-1/0)}static get up(){return new Vector2(0,1)}static get right(){return new Vector2(1,0)}static get down(){return new Vector2(0,-1)}static get left(){return new Vector2(-1,0)}}return Vector2}));