UNPKG

@nekobird/vector2

Version:
3 lines (2 loc) 13.1 kB
"use strict";function t(e){return(t="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(e)}function e(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}function n(t){return function(t){if(Array.isArray(t)){for(var e=0,n=new Array(t.length);e<t.length;e++)n[e]=t[e];return n}}(t)||function(t){if(Symbol.iterator in Object(t)||"[object Arguments]"===Object.prototype.toString.call(t))return Array.from(t)}(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance")}()}function r(e){return"object"===t(e)&&"number"==typeof e.x&&"number"==typeof e.y}function i(t,e){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){if(!(Symbol.iterator in Object(t)||"[object Arguments]"===Object.prototype.toString.call(t)))return;var n=[],r=!0,i=!1,a=void 0;try{for(var u,o=t[Symbol.iterator]();!(r=(u=o.next()).done)&&(n.push(u.value),!e||n.length!==e);r=!0);}catch(t){i=!0,a=t}finally{try{r||null==o.return||o.return()}finally{if(i)throw a}}return n}(t,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}()}function a(t){return function(t){if(Array.isArray(t)){for(var e=0,n=new Array(t.length);e<t.length;e++)n[e]=t[e];return n}}(t)||function(t){if(Symbol.iterator in Object(t)||"[object Arguments]"===Object.prototype.toString.call(t))return Array.from(t)}(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance")}()}function u(t){return"number"==typeof t&&!isNaN(t)}function o(t,e,n){var r,a;if(u(e)&&u(n))r=v([e,n]);else{if(!u(a=e)&&!function(t){return Array.isArray(t)&&2===t.length&&t.every(u)}(a)||void 0!==n)return t;r=f(e)}var o=i(v(r),2),h=o[0],s=o[1];return Math.max(h,Math.min(t,s))}function h(t,e,n,r,i){return Math.pow(1-t,3)*e+3*t*Math.pow(1-t,2)*n+3*t*t*(1-t)*r+t*t*t*i}function s(t,e){var n=i(v(f(e)),2),r=n[0],a=n[1];if(0===r&&0===a)return 0;var u=y(r,a);if(t>a){var o=y(t,a)%u+r;return o===r?a:o}if(t<r){var h=a-y(t,r)%u;return h===a?r:h}return t}function y(t,e){return t===e?0:Math.sqrt(Math.abs((t-e)*(e-t)))}function l(t,e){if(Math.hypot)return Math.hypot(t,e);var n=Math.max(Math.abs(t),Math.abs(e));0===n&&(n=1);var r=Math.min(Math.abs(t),Math.abs(e))/n;return n*Math.sqrt(1+r*r)}function c(t,e,n){var r=!(arguments.length>3&&void 0!==arguments[3])||arguments[3],i=f(e),a=f(n),u=(t-i[0])*((a[1]-a[0])/(i[1]-i[0]))+a[0];return r?o(u,a):u}function f(t){return u(t)?[0,t]:[t[0],t[1]]}function v(t){return[Math.min.apply(Math,a(t)),Math.max.apply(Math,a(t))]}function x(t,e){var n=0;return t>e?n=2*Math.PI-t+e:e>t&&(n=e-t),n}function m(t,e){var n=0;return t>e?n=t-e:e>t&&(n=t+2*Math.PI-e),n}Object.defineProperty(exports,"__esModule",{value:!0});var p=function(){function i(t,e){return function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,i),this.x=0,this.y=0,u(t)&&u(e)?(this.x=t,this.y=e):u(t)&&!u(e)?(this.x=t,this.y=t):r(t)&&(this.x=t.x,this.y=t.y),this}var a,y,f;return a=i,f=[{key:"clone",value:function(t){return new i(t.x,t.y)}},{key:"unit",value:function(t){return i.clone(t).normalize()}},{key:"add",value:function(t,e){return i.clone(t).add(e)}},{key:"subtract",value:function(t,e){return i.clone(t).subtract(e)}},{key:"multiply",value:function(t,e){return i.clone(t).multiply(e)}},{key:"divide",value:function(t,e){return i.clone(t).divide(e)}},{key:"projectFrom",value:function(t,e,n){var r=i.clone(e).normalize().multiply(n);return i.add(t,r)}},{key:"splitAtAngle",value:function(t,e,n){var r=[];return r[0]=i.clone(t).moveRadiallyBy(e,-n),r[1]=i.clone(t).moveRadiallyBy(e,n),r}},{key:"getMidPointBetween",value:function(t,e){var n=t.x-e.x,r=t.y-e.y;return n/=2,r/=2,new i(n+=e.x,r+=e.y)}},{key:"getDistanceBetween",value:function(t,e){return i.subtract(t,e).magnitude}},{key:"getLengthBetween",value:function(t,e){return i.subtract(t,e).magnitude}},{key:"scaleByFrom",value:function(t,e,n){return i.clone(t).scaleByFrom(e,n)}},{key:"getDisplacement",value:function(t,e){return i.subtract(e,t)}},{key:"getDirection",value:function(t,e){return i.subtract(e,t).normalize()}},{key:"perpendicular",value:function(t){return t.clone().rotateBy(Math.PI/2)}},{key:"angleIsInProximity",value:function(t,e,n){var r=x(t,e),i=m(t,e);return Math.min(r,i)<=n}},{key:"getAngleBetween2Points",value:function(t,e){var n=i.clone(t).angle,r=i.clone(e).angle,a=x(n,r),u=m(n,r);return Math.min(a,u)}},{key:"getAngleBetween3Points",value:function(t,e,n){var r=i.clone(t),a=i.clone(e),u=i.clone(n),o=a.getAngleTo(r),h=a.getAngleTo(u),s=x(o,h),y=m(o,h);return Math.min(s,y)}},{key:"getBasePointOfTriangle",value:function(t,e,n){var r=t.getAngleTo(n),a=t.getAngleTo(e),u=Math.abs(r-a),o=t.getDistanceTo(e),h=Math.sin(u)*o,s=Math.atan(u)/h;return i.clone(t).moveRadiallyBy(r,s)}},{key:"getCenterVector",value:function(){var t=this.minX.apply(this,arguments),e=this.maxX.apply(this,arguments),n=this.minY.apply(this,arguments),r=this.maxY.apply(this,arguments);return new i(t+(e-t)/2,n+(r-n)/2)}},{key:"one",value:function(){return new i(1,1)}},{key:"up",value:function(){return new i(0,-1)}},{key:"down",value:function(){return new i(0,1)}},{key:"left",value:function(){return new i(-1,0)}},{key:"right",value:function(){return new i(0,1)}},{key:"min",value:function(){for(var t=arguments.length,e=new Array(t),r=0;r<t;r++)e[r]=arguments[r];var i=e.map((function(t){return t.magnitude})),a=i.indexOf(Math.min.apply(Math,n(i)));return e[a]}},{key:"max",value:function(){for(var t=arguments.length,e=new Array(t),r=0;r<t;r++)e[r]=arguments[r];var i=e.map((function(t){return t.magnitude})),a=i.indexOf(Math.max.apply(Math,n(i)));return e[a]}},{key:"minX",value:function(){for(var t=arguments.length,e=new Array(t),r=0;r<t;r++)e[r]=arguments[r];var i=e.map((function(t){return t.x})),a=i.indexOf(Math.min.apply(Math,n(i)));return i[a]}},{key:"minY",value:function(){for(var t=arguments.length,e=new Array(t),r=0;r<t;r++)e[r]=arguments[r];var i=e.map((function(t){return t.y})),a=i.indexOf(Math.min.apply(Math,n(i)));return i[a]}},{key:"maxX",value:function(){for(var t=arguments.length,e=new Array(t),r=0;r<t;r++)e[r]=arguments[r];var i=e.map((function(t){return t.x})),a=i.indexOf(Math.max.apply(Math,n(i)));return i[a]}},{key:"maxY",value:function(){for(var t=arguments.length,e=new Array(t),r=0;r<t;r++)e[r]=arguments[r];var i=e.map((function(t){return t.y})),a=i.indexOf(Math.max.apply(Math,n(i)));return i[a]}},{key:"zero",get:function(){return new i(0,0)}},{key:"random",get:function(){return new i(Math.random(),Math.random())}}],(y=[{key:"equals",value:function(t,e){return u(t)&&u(e)?(this.x=t,this.y=e):u(t)&&!u(e)?(this.x=t,this.y=t):r(t)&&(this.x=t.x,this.y=t.y),this}},{key:"isEqual",value:function(){for(var t=this,e=arguments.length,n=new Array(e),r=0;r<e;r++)n[r]=arguments[r];return n.every((function(e){return t.x===e.x&&t.y===e.y}))}},{key:"negative",value:function(){return this.x=-this.x,this.y=-this.y,this}},{key:"absolute",value:function(){return this.x=Math.abs(this.x),this.y=Math.abs(this.y),this}},{key:"clamp",value:function(t){return this.x=o(this.x,t),this.y=o(this.y,t),this}},{key:"limit",value:function(t){return this.magnitude>t&&this.normalize().multiply(t),this}},{key:"normalize",value:function(){var t=Math.abs(this.magnitude);return 0===t&&(t=1),this.x/=t,this.y/=t,this}},{key:"dot",value:function(t){return this.x*t.x+this.y*t.y}},{key:"round",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0;return this.x=parseFloat(this.x.toFixed(t)),this.y=parseFloat(this.y.toFixed(t)),this}},{key:"zero",value:function(){return this.x=this.y=0,this}},{key:"clone",value:function(){return i.clone(this)}},{key:"add",value:function(t,e){return r(t)?(this.x+=t.x,this.y+=t.y):u(t)&&u(e)?(this.x+=t,this.y+=e):u(t)&&!u(e)&&(this.x+=t,this.y+=t),this}},{key:"addX",value:function(e){return u(e)?this.x+=e:"object"===t(e)&&u(e.x)&&(this.x+=e.x),this}},{key:"addY",value:function(e){return u(e)?this.y+=e:"object"===t(e)&&u(e.y)&&(this.y+=e.y),this}},{key:"subtract",value:function(t,e){return r(t)?(this.x-=t.x,this.y-=t.y):u(t)&&!u(e)?(this.x-=t,this.y-=t):u(t)&&u(e)&&(this.x-=t,this.y-=e),this}},{key:"subtractX",value:function(e){return u(e)?this.x-=e:"object"!==t(e)||u(e.x)||(this.x-=e.x),this}},{key:"subtractY",value:function(e){return u(e)?this.y-=e:"object"!==t(e)||u(e.y)||(this.y-=e.y),this}},{key:"multiply",value:function(t,e){return r(t)?(this.x*=t.x,this.y*=t.y):u(t)&&!u(e)?(this.x*=t,this.y*=t):u(t)&&u(e)&&(this.x*=t,this.y*=e),this}},{key:"multiplyX",value:function(t){return u(t)?this.x*=t:r(t)&&(this.x*=t.x),this}},{key:"multiplyY",value:function(t){return u(t)?this.y*=t:r(t)&&(this.y*=t.y),this}},{key:"divide",value:function(t,e){if(u(t)&&!u(e)){if(0===t)throw new Error("Vector2: Division by zero.");this.x/=t,this.y/=t}else if(u(t)&&u(e)){if(0===t||0===e)throw new Error("Vector2: Division by zero.");this.x/=t,this.y/=e}else if(r(t)){if(0===t.x||0===t.y)throw new Error("Vector2: Division by zero.");this.x/=t.x,this.y/=t.y}return this}},{key:"divideX",value:function(t){if(u(t)){if(0===t)throw new Error("Vector2: Division by zero.");this.x/=t}else if(r(t)){if(0===t.x)throw new Error("Vector2: Division by zero.");this.x/=t.x}return this}},{key:"divideY",value:function(t){if(u(t)){if(0===t)throw new Error("Vector2: Division by zero.");this.y/=t}else if(r(t)){if(0===t.y)throw new Error("Vector2: Division by zero.");this.y/=t.y}return this}},{key:"getDistanceTo",value:function(t){return i.subtract(this,t).magnitude}},{key:"getAngleFrom",value:function(t){var e=this.x-t.x,n=this.y-t.y,r=l(e,n),i=Math.acos(e/r);return n<0?Math.PI+(Math.PI-i):i}},{key:"getAngleTo",value:function(t){var e=t.x-this.x,n=t.y-this.y,r=l(e,n),i=Math.acos(e/r);return n<0?Math.PI+(Math.PI-i):i}},{key:"moveBy",value:function(t,e){return u(t)&&u(e)?(this.x+=t,this.y+=e):r(t)&&this.add(t),this}},{key:"moveTo",value:function(t,e){return u(t)&&u(e)?(this.x=t,this.y=e):r(t)&&this.equals(t),this}},{key:"polarTranslate",value:function(t,e){return t=s(t,2*Math.PI),this.x+=Math.cos(t)*e,this.y+=Math.sin(t)*e,this}},{key:"polarTranslateBy",value:function(t,e){var n=s(this.angle+t,2*Math.PI);return this.x+=Math.cos(n)*e,this.y+=Math.sin(n)*e,this}},{key:"moveRadiallyBy",value:function(t,e){return t=s(t,2*Math.PI),this.x+=Math.cos(t)*e,this.y+=Math.sin(t)*e,this}},{key:"moveRadiallyTo",value:function(t,e){return t=s(t,2*Math.PI),this.x=Math.cos(t)*e,this.y=Math.sin(t)*e,this}},{key:"rotateBy",value:function(t){var e=this.angle+t;return this.x=Math.cos(e)*this.magnitude,this.y=Math.sin(e)*this.magnitude,this}},{key:"rotateTo",value:function(t){return t=s(t,2*Math.PI),this.x=Math.cos(t)*this.magnitude,this.y=Math.sin(t)*this.magnitude,this}},{key:"rotateByFrom",value:function(t,e){t=s(t,2*Math.PI);var n=this.x-e.x,r=this.y-e.y,i=l(n,r),a=Math.acos(n/i);r<0&&(a=Math.PI+(Math.PI-a));var u=s(a+t,2*Math.PI);return this.x=e.x+Math.cos(u)*i,this.y=e.y+Math.sin(u)*i,this}},{key:"rotateToFrom",value:function(t,e){t=s(t,2*Math.PI);var n=l(this.x-e.x,this.y-e.y);return this.x=e.x+Math.cos(t)*n,this.y=e.y+Math.sin(t)*n,this}},{key:"scaleBy",value:function(t){var e=this.magnitude;return 0===e&&(e=1),this.x/=e,this.y/=e,this.x*=t,this.y*=t,this}},{key:"scaleByFrom",value:function(t,e){var n=i.subtract(this,e),r=n.magnitude;return n.normalize().multiply(r*t).add(e),this.equals(n),this}},{key:"applyLerp",value:function(t,e){return this.x=c(t,1,[this.x,e.x],!1),this.y=c(t,1,[this.y,e.y],!1),this}},{key:"applyCubicBezier",value:function(t,e,n,r,i){return this.x=h(t,e.x,n.x,r.x,i.x),this.y=h(t,e.y,n.y,r.y,i.y),this}},{key:"magnitude",get:function(){return l(this.x,this.y)},set:function(t){this.normalize().multiply(t)}},{key:"isZero",get:function(){return 0===this.x&&0===this.y}},{key:"array",get:function(){return[this.x,this.y]}},{key:"string",get:function(){return"x: ".concat(this.x,", y: ").concat(this.y)}},{key:"average",get:function(){return(Math.abs(this.x)+Math.abs(this.y))/2}},{key:"angle",get:function(){var t=Math.acos(this.x/this.magnitude);return this.y<0?Math.PI+(Math.PI-t):t}}])&&e(a.prototype,y),f&&e(a,f),i}();exports.Vector2=p,exports.deltaClockwise=function(t,e){var n=arguments.length>2&&void 0!==arguments[2]&&arguments[2],r=1,i=t-Math.PI;if(i<0){var a=s(i,2*Math.PI);(e<t||e>=a)&&(r=-1)}else e<t&&e>=i&&(r=-1);!1===n&&(r=1);var u=0;return t>e?u=t-e:e>t&&(u=e-t),u*r},exports.deltaCounterclockwise=function(t,e){var n=arguments.length>2&&void 0!==arguments[2]&&arguments[2],r=1,i=t+Math.PI;if(i>2*Math.PI){var a=s(i,2*Math.PI);(e>t||e<=a)&&(r=-1)}else e>t&&e<=i&&(r=-1);!1===n&&(r=1);var u=0;return t>e?u=t-e:e>t&&(u=e-t),u*r},exports.differenceClockwise=x,exports.differenceCounterclockwise=m,exports.isPointLike=r,exports.toDegrees=function(t){return t*(180/Math.PI)},exports.toRadians=function(t){return t*(Math.PI/180)}; //# sourceMappingURL=vector2.js.map