UNPKG

@equinor/videx-vector2

Version:

Vector2 class library written in javascript.

2 lines (1 loc) 5.03 kB
"use strict";var videxLinearAlgebra=require("@equinor/videx-linear-algebra");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 videxLinearAlgebra.magnitude(this)}set magnitude(val){const len=videxLinearAlgebra.magnitude(this);videxLinearAlgebra.scale(this,val/len,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?videxLinearAlgebra.add(this,[a,b]):videxLinearAlgebra.add(this.clone(),[a,b]):this.isMutating?videxLinearAlgebra.add(this,a):videxLinearAlgebra.add(this.clone(),a)}static add(a,b){const output=new Vector2(a);return videxLinearAlgebra.add(output,b)}sub(a,b=0){return"number"==typeof a?this.isMutating?videxLinearAlgebra.sub(this,[a,b]):videxLinearAlgebra.sub(this.clone(),[a,b]):this.isMutating?videxLinearAlgebra.sub(this,a):videxLinearAlgebra.sub(this.clone(),a)}static sub(a,b){const output=new Vector2(a);return videxLinearAlgebra.sub(output,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){const output=new Vector2(v);return videxLinearAlgebra.scale(output,1/n)}static multiply(v,n){const output=new Vector2(v);return videxLinearAlgebra.scale(output,n)}scale(n){return videxLinearAlgebra.scale(this,n,this.isMutating?this:Vector2.zero)}rescale(n){const len=videxLinearAlgebra.magnitude(this);return len<=0?this.isMutating?this:Vector2.zero:videxLinearAlgebra.scale(this,n/len,this.isMutating?this:Vector2.zero)}clampMagnitude(n){const len=videxLinearAlgebra.magnitude(this);return len>n?videxLinearAlgebra.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 videxLinearAlgebra.normalize(this)}normalized(){return videxLinearAlgebra.normalize(this,Vector2.zero)}static distance(a,b){return videxLinearAlgebra.dist(a,b)}static dot(a,b){return videxLinearAlgebra.dot(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){const output=new Vector2(a);return videxLinearAlgebra.mix(output,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 videxLinearAlgebra.isZeroVector(vector,epsilon)}isZeroVector(epsilon=0){return videxLinearAlgebra.isZeroVector(this,epsilon)}toArray(){return[this[0],this[1]]}modify(modifier){return videxLinearAlgebra.modify(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)}}module.exports=Vector2;