2 // System.Drawing.Drawing2D.Matrix.cs
5 // Stefan Maierhofer <sm@cg.tuwien.ac.at>
6 // Dennis Hayes (dennish@Raytek.com)
7 // Duncan Mak (duncan@ximian.com)
8 // Ravindra (rkumar@novell.com)
10 // (C) Ximian, Inc. http://www.ximian.com
11 // Copyright (C) 2004, 2006 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Runtime.InteropServices;
35 namespace System.Drawing.Drawing2D
37 public sealed class Matrix : MarshalByRefObject, IDisposable
39 internal IntPtr nativeMatrix;
42 internal Matrix (IntPtr ptr)
49 Status status = GDIPlus.GdipCreateMatrix (out nativeMatrix);
50 GDIPlus.CheckStatus (status);
53 public Matrix (Rectangle rect, Point[] plgpts)
56 throw new ArgumentNullException ("plgpts");
57 if (plgpts.Length != 3)
58 throw new ArgumentException ("plgpts");
60 Status status = GDIPlus.GdipCreateMatrix3I (ref rect, plgpts, out nativeMatrix);
61 GDIPlus.CheckStatus (status);
64 public Matrix (RectangleF rect, PointF[] plgpts)
67 throw new ArgumentNullException ("plgpts");
68 if (plgpts.Length != 3)
69 throw new ArgumentException ("plgpts");
71 Status status = GDIPlus.GdipCreateMatrix3 (ref rect, plgpts, out nativeMatrix);
72 GDIPlus.CheckStatus (status);
75 public Matrix (float m11, float m12, float m21, float m22, float dx, float dy)
77 Status status = GDIPlus.GdipCreateMatrix2 (m11, m12, m21, m22, dx, dy, out nativeMatrix);
78 GDIPlus.CheckStatus (status);
82 public float[] Elements {
84 float [] retval = new float [6];
85 IntPtr tmp = Marshal.AllocHGlobal (Marshal.SizeOf (typeof (float)) * 6);
87 Status status = GDIPlus.GdipGetMatrixElements (nativeMatrix, tmp);
88 GDIPlus.CheckStatus (status);
89 Marshal.Copy (tmp, retval, 0, 6);
92 Marshal.FreeHGlobal (tmp);
98 public bool IsIdentity {
101 Status status = GDIPlus.GdipIsMatrixIdentity (nativeMatrix, out retval);
102 GDIPlus.CheckStatus (status);
107 public bool IsInvertible {
110 Status status = GDIPlus.GdipIsMatrixInvertible (nativeMatrix, out retval);
111 GDIPlus.CheckStatus (status);
116 public float OffsetX {
118 return this.Elements [4];
122 public float OffsetY {
124 return this.Elements [5];
128 public Matrix Clone()
131 Status status = GDIPlus.GdipCloneMatrix (nativeMatrix, out retval);
132 GDIPlus.CheckStatus (status);
133 return new Matrix (retval);
137 public void Dispose ()
139 if (nativeMatrix != IntPtr.Zero) {
140 Status status = GDIPlus.GdipDeleteMatrix (nativeMatrix);
141 GDIPlus.CheckStatus (status);
142 nativeMatrix = IntPtr.Zero;
145 GC.SuppressFinalize (this);
148 public override bool Equals (object obj)
150 Matrix m = obj as Matrix;
154 Status status = GDIPlus.GdipIsMatrixEqual (nativeMatrix, m.nativeMatrix, out retval);
155 GDIPlus.CheckStatus (status);
167 public override int GetHashCode ()
169 return base.GetHashCode ();
172 public void Invert ()
174 Status status = GDIPlus.GdipInvertMatrix (nativeMatrix);
175 GDIPlus.CheckStatus (status);
178 public void Multiply (Matrix matrix)
180 Multiply (matrix, MatrixOrder.Prepend);
183 public void Multiply (Matrix matrix, MatrixOrder order)
186 throw new ArgumentNullException ("matrix");
188 Status status = GDIPlus.GdipMultiplyMatrix (nativeMatrix, matrix.nativeMatrix, order);
189 GDIPlus.CheckStatus (status);
194 Status status = GDIPlus.GdipSetMatrixElements (nativeMatrix, 1, 0, 0, 1, 0, 0);
195 GDIPlus.CheckStatus (status);
198 public void Rotate (float angle)
200 Rotate (angle, MatrixOrder.Prepend);
203 public void Rotate (float angle, MatrixOrder order)
205 Status status = GDIPlus.GdipRotateMatrix (nativeMatrix, angle, order);
206 GDIPlus.CheckStatus (status);
209 public void RotateAt (float angle, PointF point)
211 RotateAt (angle, point, MatrixOrder.Prepend);
214 public void RotateAt (float angle, PointF point, MatrixOrder order)
216 if ((order < MatrixOrder.Prepend) || (order > MatrixOrder.Append))
217 throw new ArgumentException ("order");
219 angle *= (float) (Math.PI / 180.0); // degrees to radians
220 float cos = (float) Math.Cos (angle);
221 float sin = (float) Math.Sin (angle);
222 float e4 = -point.X * cos + point.Y * sin + point.X;
223 float e5 = -point.X * sin - point.Y * cos + point.Y;
224 float[] m = this.Elements;
228 if (order == MatrixOrder.Prepend)
229 status = GDIPlus.GdipSetMatrixElements (nativeMatrix,
230 cos * m[0] + sin * m[2],
231 cos * m[1] + sin * m[3],
232 -sin * m[0] + cos * m[2],
233 -sin * m[1] + cos * m[3],
234 e4 * m[0] + e5 * m[2] + m[4],
235 e4 * m[1] + e5 * m[3] + m[5]);
237 status = GDIPlus.GdipSetMatrixElements (nativeMatrix,
238 m[0] * cos + m[1] * -sin,
239 m[0] * sin + m[1] * cos,
240 m[2] * cos + m[3] * -sin,
241 m[2] * sin + m[3] * cos,
242 m[4] * cos + m[5] * -sin + e4,
243 m[4] * sin + m[5] * cos + e5);
244 GDIPlus.CheckStatus (status);
247 public void Scale (float scaleX, float scaleY)
249 Scale (scaleX, scaleY, MatrixOrder.Prepend);
252 public void Scale (float scaleX, float scaleY, MatrixOrder order)
254 Status status = GDIPlus.GdipScaleMatrix (nativeMatrix, scaleX, scaleY, order);
255 GDIPlus.CheckStatus (status);
258 public void Shear (float shearX, float shearY)
260 Shear (shearX, shearY, MatrixOrder.Prepend);
263 public void Shear (float shearX, float shearY, MatrixOrder order)
265 Status status = GDIPlus.GdipShearMatrix (nativeMatrix, shearX, shearY, order);
266 GDIPlus.CheckStatus (status);
269 public void TransformPoints (Point[] pts)
272 throw new ArgumentNullException ("pts");
274 Status status = GDIPlus.GdipTransformMatrixPointsI (nativeMatrix, pts, pts.Length);
275 GDIPlus.CheckStatus (status);
278 public void TransformPoints (PointF[] pts)
281 throw new ArgumentNullException ("pts");
283 Status status = GDIPlus.GdipTransformMatrixPoints (nativeMatrix, pts, pts.Length);
284 GDIPlus.CheckStatus (status);
287 public void TransformVectors (Point[] pts)
290 throw new ArgumentNullException ("pts");
292 Status status = GDIPlus.GdipVectorTransformMatrixPointsI (nativeMatrix, pts, pts.Length);
293 GDIPlus.CheckStatus (status);
296 public void TransformVectors (PointF[] pts)
299 throw new ArgumentNullException ("pts");
301 Status status = GDIPlus.GdipVectorTransformMatrixPoints (nativeMatrix, pts, pts.Length);
302 GDIPlus.CheckStatus (status);
305 public void Translate (float offsetX, float offsetY)
307 Translate (offsetX, offsetY, MatrixOrder.Prepend);
310 public void Translate (float offsetX, float offsetY, MatrixOrder order)
312 Status status = GDIPlus.GdipTranslateMatrix (nativeMatrix, offsetX, offsetY, order);
313 GDIPlus.CheckStatus (status);
316 public void VectorTransformPoints (Point[] pts)
318 TransformVectors (pts);
321 internal IntPtr NativeObject
327 nativeMatrix = value;