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)
27 using System.ComponentModel;
28 using System.Globalization;
29 using System.Windows.Markup;
30 using System.Windows.Media.Converters;
31 using System.Windows.Threading;
33 namespace System.Windows.Media {
36 [TypeConverter (typeof(MatrixConverter))]
37 [ValueSerializer (typeof (MatrixValueSerializer))]
38 public struct Matrix : IFormattable {
47 public Matrix (double m11,
58 this._offsetX = offsetX;
59 this._offsetY = offsetY;
62 public void Append (Matrix matrix)
71 _m11 = this._m11 * matrix.M11 + this._m12 * matrix.M21;
72 _m12 = this._m11 * matrix.M12 + this._m12 * matrix.M22;
73 _m21 = this._m21 * matrix.M11 + this._m22 * matrix.M21;
74 _m22 = this._m21 * matrix.M12 + this._m22 * matrix.M22;
76 _offsetX = this._offsetX * matrix.M11 + this._offsetY * matrix.M21 + matrix.OffsetX;
77 _offsetY = this._offsetX * matrix.M12 + this._offsetY * matrix.M22 + matrix.OffsetY;
83 this._offsetX = _offsetX;
84 this._offsetY = _offsetY;
87 public bool Equals (Matrix value)
89 return (_m11 == value.M11 &&
93 _offsetX == value.OffsetX &&
94 _offsetY == value.OffsetY);
97 public override bool Equals (object o)
102 return Equals ((Matrix)o);
105 public static bool Equals (Matrix matrix1,
108 return matrix1.Equals (matrix2);
111 public override int GetHashCode ()
115 var hashCode = _m11.GetHashCode ();
116 hashCode = (hashCode * 397) ^ _m12.GetHashCode ();
117 hashCode = (hashCode * 397) ^ _m21.GetHashCode ();
118 hashCode = (hashCode * 397) ^ _m22.GetHashCode ();
119 hashCode = (hashCode * 397) ^ _offsetX.GetHashCode ();
120 hashCode = (hashCode * 397) ^ _offsetY.GetHashCode ();
125 public void Invert ()
128 throw new InvalidOperationException ("Transform is not invertible.");
130 double d = Determinant;
132 /* 1/(ad-bc)[d -b; -c a] */
134 double _m11 = this._m22;
135 double _m12 = -this._m12;
136 double _m21 = -this._m21;
137 double _m22 = this._m11;
139 double _offsetX = this._m21 * this._offsetY - this._m22 * this._offsetX;
140 double _offsetY = this._m12 * this._offsetX - this._m11 * this._offsetY;
142 this._m11 = _m11 / d;
143 this._m12 = _m12 / d;
144 this._m21 = _m21 / d;
145 this._m22 = _m22 / d;
146 this._offsetX = _offsetX / d;
147 this._offsetY = _offsetY / d;
150 public static Matrix Multiply (Matrix trans1,
158 public static bool operator == (Matrix matrix1,
161 return matrix1.Equals (matrix2);
164 public static bool operator != (Matrix matrix1,
167 return !matrix1.Equals (matrix2);
170 public static Matrix operator * (Matrix trans1,
173 Matrix result = trans1;
174 result.Append (trans2);
178 public static Matrix Parse (string source)
181 throw new ArgumentNullException ("source");
183 if (source.Trim () == "Identity")
189 var parts = source.Split (',');
190 if (parts.Length != 6)
191 throw new FormatException (string.Format ("Invalid Matrix format: {0}", source));
198 if (double.TryParse (parts[0], NumberStyles.Float, CultureInfo.InvariantCulture, out m11)
199 && double.TryParse (parts[1], NumberStyles.Float, CultureInfo.InvariantCulture, out m12)
200 && double.TryParse (parts[2], NumberStyles.Float, CultureInfo.InvariantCulture, out m21)
201 && double.TryParse (parts[3], NumberStyles.Float, CultureInfo.InvariantCulture, out m22)
202 && double.TryParse (parts[4], NumberStyles.Float, CultureInfo.InvariantCulture, out offsetX)
203 && double.TryParse (parts[5], NumberStyles.Float, CultureInfo.InvariantCulture, out offsetY))
205 value = new Matrix (m11, m12, m21, m22, offsetX, offsetY);
209 throw new FormatException (string.Format ("Invalid Matrix format: {0}", source));
215 public void Prepend (Matrix matrix)
224 _m11 = matrix.M11 * this._m11 + matrix.M12 * this._m21;
225 _m12 = matrix.M11 * this._m12 + matrix.M12 * this._m22;
226 _m21 = matrix.M21 * this._m11 + matrix.M22 * this._m21;
227 _m22 = matrix.M21 * this._m12 + matrix.M22 * this._m22;
229 _offsetX = matrix.OffsetX * this._m11 + matrix.OffsetY * this._m21 + this._offsetX;
230 _offsetY = matrix.OffsetX * this._m12 + matrix.OffsetY * this._m22 + this._offsetY;
236 this._offsetX = _offsetX;
237 this._offsetY = _offsetY;
240 public void Rotate (double angle)
242 // R_theta==[costheta -sintheta; sintheta costheta],
243 double theta = angle * Math.PI / 180;
245 Matrix r_theta = new Matrix (Math.Cos (theta), Math.Sin(theta),
246 -Math.Sin (theta), Math.Cos(theta),
252 public void RotateAt (double angle,
256 Translate (-centerX, -centerY);
258 Translate (centerX, centerY);
261 public void RotateAtPrepend (double angle,
265 Matrix m = Matrix.Identity;
266 m.RotateAt (angle, centerX, centerY);
270 public void RotatePrepend (double angle)
272 Matrix m = Matrix.Identity;
277 public void Scale (double scaleX,
280 Matrix scale = new Matrix (scaleX, 0,
287 public void ScaleAt (double scaleX,
292 Translate (-centerX, -centerY);
293 Scale (scaleX, scaleY);
294 Translate (centerX, centerY);
297 public void ScaleAtPrepend (double scaleX,
302 Matrix m = Matrix.Identity;
303 m.ScaleAt (scaleX, scaleY, centerX, centerY);
307 public void ScalePrepend (double scaleX,
310 Matrix m = Matrix.Identity;
311 m.Scale (scaleX, scaleY);
315 public void SetIdentity ()
319 _offsetX = _offsetY = 0.0;
322 public void Skew (double skewX,
325 Matrix skew_m = new Matrix (1, Math.Tan (skewY * Math.PI / 180),
326 Math.Tan (skewX * Math.PI / 180), 1,
331 public void SkewPrepend (double skewX,
334 Matrix m = Matrix.Identity;
335 m.Skew (skewX, skewY);
339 string IFormattable.ToString (string format,
340 IFormatProvider provider)
342 return ToString (provider);
345 public override string ToString ()
347 return ToString (null);
350 public string ToString (IFormatProvider provider)
354 : string.Concat (_m11, ",", _m12, ",", _m21, ",", _m22, ",", _offsetX, ",", _offsetY);
357 public Point Transform (Point point)
359 return Point.Multiply (point, this);
362 public void Transform (Point[] points)
364 for (int i = 0; i < points.Length; i ++)
365 points[i] = Transform (points[i]);
368 public Vector Transform (Vector vector)
370 return Vector.Multiply (vector, this);
373 public void Transform (Vector[] vectors)
375 for (int i = 0; i < vectors.Length; i ++)
376 vectors[i] = Transform (vectors[i]);
379 public void Translate (double offsetX,
382 this._offsetX += offsetX;
383 this._offsetY += offsetY;
386 public void TranslatePrepend (double offsetX,
389 Matrix m = Matrix.Identity;
390 m.Translate (offsetX, offsetY);
394 public double Determinant {
395 get { return _m11 * _m22 - _m12 * _m21; }
398 public bool HasInverse {
399 get { return Determinant != 0; }
402 public static Matrix Identity {
403 get { return new Matrix (1.0, 0.0, 0.0, 1.0, 0.0, 0.0); }
406 public bool IsIdentity {
407 get { return Equals (Matrix.Identity); }
412 set { _m11 = value; }
416 set { _m12 = value; }
420 set { _m21 = value; }
424 set { _m22 = value; }
426 public double OffsetX {
427 get { return _offsetX; }
428 set { _offsetX = value; }
430 public double OffsetY {
431 get { return _offsetY; }
432 set { _offsetY = value; }