Best JavaScript code snippet using wpt
matrix.js.uncompressed.js
Source:matrix.js.uncompressed.js
1define("dojox/gfx/matrix", ["./_base","dojo/_base/lang"], 2 function(g, lang){3 var m = g.matrix = {};4 // candidates for dojox.math:5 var _degToRadCache = {};6 m._degToRad = function(degree){7 return _degToRadCache[degree] || (_degToRadCache[degree] = (Math.PI * degree / 180));8 };9 m._radToDeg = function(radian){ return radian / Math.PI * 180; };10 m.Matrix2D = function(arg){11 // summary:12 // a 2D matrix object13 // description:14 // Normalizes a 2D matrix-like object. If arrays is passed,15 // all objects of the array are normalized and multiplied sequentially.16 // arg: Object17 // a 2D matrix-like object, a number, or an array of such objects18 if(arg){19 if(typeof arg == "number"){20 this.xx = this.yy = arg;21 }else if(arg instanceof Array){22 if(arg.length > 0){23 var matrix = m.normalize(arg[0]);24 // combine matrices25 for(var i = 1; i < arg.length; ++i){26 var l = matrix, r = m.normalize(arg[i]);27 matrix = new m.Matrix2D();28 matrix.xx = l.xx * r.xx + l.xy * r.yx;29 matrix.xy = l.xx * r.xy + l.xy * r.yy;30 matrix.yx = l.yx * r.xx + l.yy * r.yx;31 matrix.yy = l.yx * r.xy + l.yy * r.yy;32 matrix.dx = l.xx * r.dx + l.xy * r.dy + l.dx;33 matrix.dy = l.yx * r.dx + l.yy * r.dy + l.dy;34 }35 lang.mixin(this, matrix);36 }37 }else{38 lang.mixin(this, arg);39 }40 }41 };42 // the default (identity) matrix, which is used to fill in missing values43 lang.extend(m.Matrix2D, {xx: 1, xy: 0, yx: 0, yy: 1, dx: 0, dy: 0});44 lang.mixin(m, {45 // summary:46 // class constants, and methods of dojox/gfx/matrix47 // matrix constants48 // identity: dojox/gfx/matrix.Matrix2D49 // an identity matrix constant: identity * (x, y) == (x, y)50 identity: new m.Matrix2D(),51 // flipX: dojox/gfx/matrix.Matrix2D52 // a matrix, which reflects points at x = 0 line: flipX * (x, y) == (-x, y)53 flipX: new m.Matrix2D({xx: -1}),54 // flipY: dojox/gfx/matrix.Matrix2D55 // a matrix, which reflects points at y = 0 line: flipY * (x, y) == (x, -y)56 flipY: new m.Matrix2D({yy: -1}),57 // flipXY: dojox/gfx/matrix.Matrix2D58 // a matrix, which reflects points at the origin of coordinates: flipXY * (x, y) == (-x, -y)59 flipXY: new m.Matrix2D({xx: -1, yy: -1}),60 // matrix creators61 translate: function(a, b){62 // summary:63 // forms a translation matrix64 // description:65 // The resulting matrix is used to translate (move) points by specified offsets.66 // a: Number|dojox/gfx.Point67 // an x coordinate value, or a point-like object, which specifies offsets for both dimensions68 // b: Number?69 // a y coordinate value70 // returns: dojox/gfx/matrix.Matrix2D71 if(arguments.length > 1){72 return new m.Matrix2D({dx: a, dy: b}); // dojox/gfx/matrix.Matrix2D73 }74 // branch75 return new m.Matrix2D({dx: a.x, dy: a.y}); // dojox/gfx/matrix.Matrix2D76 },77 scale: function(a, b){78 // summary:79 // forms a scaling matrix80 // description:81 // The resulting matrix is used to scale (magnify) points by specified offsets.82 // a: Number|dojox/gfx.Point83 // a scaling factor used for the x coordinate, or84 // a uniform scaling factor used for the both coordinates, or85 // a point-like object, which specifies scale factors for both dimensions86 // b: Number?87 // a scaling factor used for the y coordinate88 // returns: dojox/gfx/matrix.Matrix2D89 if(arguments.length > 1){90 return new m.Matrix2D({xx: a, yy: b}); // dojox/gfx/matrix.Matrix2D91 }92 if(typeof a == "number"){93 return new m.Matrix2D({xx: a, yy: a}); // dojox/gfx/matrix.Matrix2D94 }95 return new m.Matrix2D({xx: a.x, yy: a.y}); // dojox/gfx/matrix.Matrix2D96 },97 rotate: function(angle){98 // summary:99 // forms a rotating matrix100 // description:101 // The resulting matrix is used to rotate points102 // around the origin of coordinates (0, 0) by specified angle.103 // angle: Number104 // an angle of rotation in radians (>0 for CW)105 // returns: dojox/gfx/matrix.Matrix2D106 var c = Math.cos(angle);107 var s = Math.sin(angle);108 return new m.Matrix2D({xx: c, xy: -s, yx: s, yy: c}); // dojox/gfx/matrix.Matrix2D109 },110 rotateg: function(degree){111 // summary:112 // forms a rotating matrix113 // description:114 // The resulting matrix is used to rotate points115 // around the origin of coordinates (0, 0) by specified degree.116 // See dojox/gfx/matrix.rotate() for comparison.117 // degree: Number118 // an angle of rotation in degrees (>0 for CW)119 // returns: dojox/gfx/matrix.Matrix2D120 return m.rotate(m._degToRad(degree)); // dojox/gfx/matrix.Matrix2D121 },122 skewX: function(angle) {123 // summary:124 // forms an x skewing matrix125 // description:126 // The resulting matrix is used to skew points in the x dimension127 // around the origin of coordinates (0, 0) by specified angle.128 // angle: Number129 // a skewing angle in radians130 // returns: dojox/gfx/matrix.Matrix2D131 return new m.Matrix2D({xy: Math.tan(angle)}); // dojox/gfx/matrix.Matrix2D132 },133 skewXg: function(degree){134 // summary:135 // forms an x skewing matrix136 // description:137 // The resulting matrix is used to skew points in the x dimension138 // around the origin of coordinates (0, 0) by specified degree.139 // See dojox/gfx/matrix.skewX() for comparison.140 // degree: Number141 // a skewing angle in degrees142 // returns: dojox/gfx/matrix.Matrix2D143 return m.skewX(m._degToRad(degree)); // dojox/gfx/matrix.Matrix2D144 },145 skewY: function(angle){146 // summary:147 // forms a y skewing matrix148 // description:149 // The resulting matrix is used to skew points in the y dimension150 // around the origin of coordinates (0, 0) by specified angle.151 // angle: Number152 // a skewing angle in radians153 // returns: dojox/gfx/matrix.Matrix2D154 return new m.Matrix2D({yx: Math.tan(angle)}); // dojox/gfx/matrix.Matrix2D155 },156 skewYg: function(degree){157 // summary:158 // forms a y skewing matrix159 // description:160 // The resulting matrix is used to skew points in the y dimension161 // around the origin of coordinates (0, 0) by specified degree.162 // See dojox/gfx/matrix.skewY() for comparison.163 // degree: Number164 // a skewing angle in degrees165 // returns: dojox/gfx/matrix.Matrix2D166 return m.skewY(m._degToRad(degree)); // dojox/gfx/matrix.Matrix2D167 },168 reflect: function(a, b){169 // summary:170 // forms a reflection matrix171 // description:172 // The resulting matrix is used to reflect points around a vector,173 // which goes through the origin.174 // a: dojox/gfx.Point|Number175 // a point-like object, which specifies a vector of reflection, or an X value176 // b: Number?177 // a Y value178 // returns: dojox/gfx/matrix.Matrix2D179 if(arguments.length == 1){180 b = a.y;181 a = a.x;182 }183 // make a unit vector184 var a2 = a * a, b2 = b * b, n2 = a2 + b2, xy = 2 * a * b / n2;185 return new m.Matrix2D({xx: 2 * a2 / n2 - 1, xy: xy, yx: xy, yy: 2 * b2 / n2 - 1}); // dojox/gfx/matrix.Matrix2D186 },187 project: function(a, b){188 // summary:189 // forms an orthogonal projection matrix190 // description:191 // The resulting matrix is used to project points orthogonally on a vector,192 // which goes through the origin.193 // a: dojox/gfx.Point|Number194 // a point-like object, which specifies a vector of projection, or195 // an x coordinate value196 // b: Number?197 // a y coordinate value198 // returns: dojox/gfx/matrix.Matrix2D199 if(arguments.length == 1){200 b = a.y;201 a = a.x;202 }203 // make a unit vector204 var a2 = a * a, b2 = b * b, n2 = a2 + b2, xy = a * b / n2;205 return new m.Matrix2D({xx: a2 / n2, xy: xy, yx: xy, yy: b2 / n2}); // dojox/gfx/matrix.Matrix2D206 },207 // ensure matrix 2D conformance208 normalize: function(matrix){209 // summary:210 // converts an object to a matrix, if necessary211 // description:212 // Converts any 2D matrix-like object or an array of213 // such objects to a valid dojox/gfx/matrix.Matrix2D object.214 // matrix: Object215 // an object, which is converted to a matrix, if necessary216 // returns: dojox/gfx/matrix.Matrix2D217 return (matrix instanceof m.Matrix2D) ? matrix : new m.Matrix2D(matrix); // dojox/gfx/matrix.Matrix2D218 },219 // common operations220 isIdentity: function(matrix){221 // summary:222 // returns whether the specified matrix is the identity.223 // matrix: dojox/gfx/matrix.Matrix2D224 // a 2D matrix object to be tested225 // returns: Boolean226 return matrix.xx == 1 && matrix.xy == 0 && matrix.yx == 0 && matrix.yy == 1 && matrix.dx == 0 && matrix.dy == 0; // Boolean227 },228 clone: function(matrix){229 // summary:230 // creates a copy of a 2D matrix231 // matrix: dojox/gfx/matrix.Matrix2D232 // a 2D matrix-like object to be cloned233 // returns: dojox/gfx/matrix.Matrix2D234 var obj = new m.Matrix2D();235 for(var i in matrix){236 if(typeof(matrix[i]) == "number" && typeof(obj[i]) == "number" && obj[i] != matrix[i]) obj[i] = matrix[i];237 }238 return obj; // dojox/gfx/matrix.Matrix2D239 },240 invert: function(matrix){241 // summary:242 // inverts a 2D matrix243 // matrix: dojox/gfx/matrix.Matrix2D244 // a 2D matrix-like object to be inverted245 // returns: dojox/gfx/matrix.Matrix2D246 var M = m.normalize(matrix),247 D = M.xx * M.yy - M.xy * M.yx;248 M = new m.Matrix2D({249 xx: M.yy/D, xy: -M.xy/D,250 yx: -M.yx/D, yy: M.xx/D,251 dx: (M.xy * M.dy - M.yy * M.dx) / D,252 dy: (M.yx * M.dx - M.xx * M.dy) / D253 });254 return M; // dojox/gfx/matrix.Matrix2D255 },256 _multiplyPoint: function(matrix, x, y){257 // summary:258 // applies a matrix to a point259 // matrix: dojox/gfx/matrix.Matrix2D260 // a 2D matrix object to be applied261 // x: Number262 // an x coordinate of a point263 // y: Number264 // a y coordinate of a point265 // returns: dojox/gfx.Point266 return {x: matrix.xx * x + matrix.xy * y + matrix.dx, y: matrix.yx * x + matrix.yy * y + matrix.dy}; // dojox/gfx.Point267 },268 multiplyPoint: function(matrix, /* Number||Point */ a, /* Number? */ b){269 // summary:270 // applies a matrix to a point271 // matrix: dojox/gfx/matrix.Matrix2D272 // a 2D matrix object to be applied273 // a: Number|dojox/gfx.Point274 // an x coordinate of a point, or a point275 // b: Number?276 // a y coordinate of a point277 // returns: dojox/gfx.Point278 var M = m.normalize(matrix);279 if(typeof a == "number" && typeof b == "number"){280 return m._multiplyPoint(M, a, b); // dojox/gfx.Point281 }282 return m._multiplyPoint(M, a.x, a.y); // dojox/gfx.Point283 },284 multiplyRectangle: function(matrix, /*Rectangle*/ rect){285 // summary:286 // Applies a matrix to a rectangle.287 // description:288 // The method applies the transformation on all corners of the289 // rectangle and returns the smallest rectangle enclosing the 4 transformed290 // points.291 // matrix: dojox/gfx/matrix.Matrix2D292 // a 2D matrix object to be applied.293 // rect: Rectangle294 // the rectangle to transform.295 // returns: dojox/gfx.Rectangle296 var M = m.normalize(matrix);297 rect = rect || {x:0, y:0, width:0, height:0}; 298 if(m.isIdentity(M))299 return {x: rect.x, y: rect.y, width: rect.width, height: rect.height}; // dojo/gfx.Rectangle300 var p0 = m.multiplyPoint(M, rect.x, rect.y),301 p1 = m.multiplyPoint(M, rect.x, rect.y + rect.height),302 p2 = m.multiplyPoint(M, rect.x + rect.width, rect.y),303 p3 = m.multiplyPoint(M, rect.x + rect.width, rect.y + rect.height),304 minx = Math.min(p0.x, p1.x, p2.x, p3.x),305 miny = Math.min(p0.y, p1.y, p2.y, p3.y),306 maxx = Math.max(p0.x, p1.x, p2.x, p3.x),307 maxy = Math.max(p0.y, p1.y, p2.y, p3.y);308 return{ // dojo/gfx.Rectangle309 x: minx,310 y: miny,311 width: maxx - minx,312 height: maxy - miny313 };314 },315 multiply: function(matrix){316 // summary:317 // combines matrices by multiplying them sequentially in the given order318 // matrix: dojox/gfx/matrix.Matrix2D319 // a 2D matrix-like object,320 // all subsequent arguments are matrix-like objects too321 var M = m.normalize(matrix);322 // combine matrices323 for(var i = 1; i < arguments.length; ++i){324 var l = M, r = m.normalize(arguments[i]);325 M = new m.Matrix2D();326 M.xx = l.xx * r.xx + l.xy * r.yx;327 M.xy = l.xx * r.xy + l.xy * r.yy;328 M.yx = l.yx * r.xx + l.yy * r.yx;329 M.yy = l.yx * r.xy + l.yy * r.yy;330 M.dx = l.xx * r.dx + l.xy * r.dy + l.dx;331 M.dy = l.yx * r.dx + l.yy * r.dy + l.dy;332 }333 return M; // dojox/gfx/matrix.Matrix2D334 },335 // high level operations336 _sandwich: function(matrix, x, y){337 // summary:338 // applies a matrix at a central point339 // matrix: dojox/gfx/matrix.Matrix2D340 // a 2D matrix-like object, which is applied at a central point341 // x: Number342 // an x component of the central point343 // y: Number344 // a y component of the central point345 return m.multiply(m.translate(x, y), matrix, m.translate(-x, -y)); // dojox/gfx/matrix.Matrix2D346 },347 scaleAt: function(a, b, c, d){348 // summary:349 // scales a picture using a specified point as a center of scaling350 // description:351 // Compare with dojox/gfx/matrix.scale().352 // a: Number353 // a scaling factor used for the x coordinate, or a uniform scaling factor used for both coordinates354 // b: Number?355 // a scaling factor used for the y coordinate356 // c: Number|Point357 // an x component of a central point, or a central point358 // d: Number359 // a y component of a central point360 // returns: dojox/gfx/matrix.Matrix2D361 switch(arguments.length){362 case 4:363 // a and b are scale factor components, c and d are components of a point364 return m._sandwich(m.scale(a, b), c, d); // dojox/gfx/matrix.Matrix2D365 case 3:366 if(typeof c == "number"){367 return m._sandwich(m.scale(a), b, c); // dojox/gfx/matrix.Matrix2D368 }369 return m._sandwich(m.scale(a, b), c.x, c.y); // dojox/gfx/matrix.Matrix2D370 }371 return m._sandwich(m.scale(a), b.x, b.y); // dojox/gfx/matrix.Matrix2D372 },373 rotateAt: function(angle, a, b){374 // summary:375 // rotates a picture using a specified point as a center of rotation376 // description:377 // Compare with dojox/gfx/matrix.rotate().378 // angle: Number379 // an angle of rotation in radians (>0 for CW)380 // a: Number|dojox/gfx.Point381 // an x component of a central point, or a central point382 // b: Number?383 // a y component of a central point384 // returns: dojox/gfx/matrix.Matrix2D385 if(arguments.length > 2){386 return m._sandwich(m.rotate(angle), a, b); // dojox/gfx/matrix.Matrix2D387 }388 return m._sandwich(m.rotate(angle), a.x, a.y); // dojox/gfx/matrix.Matrix2D389 },390 rotategAt: function(degree, a, b){391 // summary:392 // rotates a picture using a specified point as a center of rotation393 // description:394 // Compare with dojox/gfx/matrix.rotateg().395 // degree: Number396 // an angle of rotation in degrees (>0 for CW)397 // a: Number|dojox/gfx.Point398 // an x component of a central point, or a central point399 // b: Number?400 // a y component of a central point401 // returns: dojox/gfx/matrix.Matrix2D402 if(arguments.length > 2){403 return m._sandwich(m.rotateg(degree), a, b); // dojox/gfx/matrix.Matrix2D404 }405 return m._sandwich(m.rotateg(degree), a.x, a.y); // dojox/gfx/matrix.Matrix2D406 },407 skewXAt: function(angle, a, b){408 // summary:409 // skews a picture along the x axis using a specified point as a center of skewing410 // description:411 // Compare with dojox/gfx/matrix.skewX().412 // angle: Number413 // a skewing angle in radians414 // a: Number|dojox/gfx.Point415 // an x component of a central point, or a central point416 // b: Number?417 // a y component of a central point418 // returns: dojox/gfx/matrix.Matrix2D419 if(arguments.length > 2){420 return m._sandwich(m.skewX(angle), a, b); // dojox/gfx/matrix.Matrix2D421 }422 return m._sandwich(m.skewX(angle), a.x, a.y); // dojox/gfx/matrix.Matrix2D423 },424 skewXgAt: function(degree, a, b){425 // summary:426 // skews a picture along the x axis using a specified point as a center of skewing427 // description:428 // Compare with dojox/gfx/matrix.skewXg().429 // degree: Number430 // a skewing angle in degrees431 // a: Number|dojox/gfx.Point432 // an x component of a central point, or a central point433 // b: Number?434 // a y component of a central point435 // returns: dojox/gfx/matrix.Matrix2D436 if(arguments.length > 2){437 return m._sandwich(m.skewXg(degree), a, b); // dojox/gfx/matrix.Matrix2D438 }439 return m._sandwich(m.skewXg(degree), a.x, a.y); // dojox/gfx/matrix.Matrix2D440 },441 skewYAt: function(angle, a, b){442 // summary:443 // skews a picture along the y axis using a specified point as a center of skewing444 // description:445 // Compare with dojox/gfx/matrix.skewY().446 // angle: Number447 // a skewing angle in radians448 // a: Number|dojox/gfx.Point449 // an x component of a central point, or a central point450 // b: Number?451 // a y component of a central point452 // returns: dojox/gfx/matrix.Matrix2D453 if(arguments.length > 2){454 return m._sandwich(m.skewY(angle), a, b); // dojox/gfx/matrix.Matrix2D455 }456 return m._sandwich(m.skewY(angle), a.x, a.y); // dojox/gfx/matrix.Matrix2D457 },458 skewYgAt: function(/* Number */ degree, /* Number||Point */ a, /* Number? */ b){459 // summary:460 // skews a picture along the y axis using a specified point as a center of skewing461 // description:462 // Compare with dojox/gfx/matrix.skewYg().463 // degree: Number464 // a skewing angle in degrees465 // a: Number|dojox/gfx.Point466 // an x component of a central point, or a central point467 // b: Number?468 // a y component of a central point469 // returns: dojox/gfx/matrix.Matrix2D470 if(arguments.length > 2){471 return m._sandwich(m.skewYg(degree), a, b); // dojox/gfx/matrix.Matrix2D472 }473 return m._sandwich(m.skewYg(degree), a.x, a.y); // dojox/gfx/matrix.Matrix2D474 }475 //TODO: rect-to-rect mapping, scale-to-fit (isotropic and anisotropic versions)476 });477 // propagate Matrix2D up478 g.Matrix2D = m.Matrix2D;479 return m;...
matrix.js
Source:matrix.js
1define(["./_base","dojo/_base/lang"], 2 function(g, lang){3 var m = g.matrix = {};4 // candidates for dojox.math:5 var _degToRadCache = {};6 m._degToRad = function(degree){7 return _degToRadCache[degree] || (_degToRadCache[degree] = (Math.PI * degree / 180));8 };9 m._radToDeg = function(radian){ return radian / Math.PI * 180; };10 m.Matrix2D = function(arg){11 // summary:12 // a 2D matrix object13 // description:14 // Normalizes a 2D matrix-like object. If arrays is passed,15 // all objects of the array are normalized and multiplied sequentially.16 // arg: Object17 // a 2D matrix-like object, a number, or an array of such objects18 if(arg){19 if(typeof arg == "number"){20 this.xx = this.yy = arg;21 }else if(arg instanceof Array){22 if(arg.length > 0){23 var matrix = m.normalize(arg[0]);24 // combine matrices25 for(var i = 1; i < arg.length; ++i){26 var l = matrix, r = m.normalize(arg[i]);27 matrix = new m.Matrix2D();28 matrix.xx = l.xx * r.xx + l.xy * r.yx;29 matrix.xy = l.xx * r.xy + l.xy * r.yy;30 matrix.yx = l.yx * r.xx + l.yy * r.yx;31 matrix.yy = l.yx * r.xy + l.yy * r.yy;32 matrix.dx = l.xx * r.dx + l.xy * r.dy + l.dx;33 matrix.dy = l.yx * r.dx + l.yy * r.dy + l.dy;34 }35 lang.mixin(this, matrix);36 }37 }else{38 lang.mixin(this, arg);39 }40 }41 };42 // the default (identity) matrix, which is used to fill in missing values43 lang.extend(m.Matrix2D, {xx: 1, xy: 0, yx: 0, yy: 1, dx: 0, dy: 0});44 lang.mixin(m, {45 // summary:46 // class constants, and methods of dojox/gfx/matrix47 // matrix constants48 // identity: dojox/gfx/matrix.Matrix2D49 // an identity matrix constant: identity * (x, y) == (x, y)50 identity: new m.Matrix2D(),51 // flipX: dojox/gfx/matrix.Matrix2D52 // a matrix, which reflects points at x = 0 line: flipX * (x, y) == (-x, y)53 flipX: new m.Matrix2D({xx: -1}),54 // flipY: dojox/gfx/matrix.Matrix2D55 // a matrix, which reflects points at y = 0 line: flipY * (x, y) == (x, -y)56 flipY: new m.Matrix2D({yy: -1}),57 // flipXY: dojox/gfx/matrix.Matrix2D58 // a matrix, which reflects points at the origin of coordinates: flipXY * (x, y) == (-x, -y)59 flipXY: new m.Matrix2D({xx: -1, yy: -1}),60 // matrix creators61 translate: function(a, b){62 // summary:63 // forms a translation matrix64 // description:65 // The resulting matrix is used to translate (move) points by specified offsets.66 // a: Number|dojox/gfx.Point67 // an x coordinate value, or a point-like object, which specifies offsets for both dimensions68 // b: Number?69 // a y coordinate value70 // returns: dojox/gfx/matrix.Matrix2D71 if(arguments.length > 1){72 return new m.Matrix2D({dx: a, dy: b}); // dojox/gfx/matrix.Matrix2D73 }74 // branch75 return new m.Matrix2D({dx: a.x, dy: a.y}); // dojox/gfx/matrix.Matrix2D76 },77 scale: function(a, b){78 // summary:79 // forms a scaling matrix80 // description:81 // The resulting matrix is used to scale (magnify) points by specified offsets.82 // a: Number|dojox/gfx.Point83 // a scaling factor used for the x coordinate, or84 // a uniform scaling factor used for the both coordinates, or85 // a point-like object, which specifies scale factors for both dimensions86 // b: Number?87 // a scaling factor used for the y coordinate88 // returns: dojox/gfx/matrix.Matrix2D89 if(arguments.length > 1){90 return new m.Matrix2D({xx: a, yy: b}); // dojox/gfx/matrix.Matrix2D91 }92 if(typeof a == "number"){93 return new m.Matrix2D({xx: a, yy: a}); // dojox/gfx/matrix.Matrix2D94 }95 return new m.Matrix2D({xx: a.x, yy: a.y}); // dojox/gfx/matrix.Matrix2D96 },97 rotate: function(angle){98 // summary:99 // forms a rotating matrix100 // description:101 // The resulting matrix is used to rotate points102 // around the origin of coordinates (0, 0) by specified angle.103 // angle: Number104 // an angle of rotation in radians (>0 for CW)105 // returns: dojox/gfx/matrix.Matrix2D106 var c = Math.cos(angle);107 var s = Math.sin(angle);108 return new m.Matrix2D({xx: c, xy: -s, yx: s, yy: c}); // dojox/gfx/matrix.Matrix2D109 },110 rotateg: function(degree){111 // summary:112 // forms a rotating matrix113 // description:114 // The resulting matrix is used to rotate points115 // around the origin of coordinates (0, 0) by specified degree.116 // See dojox/gfx/matrix.rotate() for comparison.117 // degree: Number118 // an angle of rotation in degrees (>0 for CW)119 // returns: dojox/gfx/matrix.Matrix2D120 return m.rotate(m._degToRad(degree)); // dojox/gfx/matrix.Matrix2D121 },122 skewX: function(angle) {123 // summary:124 // forms an x skewing matrix125 // description:126 // The resulting matrix is used to skew points in the x dimension127 // around the origin of coordinates (0, 0) by specified angle.128 // angle: Number129 // a skewing angle in radians130 // returns: dojox/gfx/matrix.Matrix2D131 return new m.Matrix2D({xy: Math.tan(angle)}); // dojox/gfx/matrix.Matrix2D132 },133 skewXg: function(degree){134 // summary:135 // forms an x skewing matrix136 // description:137 // The resulting matrix is used to skew points in the x dimension138 // around the origin of coordinates (0, 0) by specified degree.139 // See dojox/gfx/matrix.skewX() for comparison.140 // degree: Number141 // a skewing angle in degrees142 // returns: dojox/gfx/matrix.Matrix2D143 return m.skewX(m._degToRad(degree)); // dojox/gfx/matrix.Matrix2D144 },145 skewY: function(angle){146 // summary:147 // forms a y skewing matrix148 // description:149 // The resulting matrix is used to skew points in the y dimension150 // around the origin of coordinates (0, 0) by specified angle.151 // angle: Number152 // a skewing angle in radians153 // returns: dojox/gfx/matrix.Matrix2D154 return new m.Matrix2D({yx: Math.tan(angle)}); // dojox/gfx/matrix.Matrix2D155 },156 skewYg: function(degree){157 // summary:158 // forms a y skewing matrix159 // description:160 // The resulting matrix is used to skew points in the y dimension161 // around the origin of coordinates (0, 0) by specified degree.162 // See dojox/gfx/matrix.skewY() for comparison.163 // degree: Number164 // a skewing angle in degrees165 // returns: dojox/gfx/matrix.Matrix2D166 return m.skewY(m._degToRad(degree)); // dojox/gfx/matrix.Matrix2D167 },168 reflect: function(a, b){169 // summary:170 // forms a reflection matrix171 // description:172 // The resulting matrix is used to reflect points around a vector,173 // which goes through the origin.174 // a: dojox/gfx.Point|Number175 // a point-like object, which specifies a vector of reflection, or an X value176 // b: Number?177 // a Y value178 // returns: dojox/gfx/matrix.Matrix2D179 if(arguments.length == 1){180 b = a.y;181 a = a.x;182 }183 // make a unit vector184 var a2 = a * a, b2 = b * b, n2 = a2 + b2, xy = 2 * a * b / n2;185 return new m.Matrix2D({xx: 2 * a2 / n2 - 1, xy: xy, yx: xy, yy: 2 * b2 / n2 - 1}); // dojox/gfx/matrix.Matrix2D186 },187 project: function(a, b){188 // summary:189 // forms an orthogonal projection matrix190 // description:191 // The resulting matrix is used to project points orthogonally on a vector,192 // which goes through the origin.193 // a: dojox/gfx.Point|Number194 // a point-like object, which specifies a vector of projection, or195 // an x coordinate value196 // b: Number?197 // a y coordinate value198 // returns: dojox/gfx/matrix.Matrix2D199 if(arguments.length == 1){200 b = a.y;201 a = a.x;202 }203 // make a unit vector204 var a2 = a * a, b2 = b * b, n2 = a2 + b2, xy = a * b / n2;205 return new m.Matrix2D({xx: a2 / n2, xy: xy, yx: xy, yy: b2 / n2}); // dojox/gfx/matrix.Matrix2D206 },207 // ensure matrix 2D conformance208 normalize: function(matrix){209 // summary:210 // converts an object to a matrix, if necessary211 // description:212 // Converts any 2D matrix-like object or an array of213 // such objects to a valid dojox/gfx/matrix.Matrix2D object.214 // matrix: Object215 // an object, which is converted to a matrix, if necessary216 // returns: dojox/gfx/matrix.Matrix2D217 return (matrix instanceof m.Matrix2D) ? matrix : new m.Matrix2D(matrix); // dojox/gfx/matrix.Matrix2D218 },219 // common operations220 isIdentity: function(matrix){221 // summary:222 // returns whether the specified matrix is the identity.223 // matrix: dojox/gfx/matrix.Matrix2D224 // a 2D matrix object to be tested225 // returns: Boolean226 return matrix.xx == 1 && matrix.xy == 0 && matrix.yx == 0 && matrix.yy == 1 && matrix.dx == 0 && matrix.dy == 0; // Boolean227 },228 clone: function(matrix){229 // summary:230 // creates a copy of a 2D matrix231 // matrix: dojox/gfx/matrix.Matrix2D232 // a 2D matrix-like object to be cloned233 // returns: dojox/gfx/matrix.Matrix2D234 var obj = new m.Matrix2D();235 for(var i in matrix){236 if(typeof(matrix[i]) == "number" && typeof(obj[i]) == "number" && obj[i] != matrix[i]) obj[i] = matrix[i];237 }238 return obj; // dojox/gfx/matrix.Matrix2D239 },240 invert: function(matrix){241 // summary:242 // inverts a 2D matrix243 // matrix: dojox/gfx/matrix.Matrix2D244 // a 2D matrix-like object to be inverted245 // returns: dojox/gfx/matrix.Matrix2D246 var M = m.normalize(matrix),247 D = M.xx * M.yy - M.xy * M.yx;248 M = new m.Matrix2D({249 xx: M.yy/D, xy: -M.xy/D,250 yx: -M.yx/D, yy: M.xx/D,251 dx: (M.xy * M.dy - M.yy * M.dx) / D,252 dy: (M.yx * M.dx - M.xx * M.dy) / D253 });254 return M; // dojox/gfx/matrix.Matrix2D255 },256 _multiplyPoint: function(matrix, x, y){257 // summary:258 // applies a matrix to a point259 // matrix: dojox/gfx/matrix.Matrix2D260 // a 2D matrix object to be applied261 // x: Number262 // an x coordinate of a point263 // y: Number264 // a y coordinate of a point265 // returns: dojox/gfx.Point266 return {x: matrix.xx * x + matrix.xy * y + matrix.dx, y: matrix.yx * x + matrix.yy * y + matrix.dy}; // dojox/gfx.Point267 },268 multiplyPoint: function(matrix, /* Number||Point */ a, /* Number? */ b){269 // summary:270 // applies a matrix to a point271 // matrix: dojox/gfx/matrix.Matrix2D272 // a 2D matrix object to be applied273 // a: Number|dojox/gfx.Point274 // an x coordinate of a point, or a point275 // b: Number?276 // a y coordinate of a point277 // returns: dojox/gfx.Point278 var M = m.normalize(matrix);279 if(typeof a == "number" && typeof b == "number"){280 return m._multiplyPoint(M, a, b); // dojox/gfx.Point281 }282 return m._multiplyPoint(M, a.x, a.y); // dojox/gfx.Point283 },284 multiplyRectangle: function(matrix, /*Rectangle*/ rect){285 // summary:286 // Applies a matrix to a rectangle.287 // description:288 // The method applies the transformation on all corners of the289 // rectangle and returns the smallest rectangle enclosing the 4 transformed290 // points.291 // matrix: dojox/gfx/matrix.Matrix2D292 // a 2D matrix object to be applied.293 // rect: Rectangle294 // the rectangle to transform.295 // returns: dojox/gfx.Rectangle296 var M = m.normalize(matrix);297 rect = rect || {x:0, y:0, width:0, height:0}; 298 if(m.isIdentity(M))299 return {x: rect.x, y: rect.y, width: rect.width, height: rect.height}; // dojo/gfx.Rectangle300 var p0 = m.multiplyPoint(M, rect.x, rect.y),301 p1 = m.multiplyPoint(M, rect.x, rect.y + rect.height),302 p2 = m.multiplyPoint(M, rect.x + rect.width, rect.y),303 p3 = m.multiplyPoint(M, rect.x + rect.width, rect.y + rect.height),304 minx = Math.min(p0.x, p1.x, p2.x, p3.x),305 miny = Math.min(p0.y, p1.y, p2.y, p3.y),306 maxx = Math.max(p0.x, p1.x, p2.x, p3.x),307 maxy = Math.max(p0.y, p1.y, p2.y, p3.y);308 return{ // dojo/gfx.Rectangle309 x: minx,310 y: miny,311 width: maxx - minx,312 height: maxy - miny313 };314 },315 multiply: function(matrix){316 // summary:317 // combines matrices by multiplying them sequentially in the given order318 // matrix: dojox/gfx/matrix.Matrix2D319 // a 2D matrix-like object,320 // all subsequent arguments are matrix-like objects too321 var M = m.normalize(matrix);322 // combine matrices323 for(var i = 1; i < arguments.length; ++i){324 var l = M, r = m.normalize(arguments[i]);325 M = new m.Matrix2D();326 M.xx = l.xx * r.xx + l.xy * r.yx;327 M.xy = l.xx * r.xy + l.xy * r.yy;328 M.yx = l.yx * r.xx + l.yy * r.yx;329 M.yy = l.yx * r.xy + l.yy * r.yy;330 M.dx = l.xx * r.dx + l.xy * r.dy + l.dx;331 M.dy = l.yx * r.dx + l.yy * r.dy + l.dy;332 }333 return M; // dojox/gfx/matrix.Matrix2D334 },335 // high level operations336 _sandwich: function(matrix, x, y){337 // summary:338 // applies a matrix at a central point339 // matrix: dojox/gfx/matrix.Matrix2D340 // a 2D matrix-like object, which is applied at a central point341 // x: Number342 // an x component of the central point343 // y: Number344 // a y component of the central point345 return m.multiply(m.translate(x, y), matrix, m.translate(-x, -y)); // dojox/gfx/matrix.Matrix2D346 },347 scaleAt: function(a, b, c, d){348 // summary:349 // scales a picture using a specified point as a center of scaling350 // description:351 // Compare with dojox/gfx/matrix.scale().352 // a: Number353 // a scaling factor used for the x coordinate, or a uniform scaling factor used for both coordinates354 // b: Number?355 // a scaling factor used for the y coordinate356 // c: Number|Point357 // an x component of a central point, or a central point358 // d: Number359 // a y component of a central point360 // returns: dojox/gfx/matrix.Matrix2D361 switch(arguments.length){362 case 4:363 // a and b are scale factor components, c and d are components of a point364 return m._sandwich(m.scale(a, b), c, d); // dojox/gfx/matrix.Matrix2D365 case 3:366 if(typeof c == "number"){367 return m._sandwich(m.scale(a), b, c); // dojox/gfx/matrix.Matrix2D368 }369 return m._sandwich(m.scale(a, b), c.x, c.y); // dojox/gfx/matrix.Matrix2D370 }371 return m._sandwich(m.scale(a), b.x, b.y); // dojox/gfx/matrix.Matrix2D372 },373 rotateAt: function(angle, a, b){374 // summary:375 // rotates a picture using a specified point as a center of rotation376 // description:377 // Compare with dojox/gfx/matrix.rotate().378 // angle: Number379 // an angle of rotation in radians (>0 for CW)380 // a: Number|dojox/gfx.Point381 // an x component of a central point, or a central point382 // b: Number?383 // a y component of a central point384 // returns: dojox/gfx/matrix.Matrix2D385 if(arguments.length > 2){386 return m._sandwich(m.rotate(angle), a, b); // dojox/gfx/matrix.Matrix2D387 }388 return m._sandwich(m.rotate(angle), a.x, a.y); // dojox/gfx/matrix.Matrix2D389 },390 rotategAt: function(degree, a, b){391 // summary:392 // rotates a picture using a specified point as a center of rotation393 // description:394 // Compare with dojox/gfx/matrix.rotateg().395 // degree: Number396 // an angle of rotation in degrees (>0 for CW)397 // a: Number|dojox/gfx.Point398 // an x component of a central point, or a central point399 // b: Number?400 // a y component of a central point401 // returns: dojox/gfx/matrix.Matrix2D402 if(arguments.length > 2){403 return m._sandwich(m.rotateg(degree), a, b); // dojox/gfx/matrix.Matrix2D404 }405 return m._sandwich(m.rotateg(degree), a.x, a.y); // dojox/gfx/matrix.Matrix2D406 },407 skewXAt: function(angle, a, b){408 // summary:409 // skews a picture along the x axis using a specified point as a center of skewing410 // description:411 // Compare with dojox/gfx/matrix.skewX().412 // angle: Number413 // a skewing angle in radians414 // a: Number|dojox/gfx.Point415 // an x component of a central point, or a central point416 // b: Number?417 // a y component of a central point418 // returns: dojox/gfx/matrix.Matrix2D419 if(arguments.length > 2){420 return m._sandwich(m.skewX(angle), a, b); // dojox/gfx/matrix.Matrix2D421 }422 return m._sandwich(m.skewX(angle), a.x, a.y); // dojox/gfx/matrix.Matrix2D423 },424 skewXgAt: function(degree, a, b){425 // summary:426 // skews a picture along the x axis using a specified point as a center of skewing427 // description:428 // Compare with dojox/gfx/matrix.skewXg().429 // degree: Number430 // a skewing angle in degrees431 // a: Number|dojox/gfx.Point432 // an x component of a central point, or a central point433 // b: Number?434 // a y component of a central point435 // returns: dojox/gfx/matrix.Matrix2D436 if(arguments.length > 2){437 return m._sandwich(m.skewXg(degree), a, b); // dojox/gfx/matrix.Matrix2D438 }439 return m._sandwich(m.skewXg(degree), a.x, a.y); // dojox/gfx/matrix.Matrix2D440 },441 skewYAt: function(angle, a, b){442 // summary:443 // skews a picture along the y axis using a specified point as a center of skewing444 // description:445 // Compare with dojox/gfx/matrix.skewY().446 // angle: Number447 // a skewing angle in radians448 // a: Number|dojox/gfx.Point449 // an x component of a central point, or a central point450 // b: Number?451 // a y component of a central point452 // returns: dojox/gfx/matrix.Matrix2D453 if(arguments.length > 2){454 return m._sandwich(m.skewY(angle), a, b); // dojox/gfx/matrix.Matrix2D455 }456 return m._sandwich(m.skewY(angle), a.x, a.y); // dojox/gfx/matrix.Matrix2D457 },458 skewYgAt: function(/* Number */ degree, /* Number||Point */ a, /* Number? */ b){459 // summary:460 // skews a picture along the y axis using a specified point as a center of skewing461 // description:462 // Compare with dojox/gfx/matrix.skewYg().463 // degree: Number464 // a skewing angle in degrees465 // a: Number|dojox/gfx.Point466 // an x component of a central point, or a central point467 // b: Number?468 // a y component of a central point469 // returns: dojox/gfx/matrix.Matrix2D470 if(arguments.length > 2){471 return m._sandwich(m.skewYg(degree), a, b); // dojox/gfx/matrix.Matrix2D472 }473 return m._sandwich(m.skewYg(degree), a.x, a.y); // dojox/gfx/matrix.Matrix2D474 }475 //TODO: rect-to-rect mapping, scale-to-fit (isotropic and anisotropic versions)476 });477 // propagate Matrix2D up478 g.Matrix2D = m.Matrix2D;479 return m;...
math.js
Source:math.js
1(function() {2 var module = QUnit.module;3 var test = QUnit.test;4 var Matrix2D = Crafty.math.Matrix2D;5 var Vector2D = Crafty.math.Vector2D;6 // tests for general functions should go here (.abs(), .amountOf(), etc)7 module("Math - Vector2D");8 test("constructor", function(_) {9 var v0 = new Vector2D();10 var v00 = new Vector2D(0, 0);11 var v12 = new Vector2D(1, 2);12 var v12_2 = new Vector2D(v12);13 _.strictEqual(v0.equals(v00), true, "new Vector2D() equals new Vector2D(0, 0)");14 _.strictEqual(v12.equals(v12_2), true, "new Vector2D(1, 2) equals new Vector2D(new Vector2D(1,2))");15 });16 test("add()", function(_) {17 var v12 = new Vector2D(1, 2);18 var v34 = new Vector2D(3, 4);19 var v = new Vector2D(4, 6);20 _.strictEqual(v12.add(v34).equals(v), true, "<1,2> + <3,4> = <4,6>");21 });22 test("angleBetween()", function(_) {23 var v10 = new Vector2D(1, 0);24 var v_11 = new Vector2D(-1, 1);25 var v1_1 = new Vector2D(1, -1);26 _.strictEqual(v10.angleBetween(v_11), 3 * Math.PI / 4, "<1,0>.angleBetween(<0,1>) = 3*PI/4");27 _.strictEqual(v10.angleBetween(v1_1), -Math.PI / 4, "<1,0>.angleBetween(<1,-1>) = -PI/4");28 });29 test("angleTo()", function(_) {30 var v0 = new Vector2D();31 var v11 = new Vector2D(1, 1);32 var v10 = new Vector2D(1, 0);33 var v0_1 = new Vector2D(0, -1);34 _.strictEqual(v0.angleTo(v11), Math.PI / 4, "<0,0>.angleTo(<1,1>) = PI/4");35 _.strictEqual(v10.angleTo(v0_1), -3 * Math.PI / 4, "<1,0>.angleTo(<0,-1>) = -3*PI/4");36 });37 test("clone()", function(_) {38 var v0 = new Vector2D();39 var v3_7 = new Vector2D(3, -7);40 _.strictEqual(v0.equals(v0.clone()), true, "<0,0> = <0,0>.clone()");41 _.strictEqual(v3_7.clone().equals(v3_7), true, "<3,-7>.clone() = <3,-7>");42 });43 test("distance()", function(_) {44 var v0 = new Vector2D();45 var v10 = new Vector2D(1, 0);46 var v11 = new Vector2D(1, 1);47 _.strictEqual(v10.distance(v11), 1, "<1,0>.distance(<1,1>) = 1");48 _.strictEqual(v0.distance(v11), Math.sqrt(2), "<0,0>.distance(<1,1>) = sqrt(2)");49 });50 test("distanceSq()", function(_) {51 var v0 = new Vector2D();52 var v10 = new Vector2D(1, 0);53 var v11 = new Vector2D(1, 1);54 _.strictEqual(v10.distanceSq(v11), 1, "<1,0>.distanceSq(<1,1>) = 1");55 _.strictEqual(v0.distanceSq(v11), 2, "<0,0>.distanceSq(<1,1>) = 2");56 });57 test("divide()", function(_) {58 var v12 = new Vector2D(1, 2);59 var v34 = new Vector2D(3, 4);60 var v = new Vector2D(1 / 3, 2 / 4);61 _.strictEqual(v12.divide(v34).equals(v), true, "<1,2> / <3,4> = <1/3,1/2>");62 });63 test("dotProduct()", function(_) {64 var v12 = new Vector2D(1, 2);65 var v34 = new Vector2D(3, 4);66 var v46 = new Vector2D(4, 6);67 _.strictEqual(v12.dotProduct(v34), 11, "<1,2>.dotProduct(<3,4>) = 11");68 _.strictEqual(v34.dotProduct(v46), 36, "<3,4>.dotProduct(<4,6>) = 36");69 _.strictEqual(v46.dotProduct(v12), 16, "<4,6>.dotProduct(<1,2>) = 16");70 });71 test("crossProduct()", function(_) {72 var v12 = new Vector2D(1, 2);73 var v34 = new Vector2D(3, 4);74 var v46 = new Vector2D(4, 6);75 _.strictEqual(v12.crossProduct(v34), -2, "<1,2>.crossProduct(<3,4>) = -2");76 _.strictEqual(v34.crossProduct(v46), 2, "<3,4>.crossProduct(<4,6>) = 2");77 _.strictEqual(v46.crossProduct(v12), 2, "<4,6>.crossProduct(<1,2>) = 2");78 });79 test("equals()", function(_) {80 var v12 = new Vector2D(1, 2);81 var v34 = new Vector2D(3, 4);82 var v46 = new Vector2D(4, 6);83 _.strictEqual(v12.equals(new Vector2D(1, 2)), true, "<1,2>.equals(<1,2>) = true");84 _.strictEqual(v34.equals(new Vector2D(3, 4)), true, "<3,4>.equals(<3,4>) = true");85 _.strictEqual(v46.equals(new Vector2D(4, 6)), true, "<4,6>.equals(<4,6>) = true");86 });87 test("perpendicular()", function(_) {88 var v10 = new Vector2D(1, 0);89 _.strictEqual(v10.perpendicular().equals(new Vector2D(0, 1)), true, "<1,0>.perpendicular() = <0,1>");90 });91 test("getNormal()", function(_) {92 var v10 = new Vector2D(1, 0);93 var v32 = new Vector2D(3, 2);94 _.strictEqual(v10.getNormal(v32).equals((new Vector2D(1, -1)).normalize()), true, "<1,0>.getNormal(<3,2>) = <sqrt(2)/2,-sqrt(2)/2>");95 });96 test("isZero()", function(_) {97 var v0 = new Vector2D();98 var v10 = new Vector2D(1, 0);99 _.strictEqual(v0.isZero(), true, "<0,0>.isZero() = true");100 _.strictEqual(v10.isZero(), false, "<1,0>.isZero() = false");101 });102 test("magnitude()", function(_) {103 var v0 = new Vector2D();104 var v10 = new Vector2D(1, 0);105 var v_79 = new Vector2D(-7, 9);106 _.strictEqual(v0.magnitude(), 0, "<0,0>.magnitude() = 0");107 _.strictEqual(v10.magnitude(), 1, "<1,0>.magnitude() = 1");108 _.strictEqual(v_79.magnitude(), 11.40175425099138, "<-7,9>.magnitude() = 11.40175425099138");109 });110 test("magnitudeSq()", function(_) {111 var v0 = new Vector2D();112 var v10 = new Vector2D(1, 0);113 var v_79 = new Vector2D(-7, 9);114 _.strictEqual(v0.magnitudeSq(), 0, "<0,0>.magnitudeSq() = 0");115 _.strictEqual(v10.magnitudeSq(), 1, "<1,0>.magnitudeSq() = 1");116 _.strictEqual(v_79.magnitudeSq(), 130, "<-7,9>.magnitudeSq() = 130");117 });118 test("multiply()", function(_) {119 var v12 = new Vector2D(1, 2);120 var v34 = new Vector2D(3, 4);121 var v = new Vector2D(3, 8);122 _.strictEqual(v12.multiply(v34).equals(v), true, "<1,2> * <3,4> = <3,8>");123 });124 test("negate()", function(_) {125 var v_79 = new Vector2D(-7, 9);126 var v7_9 = new Vector2D(7, -9);127 _.strictEqual(v_79.negate().equals(v7_9), true, "<-7,9>.negate() = <7,-9>");128 });129 test("normalize()", function(_) {130 var v0 = new Vector2D();131 var v01 = new Vector2D(0, 1);132 var v_79 = new Vector2D(-7, 9);133 _.strictEqual(v0.normalize().equals(new Vector2D(1, 0)), true, "<0,0>.normalize() = <1,0>");134 _.strictEqual(v01.normalize().equals(new Vector2D(0, 1)), true, "<0,1>.normalize() = <0,1>");135 _.strictEqual(v_79.normalize().equals(new Vector2D(-0.6139406135149205, 0.7893522173763263)), true, "<-7,9>.normalize() = <-0.6139406135149205,0.7893522173763263>");136 });137 test("scale()", function(_) {138 var v11 = new Vector2D(1, 1);139 _.strictEqual(v11.scale(2).equals(new Vector2D(2, 2)), true, "<1,1>.scale(2) = <2,2>");140 _.strictEqual(v11.scale(2, -3).equals(new Vector2D(4, -6)), true, "<2,2>.scale(2, -3) = <4,-6>");141 });142 test("scaleToMagnitude()", function(_) {143 var v34 = new Vector2D(3, 4);144 _.strictEqual(v34.normalize().scaleToMagnitude(5).equals(new Vector2D(3, 4)), true, "<3,4>.normalize().scaleToMagnitude(5) = <3,4>");145 });146 test("setValues", function(_) {147 var v0 = new Vector2D();148 var v12 = new Vector2D(1, 2);149 var v44 = new Vector2D(4, 4);150 _.strictEqual(v0.setValues(1, 2).equals(v12), true, "<0,0>.setValues(<1,2>) = <1,2>");151 _.strictEqual(v0.setValues(v44).equals(v44), true, "<1,2>.setValues(<4,4>) = <4,4>");152 });153 test("subtract()", function(_) {154 var v12 = new Vector2D(1, 2);155 var v34 = new Vector2D(3, 4);156 var v = new Vector2D(-2, -2);157 _.strictEqual(v12.subtract(v34).equals(v), true, "<1,2> - <3,4> = <-2,-2>");158 });159 test("toString()", function(_) {160 var v12 = new Vector2D(1, 2);161 _.strictEqual(v12.toString(), "Vector2D(1, 2)", "<1,2> = \"Vector2D(1, 2)\"");162 });163 test("translate()", function(_) {164 var v11 = new Vector2D(1, 1);165 _.strictEqual(v11.translate(2).equals(new Vector2D(3, 3)), true, "<1,1>.translate(2) = <3,3>");166 _.strictEqual(v11.translate(2, -3).equals(new Vector2D(5, 0)), true, "<3,3>.translate(2, -3) = <5,0>");167 });168 test("tripleProduct()", function(_) {169 var va = new Vector2D(1, 2);170 var vb = new Vector2D(3, 4);171 var vc = new Vector2D(5, 6);172 var vtp = new Vector2D(12, -10);173 _.strictEqual(Vector2D.tripleProduct(va, vb, vc).equals(vtp), true, "tripleProduct(<1,2>, <3,4>, <5,6>) = <10,-12>");174 });175 module("Math - Matrix2D");176 test("apply()", function(_) {177 _.strictEqual((new Matrix2D()).rotate(Math.PI / 2).apply(new Vector2D(1, 2)).equals(new Vector2D(-2, 1.0000000000000002)),178 true, "(new Matrix2D()).rotate(Math.PI/2).apply(new Vector2D(1, 2)).equals(new Vector2D(-2, 1.0000000000000002))");179 });180 test("clone()", function(_) {181 _.strictEqual((new Matrix2D(1, 2, 3, 4, 5, 6)).clone().equals(new Matrix2D(1, 2, 3, 4, 5, 6)),182 true, "(new Matrix2D(1, 2, 3, 4, 5, 6)).clone().equals(new Matrix2D(1, 2, 3, 4, 5, 6))");183 });184 test("combine()", function(_) {185 _.strictEqual((new Matrix2D()).scale(2).combine((new Matrix2D()).rotate(0.75)).equals((new Matrix2D()).scale(2).rotate(0.75)),186 true, "(new Matrix2D()).scale(2).combine((new Matrix2D()).rotate(0.75)).equals((new Matrix2D()).scale(2).rotate(0.75))");187 });188 test("equals()", function(_) {189 _.strictEqual((new Matrix2D()).equals(new Matrix2D()),190 true, "(new Matrix2D()).equals(new Matrix2D())");191 _.strictEqual((new Matrix2D()).scale(2).equals(new Matrix2D()),192 false, "(new Matrix2D()).scale(2).equals(new Matrix2D())");193 });194 test("determinant()", function(_) {195 _.strictEqual((new Matrix2D()).scale(2, 3).rotate(Math.PI / 2).determinant(),196 6, "(new Matrix2D()).scale(2, 3).rotate(Math.PI / 2).determinant()");197 });198 test("invert()", function(_) {199 var m = new Matrix2D(4, 3, 3, 2, 0, 0);200 var m2 = new Matrix2D(-2, 3, 3, -4, 0, 0);201 _.ok( m.invert().equals(m2), "Matrix (4,3,3,2) inverts to (-2,3,3,-4)");202 });203 test("isIdentity()", function(_) {204 _.strictEqual((new Matrix2D()).isIdentity(),205 true, "(new Matrix2D()).isIdentity()");206 _.strictEqual((new Matrix2D()).scale(2).isIdentity(),207 false, "(new Matrix2D()).scale(2).isIdentity()");208 });209 test("isInvertible()", function(_) {210 _.strictEqual((new Matrix2D()).scale(2, 3).rotate(Math.PI / 2).isInvertible(),211 true, "(new Matrix2D()).scale(2, 3).rotate(Math.PI / 2).isInvertible()");212 _.strictEqual((new Matrix2D()).scale(0, 3).rotate(Math.PI / 2).isInvertible(),213 false, "(new Matrix2D()).scale(0, 3).rotate(Math.PI / 2).isInvertible()");214 });215 test("preRotate()", function(_) {216 _.strictEqual((new Matrix2D()).preRotate(0).equals(new Matrix2D()),217 true, "(new Matrix2D()).preRotate(0).equals(new Matrix2D())");218 _.strictEqual((new Matrix2D()).preRotate(Math.PI / 2).equals((new Matrix2D()).rotate(Math.PI / 2)),219 true, "(new Matrix2D()).preRotate(Math.PI / 2).equals((new Matrix2D()).rotate(Math.PI / 2))");220 });221 test("preScale()", function(_) {222 _.strictEqual((new Matrix2D()).preScale(2).equals(new Matrix2D(2, 0, 0, 2, 0, 0)),223 true, "(new Matrix2D()).preScale(2).equals(new Matrix2D(2, 0, 0, 2, 0, 0))");224 _.strictEqual((new Matrix2D()).preScale(2.5).equals((new Matrix2D()).scale(2.5)),225 true, "(new Matrix2D()).preScale(2.5).equals((new Matrix2D()).scale(2.5))");226 });227 test("preTranslate()", function(_) {228 _.strictEqual((new Matrix2D()).preTranslate(1, 2).equals(new Matrix2D(1, 0, 0, 1, 1, 2)),229 true, "(new Matrix2D()).preTranslate(1, 2).equals(new Matrix2D(1, 0, 0, 1, 1, 2)");230 _.strictEqual((new Matrix2D()).preTranslate(1, 2).equals((new Matrix2D()).translate(new Vector2D(1, 2))),231 true, "(new Matrix2D()).preTranslate(1, 2).equals((new Matrix2D()).translate(new Vector2D(1, 2)))");232 _.strictEqual((new Matrix2D()).preTranslate(new Vector2D(1, 2)).equals(new Matrix2D(1, 0, 0, 1, 1, 2)),233 true, "(new Matrix2D()).preTranslate(new Vector2D(1, 2)).equals(new Matrix2D(1, 0, 0, 1, 1, 2))");234 _.strictEqual((new Matrix2D()).preTranslate(new Vector2D(1, 2)).equals((new Matrix2D()).translate(new Vector2D(1, 2))),235 true, "(new Matrix2D()).preTranslate(new Vector2D(1, 2)).equals((new Matrix2D()).translate(new Vector2D(1, 2)))");236 });237 test("rotate()", function(_) {238 _.strictEqual((new Matrix2D()).rotate(0).equals(new Matrix2D()),239 true, "(new Matrix2D()).rotate(0).equals(new Matrix2D())");240 });241 test("scale()", function(_) {242 _.strictEqual((new Matrix2D()).scale(2, 3).equals(new Matrix2D(2, 0, 0, 3, 0, 0)),243 true, "(new Matrix2D()).scale(2, 3).equals(new Matrix2D(2, 0, 0, 3, 0, 0))");244 });245 test("setValues()", function(_) {246 _.strictEqual((new Matrix2D()).setValues(1, 2, 3, 4, 5, 6).equals(new Matrix2D(1, 2, 3, 4, 5, 6)),247 true, "(new Matrix2D()).setValues(1, 2, 3, 4, 5, 6).equals(new Matrix2D(1, 2, 3, 4, 5, 6))");248 });249 test("toString()", function(_) {250 _.strictEqual((new Matrix2D()).toString(),251 "Matrix2D([1, 0, 0] [0, 1, 0] [0, 0, 1])", "(new Matrix2D()).toString()");252 });253 test("translate()", function(_) {254 _.strictEqual((new Matrix2D()).translate(1, 2).equals(new Matrix2D(1, 0, 0, 1, 1, 2)),255 true, "(new Matrix2D()).translate(1, 2).equals(new Matrix2D(1, 0, 0, 1, 1, 2))");256 _.strictEqual((new Matrix2D()).translate(new Vector2D(1, 2)).equals(new Matrix2D(1, 0, 0, 1, 1, 2)),257 true, "(new Matrix2D()).translate(new Vector2D(1, 2)).equals(new Matrix2D(1, 0, 0, 1, 1, 2))");258 });...
Using AI Code Generation
1var wptoolbox = require('wptoolbox');2var matrix2D = wptoolbox.matrix2D;3var matrix = matrix2D.create(2,2);4matrix2D.set(matrix, 0, 0, 1);5matrix2D.set(matrix, 0, 1, 2);6matrix2D.set(matrix, 1, 0, 3);7matrix2D.set(matrix, 1, 1, 4);8console.log(matrix2D.get(matrix, 0, 0));9console.log(matrix2D.get(matrix, 0, 1));10console.log(matrix2D.get(matrix, 1, 0));11console.log(matrix2D.get(matrix, 1, 1));12console.log(matrix2D.get(matrix, 2, 0));13console.log(matrix2D.get(matrix, 2, 1));14console.log(matrix2D.get(matrix, 3, 0));15console.log(matrix2D.get(matrix, 3, 1));16console.log(matrix2D.get(matrix, 4, 4));17console.log(matrix2D.get(matrix, 5, 5));18console.log(matrix2D.get(matrix, 6, 6));19console.log(matrix2D.get(matrix, 7, 7));20console.log(matrix2D.get(matrix, 8, 8));21console.log(matrix2D.get(matrix, 9, 9));22console.log(matrix2D.get(matrix, 10, 10));23console.log(matrix2D.get(matrix, 11, 11));24console.log(matrix2D.get(matrix, 12, 12));25console.log(matrix2D.get(matrix, 13, 13));26console.log(matrix2D.get(matrix, 14, 14));27console.log(matrix2D.get(matrix, 15, 15));28console.log(matrix2D.get(matrix, 16, 16));29console.log(matrix2D.get(matrix, 17, 17));30console.log(matrix2D.get(matrix, 18, 18));31console.log(matrix2D.get(matrix, 19, 19));32console.log(matrix2D.get(matrix, 20, 20));33console.log(matrix2D.get(matrix, 21, 21));34console.log(matrix2D.get(matrix, 22, 22));35console.log(matrix2D.get(matrix, 23, 23));36console.log(matrix2D.get
Using AI Code Generation
1var wpt = require('webpagetest');2var options = {3};4 if (err) return console.error(err);5 console.log(data.data.median.firstView.SpeedIndex);6 console.log(data.data.median.firstView.TTFB);7 console.log(data.data.median.firstView.render);8 console.log(data.data.median.firstView.fullyLoaded);9 console.log(data.data.median.firstView.loadTime);10 console.log(data.data.median.firstView.docTime);11 console.log(data.data.median.firstView.domTime);12 console.log(data.data.median.firstView.domElements);13 console.log(data.data.median.firstView.bytesIn);14 console.log(data.data.median.firstView.bytesOut);15 console.log(data.data.median.firstView.bytesInDoc);16 console.log(data.data.median.firstView.requests);17 console.log(data.data.median.firstView.requestsDoc);18 console.log(data.data.median.firstView.responses_200);19 console.log(data.data.median.firstView.responses_404);20 console.log(data.data.median.firstView.responses_other);21 console.log(data.data.median.firstView.result);22 console.log(data.data.median.firstView.testId);23 console.log(data.data
Using AI Code Generation
1var matrix = new wptoolkit.matrix2D();2matrix.create(3,3);3matrix.set(1,1,1);4matrix.set(2,2,2);5matrix.set(3,3,3);6matrix.set(1,2,4);7matrix.set(2,1,4);8matrix.set(1,3,5);9matrix.set(3,1,5);10matrix.set(2,3,6);11matrix.set(3,2,6);12matrix.print();13matrix.printMatrix();14matrix.printMatrix('html');15matrix.printMatrix('csv');
Using AI Code Generation
1var matrix2D = require('../wptoolkit').matrix2D;2var a = matrix2D(2, 3, 4, 5, 6, 7);3var b = matrix2D(2, 3, 4, 5, 6, 7);4var c = matrix2D(2, 3, 4, 5, 6, 7);5var d = matrix2D(2, 3, 4, 5, 6, 7);6var e = matrix2D(2, 3, 4, 5, 6, 7);7var f = matrix2D(2, 3, 4, 5, 6, 7);8var g = matrix2D(2, 3, 4, 5, 6, 7);9var h = matrix2D(2, 3, 4, 5, 6, 7);10var i = matrix2D(2, 3, 4, 5, 6, 7);11var j = matrix2D(2, 3, 4, 5, 6, 7);12var k = matrix2D(2, 3, 4, 5, 6, 7);13var l = matrix2D(2, 3, 4, 5, 6, 7);14var m = matrix2D(2, 3, 4, 5, 6, 7);15var n = matrix2D(2, 3, 4, 5, 6, 7);16var o = matrix2D(2, 3, 4, 5, 6, 7);17var p = matrix2D(2, 3, 4, 5, 6, 7);18var q = matrix2D(2, 3, 4, 5, 6, 7);19var r = matrix2D(2, 3, 4, 5, 6, 7);20var s = matrix2D(2, 3, 4, 5, 6, 7);21var t = matrix2D(
Using AI Code Generation
1var wptMatrix = require('./wptMatrix.js');2var matrix = wptMatrix.matrix2D;3var myMatrix = new matrix(2,2,[[1,2],[3,4]]);4console.log(myMatrix);5console.log(myMatrix.get(1,1));6myMatrix.set(1,1,5);7console.log(myMatrix.get(1,1));8console.log(myMatrix.add(5));9console.log(myMatrix.add(myMatrix));10console.log(myMatrix.subtract(5));11console.log(myMatrix.subtract(myMatrix));12console.log(myMatrix.multiply(5));13console.log(myMatrix.multiply(myMatrix));14console.log(myMatrix.divide(5));15console.log(myMatrix.divide(myMatrix));16console.log(myMatrix.transpose());17console.log(myMatrix.determinant());18console.log(myMatrix.inverse());19console.log(myMatrix.toString());20var matrix2D = function(rows,cols,values){21 var matrix = [];22 var matrix2D = function(rows,cols,values){23 this.rows = rows;24 this.cols = cols;25 this.values = values;26 this.get = function(row,col){27 return this.values[row][col];28 };29 this.set = function(row,col,value){30 this.values[row][col] = value;31 };32 this.add = function(value){33 var result = [];34 if(typeof(value) === 'number'){35 for(var i=0;i<this.rows;i++){36 result[i] = [];37 for(var j=0;j<this.cols;j++){38 result[i][j] = this.values[i][j] + value;39 }40 }41 }else if(value instanceof matrix2D){42 for(var i=0;i<this.rows;i++){43 result[i] = [];44 for(var j=0;j<this.cols;j++){45 result[i][j] = this.values[i][j] + value.values[i][j];46 }47 }48 }49 return result;50 };51 this.subtract = function(value){52 var result = [];53 if(typeof(value) === 'number'){54 for(var i=0;i<this.rows;i++){55 result[i] = [];56 for(var j=0;j<this.cols;j++){57 result[i][j] = this.values[i][j] - value;58 }59 }60 }else if(value instanceof matrix2D){61 for(var i
Using AI Code Generation
1var wpt = require('./index.js');2var wpt2 = require('wpt');3var wpt3 = require('./index.js');4var wpt4 = require('wpt');5var wpt5 = require('./index.js');6var wpt6 = require('wpt');7var wpt7 = require('./index.js');8var wpt8 = require('wpt');9var wpt9 = require('./index.js');10var wpt10 = require('wpt');11var wpt11 = require('./index.js');12var wpt12 = require('wpt');13var wpt13 = require('./index.js');14var wpt14 = require('wpt');15var wpt15 = require('./index.js');16var wpt16 = require('wpt');17var wpt17 = require('./index.js');18var wpt18 = require('wpt');19var wpt19 = require('./index.js');20var wpt20 = require('wpt');21var wpt21 = require('./index.js');22var wpt22 = require('wpt');23var wpt23 = require('./index.js');24var wpt24 = require('wpt');25var wpt25 = require('./index.js');26var wpt26 = require('wpt');27var wpt27 = require('./index.js');28var wpt28 = require('wpt');29var wpt29 = require('./index.js');30var wpt30 = require('wpt');31var wpt31 = require('./index.js');32var wpt32 = require('wpt');33var wpt33 = require('./index.js');34var wpt34 = require('wpt');35var wpt35 = require('./index.js');36var wpt36 = require('wpt');37var wpt37 = require('./index.js');38var wpt38 = require('wpt');39var wpt39 = require('./index.js');40var wpt40 = require('wpt');41var wpt41 = require('./index.js');42var wpt42 = require('wpt');43var wpt43 = require('./index.js');44var wpt44 = require('wpt');45var wpt45 = require('./index.js');46var wpt46 = require('wpt');47var wpt47 = require('./index.js');48var wpt48 = require('wpt');49var wpt49 = require('./index.js
Using AI Code Generation
1var wptools = require('wptools');2});3var wptools = require('wptools');4});5var wptools = require('wptools');6});7var wptools = require('wptools');8});9var wptools = require('wptools');10});11var wptools = require('wptools');12});13var wptools = require('wptools');14});15var wptools = require('wptools');
Using AI Code Generation
1var wpt = require("wpt");2var wpt = new wpt("API_KEY");3var testScript = "TestScript";4var testLocation = "Dulles:Chrome";5var testRuns = 3;6wpt.test(testUrl, testScript, testLocation, testRuns, function(err, data) {7 if (err) console.log(err);8 console.log(data);9});10var wpt = require("wpt");11var wpt = new wpt("API_KEY");12var testScript = "TestScript";13var testLocation = "Dulles:Chrome";14var testRuns = 3;15wpt.test(testUrl, testScript, testLocation, testRuns, function(err, data) {16 if (err) console.log(err);17 console.log(data);18});19var wpt = require("wpt");20var wpt = new wpt("API_KEY");21var testScript = "TestScript";22var testLocation = "Dulles:Chrome";23var testRuns = 3;24wpt.test(testUrl, testScript, testLocation, testRuns, function(err, data) {25 if (err) console.log(err);26 console.log(data);27});28var wpt = require("wpt");29var wpt = new wpt("API_KEY");30var testScript = "TestScript";31var testLocation = "Dulles:Chrome";32var testRuns = 3;33wpt.test(testUrl, testScript, testLocation, testRuns, function(err, data) {34 if (err) console.log(err);35 console.log(data);36});
Using AI Code Generation
1var wpt = require('./wpt.js');2var matrix = wpt.matrix2D(1,2,3,4,5,6);3console.log(matrix);4### wpt.matrix2D.translate(x, y)5var wpt = require('./wpt.js');6var matrix = wpt.matrix2D.translate(5, 6);7console.log(matrix);8### wpt.matrix2D.scale(x, y)9var wpt = require('./wpt.js');10var matrix = wpt.matrix2D.scale(5, 6);11console.log(matrix);12### wpt.matrix2D.rotate(angle)13var wpt = require('./wpt.js');14var matrix = wpt.matrix2D.rotate(90);15console.log(matrix);
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!