UNPKG

kld-affine

Version:

A collection of classes used in affine geometry

3 lines (2 loc) 8.71 kB
function t(t,i){if(!(t instanceof i))throw new TypeError("Cannot call a class as a function")}function i(t,i){for(var n=0;n<i.length;n++){var s=i[n];s.enumerable=s.enumerable||!1,s.configurable=!0,"value"in s&&(s.writable=!0),Object.defineProperty(t,s.key,s)}}function n(t,n,s){return n&&i(t.prototype,n),s&&i(t,s),t}var s=function(){function i(){var n=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0,s=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;t(this,i),this.x=n,this.y=s}return n(i,[{key:"clone",value:function(){return new this.constructor(this.x,this.y)}},{key:"add",value:function(t){return new this.constructor(this.x+t.x,this.y+t.y)}},{key:"subtract",value:function(t){return new this.constructor(this.x-t.x,this.y-t.y)}},{key:"multiply",value:function(t){return new this.constructor(this.x*t,this.y*t)}},{key:"divide",value:function(t){return new this.constructor(this.x/t,this.y/t)}},{key:"equals",value:function(t){return this.x===t.x&&this.y===t.y}},{key:"precisionEquals",value:function(t,i){return Math.abs(this.x-t.x)<i&&Math.abs(this.y-t.y)<i}},{key:"lerp",value:function(t,i){var n=1-i;return new this.constructor(this.x*n+t.x*i,this.y*n+t.y*i)}},{key:"distanceFrom",value:function(t){var i=this.x-t.x,n=this.y-t.y;return Math.sqrt(i*i+n*n)}},{key:"min",value:function(t){return new this.constructor(Math.min(this.x,t.x),Math.min(this.y,t.y))}},{key:"max",value:function(t){return new this.constructor(Math.max(this.x,t.x),Math.max(this.y,t.y))}},{key:"transform",value:function(t){return new this.constructor(t.a*this.x+t.c*this.y+t.e,t.b*this.x+t.d*this.y+t.f)}},{key:"toString",value:function(){return"point(".concat(this.x,",").concat(this.y,")")}}]),i}(),e=function(){function i(){var n=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0,s=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;t(this,i),this.x=n,this.y=s}return n(i,[{key:"length",value:function(){return Math.sqrt(this.x*this.x+this.y*this.y)}},{key:"magnitude",value:function(){return this.x*this.x+this.y*this.y}},{key:"dot",value:function(t){return this.x*t.x+this.y*t.y}},{key:"cross",value:function(t){return this.x*t.y-this.y*t.x}},{key:"determinant",value:function(t){return this.x*t.y-this.y*t.x}},{key:"unit",value:function(){return this.divide(this.length())}},{key:"add",value:function(t){return new this.constructor(this.x+t.x,this.y+t.y)}},{key:"subtract",value:function(t){return new this.constructor(this.x-t.x,this.y-t.y)}},{key:"multiply",value:function(t){return new this.constructor(this.x*t,this.y*t)}},{key:"divide",value:function(t){return new this.constructor(this.x/t,this.y/t)}},{key:"angleBetween",value:function(t){var i=this.dot(t)/(this.length()*t.length());i=Math.max(-1,Math.min(i,1));var n=Math.acos(i);return this.cross(t)<0?-n:n}},{key:"perp",value:function(){return new this.constructor(-this.y,this.x)}},{key:"perpendicular",value:function(t){return this.subtract(this.project(t))}},{key:"project",value:function(t){var i=this.dot(t)/t.dot(t);return t.multiply(i)}},{key:"transform",value:function(t){return new this.constructor(t.a*this.x+t.c*this.y,t.b*this.x+t.d*this.y)}},{key:"equals",value:function(t){return this.x===t.x&&this.y===t.y}},{key:"precisionEquals",value:function(t,i){return Math.abs(this.x-t.x)<i&&Math.abs(this.y-t.y)<i}},{key:"toString",value:function(){return"vector(".concat(this.x,",").concat(this.y,")")}}],[{key:"fromPoints",value:function(t,n){return new i(n.x-t.x,n.y-t.y)}}]),i}(),h=function(){function i(){var n=arguments.length>0&&void 0!==arguments[0]?arguments[0]:1,s=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,e=arguments.length>2&&void 0!==arguments[2]?arguments[2]:0,h=arguments.length>3&&void 0!==arguments[3]?arguments[3]:1,r=arguments.length>4&&void 0!==arguments[4]?arguments[4]:0,u=arguments.length>5&&void 0!==arguments[5]?arguments[5]:0;t(this,i),this.a=n,this.b=s,this.c=e,this.d=h,this.e=r,this.f=u}return n(i,[{key:"multiply",value:function(t){return this.isIdentity()?t:t.isIdentity()?this:new this.constructor(this.a*t.a+this.c*t.b,this.b*t.a+this.d*t.b,this.a*t.c+this.c*t.d,this.b*t.c+this.d*t.d,this.a*t.e+this.c*t.f+this.e,this.b*t.e+this.d*t.f+this.f)}},{key:"inverse",value:function(){if(this.isIdentity())return this;var t=this.a*this.d-this.b*this.c;if(0===t)throw new Error("Matrix is not invertible");var i=1/t,n=this.f*this.c-this.e*this.d,s=this.e*this.b-this.f*this.a;return new this.constructor(this.d*i,-this.b*i,-this.c*i,this.a*i,n*i,s*i)}},{key:"translate",value:function(t,i){return new this.constructor(this.a,this.b,this.c,this.d,this.a*t+this.c*i+this.e,this.b*t+this.d*i+this.f)}},{key:"scale",value:function(t){return new this.constructor(this.a*t,this.b*t,this.c*t,this.d*t,this.e,this.f)}},{key:"scaleAt",value:function(t,i){var n=i.x-t*i.x,s=i.y-t*i.y;return new this.constructor(this.a*t,this.b*t,this.c*t,this.d*t,this.a*n+this.c*s+this.e,this.b*n+this.d*s+this.f)}},{key:"scaleNonUniform",value:function(t,i){return new this.constructor(this.a*t,this.b*t,this.c*i,this.d*i,this.e,this.f)}},{key:"scaleNonUniformAt",value:function(t,i,n){var s=n.x-t*n.x,e=n.y-i*n.y;return new this.constructor(this.a*t,this.b*t,this.c*i,this.d*i,this.a*s+this.c*e+this.e,this.b*s+this.d*e+this.f)}},{key:"rotate",value:function(t){var i=Math.cos(t),n=Math.sin(t);return new this.constructor(this.a*i+this.c*n,this.b*i+this.d*n,this.a*-n+this.c*i,this.b*-n+this.d*i,this.e,this.f)}},{key:"rotateAt",value:function(t,i){var n=Math.cos(t),s=Math.sin(t),e=i.x,h=i.y,r=this.a*n+this.c*s,u=this.b*n+this.d*s,a=this.c*n-this.a*s,c=this.d*n-this.b*s;return new this.constructor(r,u,a,c,(this.a-r)*e+(this.c-a)*h+this.e,(this.b-u)*e+(this.d-c)*h+this.f)}},{key:"rotateFromVector",value:function(t){var i=t.unit(),n=i.x,s=i.y;return new this.constructor(this.a*n+this.c*s,this.b*n+this.d*s,this.a*-s+this.c*n,this.b*-s+this.d*n,this.e,this.f)}},{key:"flipX",value:function(){return new this.constructor(-this.a,-this.b,this.c,this.d,this.e,this.f)}},{key:"flipY",value:function(){return new this.constructor(this.a,this.b,-this.c,-this.d,this.e,this.f)}},{key:"skewX",value:function(t){var i=Math.tan(t);return new this.constructor(this.a,this.b,this.c+this.a*i,this.d+this.b*i,this.e,this.f)}},{key:"skewY",value:function(t){var i=Math.tan(t);return new this.constructor(this.a+this.c*i,this.b+this.d*i,this.c,this.d,this.e,this.f)}},{key:"isIdentity",value:function(){return 1===this.a&&0===this.b&&0===this.c&&1===this.d&&0===this.e&&0===this.f}},{key:"isInvertible",value:function(){return this.a*this.d-this.b*this.c!=0}},{key:"getScale",value:function(){return{scaleX:Math.sqrt(this.a*this.a+this.c*this.c),scaleY:Math.sqrt(this.b*this.b+this.d*this.d)}}},{key:"getDecomposition",value:function(){var t=.5*(this.a+this.d),i=.5*(this.a-this.d),n=.5*(this.b+this.c),s=.5*(this.b-this.c),e=Math.sqrt(t*t+s*s),h=Math.sqrt(i*i+n*n),r=e+h,u=e-h,a=Math.atan2(n,i),c=Math.atan2(s,t),o=.5*(c-a),y=.5*(c+a);return{translation:this.constructor.translation(this.e,this.f),rotation:this.constructor.rotation(y),scale:this.constructor.nonUniformScaling(r,u),rotation0:this.constructor.rotation(o)}}},{key:"equals",value:function(t){return this.a===t.a&&this.b===t.b&&this.c===t.c&&this.d===t.d&&this.e===t.e&&this.f===t.f}},{key:"precisionEquals",value:function(t,i){return Math.abs(this.a-t.a)<i&&Math.abs(this.b-t.b)<i&&Math.abs(this.c-t.c)<i&&Math.abs(this.d-t.d)<i&&Math.abs(this.e-t.e)<i&&Math.abs(this.f-t.f)<i}},{key:"toString",value:function(){return"matrix(".concat(this.a,",").concat(this.b,",").concat(this.c,",").concat(this.d,",").concat(this.e,",").concat(this.f,")")}}],[{key:"translation",value:function(t,n){return new i(1,0,0,1,t,n)}},{key:"scaling",value:function(t){return new i(t,0,0,t,0,0)}},{key:"scalingAt",value:function(t,n){return new i(t,0,0,t,n.x-n.x*t,n.y-n.y*t)}},{key:"nonUniformScaling",value:function(t,n){return new i(t,0,0,n,0,0)}},{key:"nonUniformScalingAt",value:function(t,n,s){return new i(t,0,0,n,s.x-s.x*t,s.y-s.y*n)}},{key:"rotation",value:function(t){var n=Math.cos(t),s=Math.sin(t);return new i(n,s,-s,n,0,0)}},{key:"rotationAt",value:function(t,n){var s=Math.cos(t),e=Math.sin(t);return new i(s,e,-e,s,n.x-n.x*s+n.y*e,n.y-n.y*s-n.x*e)}},{key:"rotationFromVector",value:function(t){var n=t.unit(),s=n.x,e=n.y;return new i(s,e,-e,s,0,0)}},{key:"xFlip",value:function(){return new i(-1,0,0,1,0,0)}},{key:"yFlip",value:function(){return new i(1,0,0,-1,0,0)}},{key:"xSkew",value:function(t){return new i(1,0,Math.tan(t),1,0,0)}},{key:"ySkew",value:function(t){return new i(1,Math.tan(t),0,1,0,0)}}]),i}();h.IDENTITY=new h,h.IDENTITY.isIdentity=function(){return!0};export{h as Matrix2D,s as Point2D,e as Vector2D}; //# sourceMappingURL=index-esm.min.js.map