UNPKG

@animech-public/playcanvas

Version:
2 lines (1 loc) 2.59 kB
import{math as t}from"./math.js";var s;class h{constructor(t=0,s=0){this.x=void 0,this.y=void 0,2===t.length?(this.x=t[0],this.y=t[1]):(this.x=t,this.y=s)}add(t){return this.x+=t.x,this.y+=t.y,this}add2(t,s){return this.x=t.x+s.x,this.y=t.y+s.y,this}addScalar(t){return this.x+=t,this.y+=t,this}addScaled(t,s){return this.x+=t.x*s,this.y+=t.y*s,this}clone(){return new(0,this.constructor)(this.x,this.y)}copy(t){return this.x=t.x,this.y=t.y,this}cross(t){return this.x*t.y-this.y*t.x}distance(t){const s=this.x-t.x,h=this.y-t.y;return Math.sqrt(s*s+h*h)}div(t){return this.x/=t.x,this.y/=t.y,this}div2(t,s){return this.x=t.x/s.x,this.y=t.y/s.y,this}divScalar(t){return this.x/=t,this.y/=t,this}dot(t){return this.x*t.x+this.y*t.y}equals(t){return this.x===t.x&&this.y===t.y}equalsApprox(t,s=1e-6){return Math.abs(this.x-t.x)<s&&Math.abs(this.y-t.y)<s}length(){return Math.sqrt(this.x*this.x+this.y*this.y)}lengthSq(){return this.x*this.x+this.y*this.y}lerp(t,s,h){return this.x=t.x+h*(s.x-t.x),this.y=t.y+h*(s.y-t.y),this}mul(t){return this.x*=t.x,this.y*=t.y,this}mul2(t,s){return this.x=t.x*s.x,this.y=t.y*s.y,this}mulScalar(t){return this.x*=t,this.y*=t,this}normalize(t=this){const s=t.x*t.x+t.y*t.y;if(s>0){const h=1/Math.sqrt(s);this.x=t.x*h,this.y=t.y*h}return this}rotate(s){const h=Math.atan2(this.x,this.y)+s*t.DEG_TO_RAD,i=Math.sqrt(this.x*this.x+this.y*this.y);return this.x=Math.sin(h)*i,this.y=Math.cos(h)*i,this}angle(){return Math.atan2(this.x,this.y)*t.RAD_TO_DEG}angleTo(s){return Math.atan2(this.x*s.y+this.y*s.x,this.x*s.x+this.y*s.y)*t.RAD_TO_DEG}floor(t=this){return this.x=Math.floor(t.x),this.y=Math.floor(t.y),this}ceil(t=this){return this.x=Math.ceil(t.x),this.y=Math.ceil(t.y),this}round(t=this){return this.x=Math.round(t.x),this.y=Math.round(t.y),this}min(t){return t.x<this.x&&(this.x=t.x),t.y<this.y&&(this.y=t.y),this}max(t){return t.x>this.x&&(this.x=t.x),t.y>this.y&&(this.y=t.y),this}set(t,s){return this.x=t,this.y=s,this}sub(t){return this.x-=t.x,this.y-=t.y,this}sub2(t,s){return this.x=t.x-s.x,this.y=t.y-s.y,this}subScalar(t){return this.x-=t,this.y-=t,this}fromArray(t,s=0){var h,i;return this.x=null!=(h=t[s])?h:this.x,this.y=null!=(i=t[s+1])?i:this.y,this}toString(){return`[${this.x}, ${this.y}]`}toArray(t=[],s=0){return t[s]=this.x,t[s+1]=this.y,t}static angleRad(t,s){return Math.atan2(t.x*s.y-t.y*s.x,t.x*s.x+t.y*s.y)}}s=h,h.ZERO=Object.freeze(new s(0,0)),h.ONE=Object.freeze(new s(1,1)),h.UP=Object.freeze(new s(0,1)),h.DOWN=Object.freeze(new s(0,-1)),h.RIGHT=Object.freeze(new s(1,0)),h.LEFT=Object.freeze(new s(-1,0));export{h as Vec2};