2 // Mono.Cairo.Matrix.cs
5 // Hisham Mardam Bey (hisham.mardambey@gmail.com)
6 // Idan Gazit (idan@fastmail.fm)
8 // (C) Ximian Inc, 2003 - 2005.
10 // This is an OO wrapper API for the Cairo API
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System.Runtime.InteropServices;
39 [StructLayout(LayoutKind.Sequential)]
40 public class Matrix : ICloneable
49 public Matrix (double xx, double yx, double xy, double yy,
52 this.Xx = xx; this.Yx = yx; this.Xy = xy;
53 this.Yy = yy; this.X0 = x0; this.Y0 = y0;
61 public bool IsIdentity ()
63 return (this == new Matrix ());
66 public void InitIdentity ()
68 // this.Init(1,0,0,1,0,0);
69 NativeMethods.cairo_matrix_init_identity (this);
72 public void Init (double xx, double yx, double xy, double yy,
75 this.Xx = xx; this.Yx = yx; this.Xy = xy;
76 this.Yy = yy; this.X0 = x0; this.Y0 = y0;
79 public void InitTranslate (double tx, double ty)
81 //this.Init (1, 0, 0, 1, tx, ty);
82 NativeMethods.cairo_matrix_init_translate (this, tx, ty);
85 public void Translate (double tx, double ty)
87 NativeMethods.cairo_matrix_translate (this, tx, ty);
90 public void InitScale (double sx, double sy)
92 //this.Init (sx, 0, 0, sy, 0, 0);
93 NativeMethods.cairo_matrix_init_scale (this, sx, sy);
96 public void Scale (double sx, double sy)
98 NativeMethods.cairo_matrix_scale (this, sx, sy);
101 public void InitRotate (double radians)
105 s = Math.Sin (radians);
106 c = Math.Cos (radians);
107 this.Init (c, s, -s, c, 0, 0);
109 NativeMethods.cairo_matrix_init_rotate (this, radians);
112 public void Rotate (double radians)
114 NativeMethods.cairo_matrix_rotate (this, radians);
117 public Cairo.Status Invert ()
119 return NativeMethods.cairo_matrix_invert (this);
122 public void Multiply (Matrix b)
124 Matrix a = (Matrix) this.Clone ();
125 NativeMethods.cairo_matrix_multiply (this, a, b);
128 public static Matrix Multiply (Matrix a, Matrix b) {
129 Matrix result = new Matrix ();
130 NativeMethods.cairo_matrix_multiply (result, a, b);
135 public void TransformDistance (ref double dx, ref double dy)
137 NativeMethods.cairo_matrix_transform_distance (this, ref dx, ref dy);
140 public void TransformPoint (ref double x, ref double y)
142 NativeMethods.cairo_matrix_transform_point (this, ref x, ref y);
145 public override String ToString ()
147 String s = String.Format ("xx:{0:##0.0#} yx:{1:##0.0#} xy:{2:##0.0#} yy:{3:##0.0#} x0:{4:##0.0#} y0:{5:##0.0#}",
148 this.Xx, this.Yx, this.Xy, this.Yy, this.X0, this.Y0);
152 public static bool operator == (Matrix lhs, Matrix rhs)
154 return (lhs.Xx == rhs.Xx &&
162 public static bool operator != (Matrix lhs, Matrix rhs)
169 public override bool Equals(object o)
174 return (this == (Matrix) o);
177 public override int GetHashCode()
179 return (int)this.Xx ^ (int)this.Xx>>32 ^
180 (int)this.Xy ^ (int)this.Xy>>32 ^
181 (int)this.Yx ^ (int)this.Yx>>32 ^
182 (int)this.Yy ^ (int)this.Yy>>32 ^
183 (int)this.X0 ^ (int)this.X0>>32 ^
184 (int)this.Y0 ^ (int)this.Y0>>32;
187 public object Clone()
189 return this.MemberwiseClone ();