1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2007 Novell, Inc. (http://www.novell.com)
23 // Chris Toshok (toshok@ximian.com)
28 using System.Windows.Media;
29 using NUnit.Framework;
31 namespace MonoTests.System.Windows.Media {
34 public class MatrixTest {
35 const double DELTA = 0.000000001d;
37 void CheckMatrix (Matrix expected, Matrix actual)
39 Assert.AreEqual (expected.M11, actual.M11, DELTA);
40 Assert.AreEqual (expected.M12, actual.M12, DELTA);
41 Assert.AreEqual (expected.M21, actual.M21, DELTA);
42 Assert.AreEqual (expected.M22, actual.M22, DELTA);
43 Assert.AreEqual (expected.OffsetX, actual.OffsetX, DELTA);
44 Assert.AreEqual (expected.OffsetY, actual.OffsetY, DELTA);
48 public void TestAccessors ()
50 Matrix m = new Matrix (1, 2, 3, 4, 5, 6);
51 Assert.AreEqual (1, m.M11);
52 Assert.AreEqual (2, m.M12);
53 Assert.AreEqual (3, m.M21);
54 Assert.AreEqual (4, m.M22);
55 Assert.AreEqual (5, m.OffsetX);
56 Assert.AreEqual (6, m.OffsetY);
62 Matrix m = new Matrix (1, 2, 3, 4, 5, 6);
64 CheckMatrix (new Matrix (7, 10, 15, 22, 28, 40), m);
70 Matrix m = new Matrix (1, 2, 3, 4, 5, 6);
71 Assert.IsTrue (m.Equals (new Matrix (1, 2, 3, 4, 5, 6)));
72 Assert.IsFalse (m.Equals (new Matrix (0, 2, 3, 4, 5, 6)));
73 Assert.IsFalse (m.Equals (new Matrix (1, 0, 3, 4, 5, 6)));
74 Assert.IsFalse (m.Equals (new Matrix (1, 2, 0, 4, 5, 6)));
75 Assert.IsFalse (m.Equals (new Matrix (1, 2, 3, 0, 5, 6)));
76 Assert.IsFalse (m.Equals (new Matrix (1, 2, 3, 4, 0, 6)));
77 Assert.IsFalse (m.Equals (new Matrix (1, 2, 3, 4, 5, 0)));
79 Assert.IsFalse (m.Equals (0));
80 Assert.IsTrue (m.Equals ((object)m));
84 public void Determinant ()
86 Assert.AreEqual (0, (new Matrix (2, 2, 2, 2, 0, 0)).Determinant);
87 Assert.AreEqual (-6, (new Matrix (1, 4, 2, 2, 0, 0)).Determinant);
88 Assert.AreEqual (1, (new Matrix (1, 0, 0, 1, 0, 0)).Determinant);
89 Assert.AreEqual (1, (new Matrix (1, 0, 0, 1, 5, 5)).Determinant);
90 Assert.AreEqual (-1, (new Matrix (0, 1, 1, 0, 5, 5)).Determinant);
94 public void HasInverse ()
96 /* same matrices as in Determinant() */
97 Assert.IsFalse ((new Matrix (2, 2, 2, 2, 0, 0)).HasInverse);
98 Assert.IsTrue ((new Matrix (1, 4, 2, 2, 0, 0)).HasInverse);
99 Assert.IsTrue ((new Matrix (1, 0, 0, 1, 0, 0)).HasInverse);
100 Assert.IsTrue ((new Matrix (1, 0, 0, 1, 5, 5)).HasInverse);
101 Assert.IsTrue ((new Matrix (0, 1, 1, 0, 5, 5)).HasInverse);
105 public void IsIdentity ()
107 Assert.IsTrue (Matrix.Identity.IsIdentity);;
108 Assert.IsFalse ((new Matrix (1, 0, 0, 1, 5, 5)).IsIdentity);
109 Assert.IsFalse ((new Matrix (5, 5, 5, 5, 5, 5)).IsIdentity);
113 [ExpectedException (typeof (InvalidOperationException))] // "Transform is not invertible."
114 public void InvertException1 ()
116 Matrix m = new Matrix (2, 2, 2, 2, 0, 0);
121 public void Invert ()
125 m = new Matrix (1, 0, 0, 1, 0, 0);
127 CheckMatrix (new Matrix (1, 0, 0, 1, 0, 0), m);
129 m = new Matrix (1, 0, 0, 1, 5, 5);
131 CheckMatrix (new Matrix (1, 0, 0, 1, -5, -5), m);
133 m = new Matrix (1, 0, 0, 2, 5, 5);
135 CheckMatrix (new Matrix (1, 0, 0, 0.5, -5, -2.5), m);
137 m = new Matrix (0, 2, 4, 0, 5, 5);
139 CheckMatrix (new Matrix (0, 0.25, 0.5, 0, -2.5, -1.25), m);
143 public void Identity ()
145 CheckMatrix (new Matrix (1, 0, 0, 1, 0, 0), Matrix.Identity);
149 public void Multiply ()
151 CheckMatrix (new Matrix (5, 0, 0, 5, 10, 10),
152 Matrix.Multiply (new Matrix (1, 0, 0, 1, 2, 2),
153 new Matrix (5, 0, 0, 5, 0, 0)));
155 CheckMatrix (new Matrix (0, 0, 0, 0, 10, 10),
156 Matrix.Multiply (new Matrix (1, 0, 0, 1, 0, 0),
157 new Matrix (0, 0, 0, 0, 10, 10)));
161 [Category ("NotWorking")]
164 CheckMatrix (Matrix.Identity, Matrix.Parse ("Identity"));
165 CheckMatrix (new Matrix (1, 0, 0, 1, 0, 0), Matrix.Parse ("1, 0, 0, 1, 0, 0"));
166 CheckMatrix (new Matrix (0.1, 0.2, 0.3, 0.4, 0.5, 0.6), Matrix.Parse ("0.1,0.2,0.3,0.4,0.5,0.6"));
167 // XXX what about locales where . and , are switched?
171 public void Prepend ()
173 Matrix m = new Matrix (1, 2, 3, 4, 5, 6);
174 m.Prepend (new Matrix (2, 4, 6, 8, 10, 12));
176 CheckMatrix (new Matrix (14, 20, 30, 44, 51, 74), m);
180 public void Rotate ()
182 Matrix m = Matrix.Identity;
184 CheckMatrix (new Matrix (0.707106781186548,
187 0.707106781186548, 0, 0), m);
189 m = new Matrix (1, 2, 3, 4, 5, 6);
191 CheckMatrix (new Matrix (-0.25060750208463,
196 7.75521858274768), m);
200 public void RotateAt ()
202 Matrix m = new Matrix (1, 2, 3, 4, 5, 6);
203 m.RotateAt (33, 5, 5);
205 CheckMatrix (new Matrix (-0.25060750208463,
210 5.83867056794542), m);
214 public void RotateAtPrepend ()
216 Matrix m = new Matrix (1, 2, 3, 4, 5, 6);
217 m.RotateAtPrepend (33, 5, 5);
219 CheckMatrix (new Matrix (2.47258767299051,
224 5.39349261148701), m);
228 public void RotatePrepend ()
230 Matrix m = new Matrix (1, 2, 3, 4, 5, 6);
231 m.RotatePrepend (33);
233 CheckMatrix (new Matrix (2.47258767299051,
243 Matrix m = Matrix.Identity;
246 CheckMatrix (new Matrix (5, 0, 0, 6, 0, 0), m);
248 m = new Matrix (1, 2, 2, 1, 3, 3);
250 CheckMatrix (new Matrix (5, 10, 10, 5, 15, 15), m);
254 public void ScaleAt ()
256 Matrix m = new Matrix (1, 0, 0, 1, 2, 2);
257 m.ScaleAt (2, 2, 0, 0);
258 CheckMatrix (new Matrix (2, 0, 0, 2, 4, 4), m);
260 m = new Matrix (1, 0, 0, 1, 2, 2);
261 m.ScaleAt (2, 2, 4, 4);
262 CheckMatrix (new Matrix (2, 0, 0, 2, 0, 0), m);
264 m = new Matrix (1, 0, 0, 1, 2, 2);
265 m.ScaleAt (2, 2, 2, 2);
266 CheckMatrix (new Matrix (2, 0, 0, 2, 2, 2), m);
270 public void ScaleAtPrepend ()
272 Matrix m = new Matrix (1, 0, 0, 1, 2, 2);
273 m.ScaleAtPrepend (2, 2, 0, 0);
274 CheckMatrix (new Matrix (2, 0, 0, 2, 2, 2), m);
276 m = new Matrix (1, 0, 0, 1, 2, 2);
277 m.ScaleAtPrepend (2, 2, 4, 4);
278 CheckMatrix (new Matrix (2, 0, 0, 2, -2, -2), m);
280 m = new Matrix (1, 0, 0, 1, 2, 2);
281 m.ScaleAtPrepend (2, 2, 2, 2);
282 CheckMatrix (new Matrix (2, 0, 0, 2, 0, 0), m);
286 public void ScalePrepend ()
288 Matrix m = Matrix.Identity;
290 m.ScalePrepend (5, 6);
291 CheckMatrix (new Matrix (5, 0, 0, 6, 0, 0), m);
293 m = new Matrix (1, 2, 2, 1, 3, 3);
294 m.ScalePrepend (5, 5);
295 CheckMatrix (new Matrix (5, 10, 10, 5, 3, 3), m);
299 public void SetIdentity ()
301 Matrix m = new Matrix (5, 5, 5, 5, 5, 5);
303 CheckMatrix (Matrix.Identity, m);
309 Matrix m = Matrix.Identity;
312 CheckMatrix (new Matrix (1,
317 m = new Matrix (1, 2, 2, 1, 3, 3);
319 CheckMatrix (new Matrix (1.35265396141693,
324 3.80384757729337), m);
328 public void SkewPrepend ()
330 Matrix m = Matrix.Identity;
332 m.SkewPrepend (10, 15);
333 CheckMatrix (new Matrix (1,
338 m = new Matrix (1, 2, 2, 1, 3, 3);
339 m.SkewPrepend (10, 15);
340 CheckMatrix (new Matrix (1.53589838486225,
348 public void ToStringTest ()
350 Matrix m = new Matrix (1, 2, 3, 4, 5, 6);
351 Assert.AreEqual ("1,2,3,4,5,6", m.ToString());
353 Assert.AreEqual ("Identity", m.ToString());
357 public void PointTransform ()
359 Matrix m = new Matrix (2, 0, 0, 2, 4, 4);
361 Point p = new Point (5, 6);
363 Assert.AreEqual (new Point (14, 16), m.Transform (p));
365 Point[] ps = new Point[10];
366 for (int i = 0; i < ps.Length; i ++)
367 ps[i] = new Point (3 * i, 2 * i);
371 for (int i = 0; i < ps.Length; i ++)
372 Assert.AreEqual (m.Transform (new Point (3 * i, 2 * i)), ps[i]);
376 public void VectorTransform ()
378 Matrix m = new Matrix (2, 0, 0, 2, 4, 4);
380 Vector p = new Vector (5, 6);
382 Assert.AreEqual (new Vector (10, 12), m.Transform (p));
384 Vector[] ps = new Vector[10];
385 for (int i = 0; i < ps.Length; i ++)
386 ps[i] = new Vector (3 * i, 2 * i);
390 for (int i = 0; i < ps.Length; i ++)
391 Assert.AreEqual (m.Transform (new Vector (3 * i, 2 * i)), ps[i]);
395 public void Translate ()
397 Matrix m = new Matrix (1, 0, 0, 1, 0, 0);
399 CheckMatrix (new Matrix (1, 0, 0, 1, 5, 5), m);
401 m = new Matrix (2, 0, 0, 2, 0, 0);
403 CheckMatrix (new Matrix (2, 0, 0, 2, 5, 5), m);
407 public void TranslatePrepend ()
409 Matrix m = new Matrix (1, 0, 0, 1, 0, 0);
410 m.TranslatePrepend (5, 5);
411 CheckMatrix (new Matrix (1, 0, 0, 1, 5, 5), m);
413 m = new Matrix (2, 0, 0, 2, 0, 0);
414 m.TranslatePrepend (5, 5);
415 CheckMatrix (new Matrix (2, 0, 0, 2, 10, 10), m);