1
+ import { Matrix2D } from '../src/Matrix2D' ;
2
+ import { Vector2D } from '../src/Vector2D' ;
3
+
4
+ test ( 'new Matrix2D' , ( ) => {
5
+ let identity = new Matrix2D ( 1 , 0 , 0 , 1 , 0 , 0 ) ;
6
+ let m = new Matrix2D ( ) ;
7
+ expect ( m ) . toEqual ( identity ) ;
8
+ } ) ;
9
+
10
+ // getter
11
+ // ----------------------
12
+
13
+ test ( 'float32Array' , ( ) => {
14
+ let a = new Matrix2D ( 1 , 2 , 3 , 4 , 5 , 6 ) ;
15
+ expect ( a . float32Array ) . toEqual ( new Float32Array ( [ 1 , 2 , 0 , 3 , 4 , 0 , 5 , 6 , 1 ] ) )
16
+ } ) ;
17
+
18
+ test ( 'determinant' , ( ) => {
19
+ let a = new Matrix2D ( 1 , 2 , 3 , 4 , 5 , 6 )
20
+ expect ( a . determinant ) . toEqual ( - 2 ) ;
21
+ } ) ;
22
+
23
+ // methods
24
+ // --------------------
25
+
26
+ test ( 'identity' , ( ) => {
27
+ let a = new Matrix2D ( 1 , 2 , 3 , 4 , 5 , 6 ) ;
28
+ expect ( a . identity ( ) ) . toEqual ( new Matrix2D ( 1 , 0 , 0 , 1 , 0 , 0 ) ) ;
29
+ } ) ;
30
+
31
+ // scale rotate translate
32
+ // ------------------------
33
+
34
+ test ( 'scale' , ( ) => {
35
+ let a = new Matrix2D ( 1 , 0 , 0 , 1 , 0 , 0 ) ;
36
+
37
+ a . scale ( 2 ) ; // === a.scale(2,2);
38
+ expect ( a . equal ( new Matrix2D ( 2 , 0 , 0 , 2 , 0 , 0 ) ) ) . toBeTruthy ( ) ;
39
+ } ) ;
40
+
41
+ test ( 'rotate' , ( ) => {
42
+ let a = new Matrix2D ( ) . rotate ( 33.3 ) ;
43
+ let b = new Matrix2D ( ) . fromSRT ( 1 , 1 , 33.3 , 0 , 0 ) ;
44
+ expect ( a ) . toEqual ( b ) ;
45
+ } ) ;
46
+
47
+ test ( 'translate' , ( ) => {
48
+ let a = new Matrix2D ( ) ;
49
+ a . translate ( 11 , 22 ) ;
50
+ expect ( a ) . toEqual ( new Matrix2D ( ) . fromSRT ( 1 , 1 , 0 , 11 , 22 ) ) ;
51
+ } ) ;
52
+
53
+ test ( 'fromSRT' , ( ) => {
54
+ let a = new Matrix2D ( ) . fromSRT ( 1 , 1 , 0 , 2 , 3 )
55
+ let b = new Matrix2D ( ) . scale ( 1 , 1 ) . rotate ( 0 ) . translate ( 2 , 3 )
56
+ expect ( a ) . toEqual ( b ) ;
57
+
58
+ a . fromSRT ( 2 , 3 , 4 , 5 , 6 ) ;
59
+ b = new Matrix2D ( ) . scale ( 2 , 3 ) . rotate ( 4 ) . translate ( 5 , 6 ) ;
60
+ expect ( a ) . toEqual ( b ) ;
61
+
62
+ a . fromSRT ( 2 , 3 , 4 , 5 , 6 ) ;
63
+
64
+ } ) ;
65
+
66
+ test ( 'fromTranslation fromRotation fromScale multiply together should equal fromSRT' , ( ) => {
67
+
68
+ let scale = new Matrix2D ( ) . fromScale ( 2 , 3 ) ;
69
+ let rotate = new Matrix2D ( ) . fromRotation ( 128 ) ;
70
+ let translate = new Matrix2D ( ) . fromTranslation ( 7 , 8 ) ;
71
+
72
+ let a = new Matrix2D ( ) . append ( scale ) . append ( rotate ) . append ( translate ) ;
73
+ let b = new Matrix2D ( ) . scale ( 2 , 3 ) . rotate ( 128 ) . translate ( 7 , 8 ) ;
74
+ expect ( a ) . toEqual ( b )
75
+
76
+ let c = new Matrix2D ( ) . fromSRT ( 2 , 3 , 128 , 7 , 8 )
77
+
78
+ expect ( a ) . toEqual ( c )
79
+
80
+ } )
81
+
82
+ test ( 'append should be inverse of prepend' , ( ) => {
83
+ let a = new Matrix2D ( ) . fromSRT ( 2 , 3 , 4 , 5 , 6 ) ;
84
+ // let a = new Matrix2D(1, 2, 3, 4, 5, 6);
85
+
86
+ let b = new Matrix2D ( ) . fromSRT ( 9 , 8 , 7 , 6 , 5 ) ;
87
+ // let b = new Matrix2D(9, 8, 7, 6, 5, 4);
88
+ expect ( a . clone ( ) . append ( b ) ) . toEqual ( b . clone ( ) . prepend ( a ) ) ;
89
+
90
+ } )
91
+
92
+ test ( 'invert' , ( ) => {
93
+
94
+ let a = new Matrix2D ( ) . rotate ( 113 ) . translate ( 3 , 5 ) . scale ( 7 , 8 ) ;
95
+ let inverseA = a . clone ( ) . invert ( ) ;
96
+
97
+ expect ( a . multiply ( inverseA ) . equal ( new Matrix2D ( ) ) ) . toBeTruthy ( ) ;
98
+ } ) ;
99
+
100
+ test ( 'should transformVector works' , ( ) => {
101
+
102
+ let right = new Vector2D ( 1 , 0 ) ;
103
+ let m = new Matrix2D ( ) . rotate ( 90 ) . translate ( 2 , 3 ) ;
104
+ expect ( m . transformVector ( right ) . equal ( new Vector2D ( 0 , 1 ) ) ) . toBeTruthy ( ) ;
105
+
106
+ } ) ;
107
+
108
+ test ( 'should transformVector works' , ( ) => {
109
+ let right = new Vector2D ( 1 , 0 ) ;
110
+ let m = new Matrix2D ( ) . rotate ( 90 ) . translate ( 2 , 3 ) ;
111
+ expect ( m . transformPoint ( right ) . equal ( new Vector2D ( 2 , 4 ) ) ) . toBeTruthy ( ) ;
112
+ } ) ;
113
+
114
+ // common methods
115
+
116
+ test ( 'reset' , ( ) => {
117
+ let a = new Matrix2D ( 1 , 2 , 3 , 4 , 5 , 6 ) ;
118
+ a . reset ( 6 , 5 , 4 , 3 , 2 , 1 ) ;
119
+ expect ( a . equal ( new Matrix2D ( 6 , 5 , 4 , 3 , 2 , 1 ) ) ) . toBeTruthy ( ) ;
120
+ } ) ;
121
+
122
+ test ( 'equal' , ( ) => {
123
+ let a = new Matrix2D ( 1 , 2 , 3 , 4 , 5 , 6 ) ;
124
+ let b = new Matrix2D ( 1 , 2 , 3 , 4 , 5 , 6 ) ;
125
+ let c = new Matrix2D ( 1 , 2 , 3 , 4 , 5 , 7 ) ;
126
+ expect ( a ) . toEqual ( b ) ;
127
+ expect ( a . equal ( c ) ) . toBeFalsy ( ) ;
128
+ } ) ;
129
+
130
+ test ( 'copyFrom ' , ( ) => {
131
+ let a = new Matrix2D ( 1 , 2 , 3 , 4 , 5 , 6 ) ;
132
+ let b = new Matrix2D ( ) ;
133
+ b . copyFrom ( a )
134
+ expect ( a ) . toEqual ( b ) ;
135
+ } ) ;
136
+
137
+ test ( 'clone ' , ( ) => {
138
+ let a = new Matrix2D ( 1 , 2 , 3 , 4 , 5 , 6 ) ;
139
+ let b = a . clone ( ) ;
140
+ expect ( a ) . toEqual ( b ) ;
141
+ } ) ;
142
+
143
+ test ( 'toString ' , ( ) => {
144
+ let a = new Matrix2D ( 1 , 2 , 3 , 4 , 5 , 6 ) ;
145
+ expect ( a . toString ( ) ) . toEqual ( "[Matrix2D](1,2,0,3,4,0,5,6,1)" ) ;
146
+ } ) ;
147
+
148
+ //--------------------
0 commit comments