xyc
2025-02-21 664db98c9e8595ce4dd636a27f480e3a08b81ff5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<!DOCTYPE html>
<html>
<head>
  <link rel='stylesheet' href='qunit/qunit.css' type='text/css'/>
  <script src='../editor/lib/jquery.js'></script>
  <script type='text/javascript' src='../editor/src/math.js'></script>
  <script type='text/javascript' src='qunit/qunit.js'></script>
  <script type='text/javascript'>
  $(function() {
      // log function
      QUnit.log = function(result, message) {
        if (window.console && window.console.log) {
            window.console.log(result +' :: '+ message);
        }
    };
 
    var svgns = 'http://www.w3.org/2000/svg';
    var svg = document.createElementNS(svgns, 'svg');
 
    module('svgedit.math');
 
    test('Test svgedit.math package', function() {
        expect(7);
 
        ok(svgedit.math);
        ok(svgedit.math.transformPoint);
        ok(svgedit.math.isIdentity);
        ok(svgedit.math.matrixMultiply);
        equals(typeof svgedit.math.transformPoint, typeof function(){});
        equals(typeof svgedit.math.isIdentity, typeof function(){});
        equals(typeof svgedit.math.matrixMultiply, typeof function(){});
    });
 
    test('Test svgedit.math.transformPoint() function', function() {
        expect(6);
        var transformPoint = svgedit.math.transformPoint;
 
        var m = svg.createSVGMatrix();
        m.a = 1; m.b = 0;
        m.c = 0; m.d = 1;
        m.e = 0; m.f = 0;
        var pt = transformPoint(100, 200, m);
        equals(pt.x, 100);
        equals(pt.y, 200);
 
        m.e = 300; m.f = 400;
        pt = transformPoint(100, 200, m);
        equals(pt.x, 400);
        equals(pt.y, 600);
 
        m.a = 0.5; m.b = 0.75;
        m.c = 1.25; m.d = 2;
        pt = transformPoint(100, 200, m);
        equals(pt.x, 100 * m.a + 200 * m.c + m.e);
        equals(pt.y, 100 * m.b + 200 * m.d + m.f);
    });
 
    test('Test svgedit.math.isIdentity() function', function() {
        expect(2);
 
        ok(svgedit.math.isIdentity(svg.createSVGMatrix()));
 
        var m = svg.createSVGMatrix();
        m.a = 1; m.b = 0;
        m.c = 0; m.d = 1;
        m.e = 0; m.f = 0;
        ok(svgedit.math.isIdentity(m));
    });
 
    test('Test svgedit.math.matrixMultiply() function', function() {
        expect(5);
        var mult = svgedit.math.matrixMultiply;
        var isIdentity = svgedit.math.isIdentity;
 
        // translate there and back
        var tr_1 = svg.createSVGMatrix().translate(100,50),
            tr_2 = svg.createSVGMatrix().translate(-90,0),
            tr_3 = svg.createSVGMatrix().translate(-10,-50),
            I = mult(tr_1,tr_2,tr_3);
        ok(isIdentity(I), 'Expected identity matrix when translating there and back');
 
        // rotate there and back
        // TODO: currently Mozilla fails this when rotating back at -50 and then -40 degrees
        // (b and c are *almost* zero, but not zero)
        var rot_there = svg.createSVGMatrix().rotate(90),
            rot_back = svg.createSVGMatrix().rotate(-90); // TODO: set this to -50
            rot_back_more = svg.createSVGMatrix().rotate(0); // TODO: set this to -40
        I = mult(rot_there, rot_back, rot_back_more);
        ok(isIdentity(I), 'Expected identity matrix when rotating there and back');
 
        // scale up and down
        var scale_up = svg.createSVGMatrix().scale(4),
            scale_down = svg.createSVGMatrix().scaleNonUniform(0.25,1);
            scale_down_more = svg.createSVGMatrix().scaleNonUniform(1,0.25);
        I = mult(scale_up, scale_down, scale_down_more);
        ok(isIdentity(I), 'Expected identity matrix when scaling up and down');
 
        // test multiplication with its inverse
        I = mult(rot_there, rot_there.inverse());
        ok(isIdentity(I), 'Expected identity matrix when multiplying a matrix by its inverse');
        I = mult(rot_there.inverse(), rot_there);
        ok(isIdentity(I), 'Expected identity matrix when multiplying a matrix by its inverse');
    });
  });
  </script>
</head>
<body>
  <h1 id='qunit-header'>Unit Tests for math.js</h1>
  <h2 id='qunit-banner'></h2>
  <h2 id='qunit-userAgent'></h2>
  <ol id='qunit-tests'>
  </ol>
</body>
</html>