matrixUtils.js (1845B)
1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- 2 * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ : 3 * This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 /* 8 * Utilities for testing SVG matrices 9 */ 10 11 function createMatrix(a, b, c, d, e, f) { 12 var svg = document.getElementsByTagName("svg")[0]; 13 var m = svg.createSVGMatrix(); 14 m.a = a; 15 m.b = b; 16 m.c = c; 17 m.d = d; 18 m.e = e; 19 m.f = f; 20 return m; 21 } 22 23 // Lightweight dummy Matrix class for representing arrays that get passed in 24 function MatrixFromArray(a) { 25 this.a = a[0]; 26 this.b = a[1]; 27 this.c = a[2]; 28 this.d = a[3]; 29 this.e = a[4]; 30 this.f = a[5]; 31 } 32 33 function cmpMatrix(a, b, msg) { 34 if (a.constructor === Array) { 35 a = new MatrixFromArray(a); 36 } 37 if (b.constructor === Array) { 38 b = new MatrixFromArray(b); 39 } 40 41 ok( 42 a.a == b.a && 43 a.b == b.b && 44 a.c == b.c && 45 a.d == b.d && 46 a.e == b.e && 47 a.f == b.f, 48 msg + " - got " + formatMatrix(a) + ", expected " + formatMatrix(b) 49 ); 50 } 51 52 function roughCmpMatrix(a, b, msg) { 53 if (a.constructor === Array) { 54 a = new MatrixFromArray(a); 55 } 56 if (b.constructor === Array) { 57 b = new MatrixFromArray(b); 58 } 59 60 const tolerance = 1 / 65535; 61 ok( 62 Math.abs(b.a - a.a) < tolerance && 63 Math.abs(b.b - a.b) < tolerance && 64 Math.abs(b.c - a.c) < tolerance && 65 Math.abs(b.d - a.d) < tolerance && 66 Math.abs(b.e - a.e) < tolerance && 67 Math.abs(b.f - a.f) < tolerance, 68 msg + " - got " + formatMatrix(a) + ", expected " + formatMatrix(b) 69 ); 70 } 71 72 function formatMatrix(m) { 73 if (m.constructor != Array) { 74 return "(" + [m.a, m.b, m.c, m.d, m.e, m.f].join(", ") + ")"; 75 } 76 77 return "(" + m.join(", ") + ")"; 78 }