Merge pull request #664 from symform/xbuild-AssignProjectConfiguration
[mono.git] / mcs / class / Mono.Cairo / Mono.Cairo / Matrix.cs
1 //
2 // Mono.Cairo.Matrix.cs
3 //
4 // Author: Duncan Mak
5 //         Hisham Mardam Bey (hisham.mardambey@gmail.com)
6 //         Idan Gazit (idan@fastmail.fm)
7 //
8 // (C) Ximian Inc, 2003 - 2005.
9 //
10 // This is an OO wrapper API for the Cairo API
11 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 //
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:
21 //
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 //
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.
32 //
33
34 using System;
35 using System.Runtime.InteropServices;
36
37 namespace Cairo {
38
39         [StructLayout(LayoutKind.Sequential)]
40         public class Matrix : ICloneable
41         {
42                 public double Xx;
43                 public double Yx;
44                 public double Xy;
45                 public double Yy;
46                 public double X0;
47                 public double Y0;
48
49                 public Matrix (double xx, double yx, double xy, double yy,
50                                 double x0, double y0)
51                 {
52                         this.Xx = xx; this.Yx = yx; this.Xy = xy;
53                         this.Yy = yy; this.X0 = x0; this.Y0 = y0;
54                 }
55
56                 public Matrix ()
57                 {
58                         this.InitIdentity ();
59                 }
60
61                 public bool IsIdentity ()
62                 {
63                         return (this == new Matrix ());
64                 }
65
66                 public void InitIdentity ()
67                 {
68                         // this.Init(1,0,0,1,0,0);
69                         NativeMethods.cairo_matrix_init_identity (this);
70                 }
71
72                 public void Init (double xx, double yx, double xy, double yy,
73                                   double x0, double y0)
74                 {
75                         this.Xx = xx; this.Yx = yx; this.Xy = xy;
76                         this.Yy = yy; this.X0 = x0; this.Y0 = y0;
77                 }
78
79                 public void InitTranslate (double tx, double ty)
80                 {
81                         //this.Init (1, 0, 0, 1, tx, ty);
82                         NativeMethods.cairo_matrix_init_translate (this, tx, ty);
83                 }
84
85                 public void Translate (double tx, double ty)
86                 {
87                         NativeMethods.cairo_matrix_translate (this, tx, ty);
88                 }
89
90                 public void InitScale (double sx, double sy)
91                 {
92                         //this.Init (sx, 0, 0, sy, 0, 0);
93                         NativeMethods.cairo_matrix_init_scale (this, sx, sy);
94                 }
95
96                 public void Scale (double sx, double sy)
97                 {
98                         NativeMethods.cairo_matrix_scale (this, sx, sy);
99                 }
100
101                 public void InitRotate (double radians)
102                 {
103                         /*
104                         double s, c;
105                         s = Math.Sin (radians);
106                         c = Math.Cos (radians);
107                         this.Init (c, s, -s, c, 0, 0);
108                         */
109                         NativeMethods.cairo_matrix_init_rotate (this, radians);
110                 }
111
112                 public void Rotate (double radians)
113                 {
114                         NativeMethods.cairo_matrix_rotate (this, radians);
115                 }
116
117                 public Cairo.Status Invert ()
118                 {
119                         return NativeMethods.cairo_matrix_invert (this);
120                 }
121
122                 public void Multiply (Matrix b)
123                 {
124                         Matrix a = (Matrix) this.Clone ();
125                         NativeMethods.cairo_matrix_multiply (this, a, b);
126                 }
127
128                 public static Matrix Multiply (Matrix a, Matrix b) {
129                         Matrix result = new Matrix ();
130                         NativeMethods.cairo_matrix_multiply (result, a, b);
131                         return result;
132                 }
133
134
135                 public void TransformDistance (ref double dx, ref double dy)
136                 {
137                         NativeMethods.cairo_matrix_transform_distance (this, ref dx, ref dy);
138                 }
139
140                 public void TransformPoint (ref double x, ref double y)
141                 {
142                         NativeMethods.cairo_matrix_transform_point (this, ref x, ref y);
143                 }
144
145                 public override String ToString ()
146                 {
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);
149                         return s;
150                 }
151
152                 public static bool operator == (Matrix lhs, Matrix rhs)
153                 {
154                         return (lhs.Xx == rhs.Xx &&
155                                 lhs.Xy == rhs.Xy &&
156                                 lhs.Yx == rhs.Yx &&
157                                 lhs.Yy == rhs.Yy &&
158                                 lhs.X0 == rhs.X0 &&
159                                 lhs.Y0 == rhs.Y0 );
160                 }
161
162                 public static bool operator != (Matrix lhs, Matrix rhs)
163                 {
164                         return !(lhs==rhs);
165                 }
166
167
168
169                 public override bool Equals(object o)
170                 {
171                         if (! (o is Matrix))
172                                 return false;
173                         else
174                                 return (this == (Matrix) o);
175                 }
176
177                 public override int GetHashCode()
178                 {
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;
185                 }
186
187                 public object Clone()
188                 {
189                         return this.MemberwiseClone ();
190                 }
191
192         }
193 }