Matrix.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. //
  2. // System.Drawing.Drawing2D.Matrix.cs
  3. //
  4. // Author:
  5. // Stefan Maierhofer <[email protected]>
  6. // Dennis Hayes ([email protected])
  7. // Duncan Mak ([email protected])
  8. //
  9. // (C) Ximian, Inc. http://www.ximian.com
  10. //
  11. using System;
  12. using System.Drawing;
  13. using System.Runtime.InteropServices;
  14. namespace System.Drawing.Drawing2D
  15. {
  16. public sealed class Matrix : MarshalByRefObject, IDisposable
  17. {
  18. internal IntPtr nativeMatrix;
  19. // constructors
  20. internal Matrix (IntPtr ptr)
  21. {
  22. nativeMatrix = ptr;
  23. }
  24. public Matrix ()
  25. {
  26. Status s = GDIPlus.GdipCreateMatrix (out nativeMatrix);
  27. }
  28. public Matrix (Rectangle rect , Point[] plgpts)
  29. {
  30. GDIPlus.GdipCreateMatrix3I (rect, plgpts, out nativeMatrix);
  31. }
  32. public Matrix (RectangleF rect , PointF[] pa)
  33. {
  34. GDIPlus.GdipCreateMatrix3 (rect, pa, out nativeMatrix);
  35. }
  36. public Matrix (float m11, float m12, float m21, float m22, float dx, float dy)
  37. {
  38. GDIPlus.GdipCreateMatrix2 (m11, m12, m21, m22, dx, dy, out nativeMatrix);
  39. }
  40. // properties
  41. public float[] Elements {
  42. get {
  43. IntPtr tmp = Marshal.AllocHGlobal (Marshal.SizeOf (typeof (float)) * 6);
  44. float [] retval = new float [6];
  45. Status s = GDIPlus.GdipGetMatrixElements (nativeMatrix, tmp);
  46. Marshal.Copy (tmp, retval, 0, 6);
  47. Marshal.FreeHGlobal (tmp);
  48. return retval;
  49. }
  50. }
  51. public bool IsIdentity {
  52. get {
  53. bool retval;
  54. GDIPlus.GdipIsMatrixIdentity (nativeMatrix, out retval);
  55. return retval;
  56. }
  57. }
  58. public bool IsInvertible {
  59. get {
  60. bool retval;
  61. GDIPlus.GdipIsMatrixInvertible (nativeMatrix, out retval);
  62. return retval;
  63. }
  64. }
  65. public float OffsetX {
  66. get {
  67. return this.Elements [4];
  68. }
  69. }
  70. public float OffsetY {
  71. get {
  72. return this.Elements [5];
  73. }
  74. }
  75. public Matrix Clone()
  76. {
  77. IntPtr retval;
  78. Status s = GDIPlus.GdipCloneMatrix (nativeMatrix, out retval);
  79. return new Matrix (retval);
  80. }
  81. public void Dispose ()
  82. {
  83. GDIPlus.GdipDeleteMatrix (nativeMatrix);
  84. }
  85. public override bool Equals (object obj)
  86. {
  87. Matrix m = obj as Matrix;
  88. if (m != null) {
  89. bool retval;
  90. GDIPlus.GdipIsMatrixEqual (nativeMatrix, m.nativeMatrix, out retval);
  91. return retval;
  92. } else
  93. return false;
  94. }
  95. ~Matrix()
  96. {
  97. Dispose ();
  98. }
  99. public override int GetHashCode ()
  100. {
  101. return base.GetHashCode ();
  102. }
  103. public void Invert ()
  104. {
  105. GDIPlus.GdipInvertMatrix (nativeMatrix);
  106. }
  107. public void Multiply (Matrix matrix)
  108. {
  109. Multiply (matrix, MatrixOrder.Prepend);
  110. }
  111. public void Multiply (Matrix matrix, MatrixOrder order)
  112. {
  113. GDIPlus.GdipMultiplyMatrix (nativeMatrix, matrix.nativeMatrix, order);
  114. }
  115. public void Reset()
  116. {
  117. GDIPlus.GdipSetMatrixElements (nativeMatrix, 1, 0, 0, 1, 0, 0);
  118. }
  119. public void Rotate (float angle)
  120. {
  121. Rotate (angle, MatrixOrder.Prepend);
  122. }
  123. public void Rotate (float angle, MatrixOrder order)
  124. {
  125. GDIPlus.GdipRotateMatrix (nativeMatrix, angle, order);
  126. }
  127. public void RotateAt (float angle, PointF point)
  128. {
  129. RotateAt (angle, point, MatrixOrder.Prepend);
  130. }
  131. public void RotateAt (float angle, PointF point, MatrixOrder order)
  132. {
  133. angle *= (float) (Math.PI / 180.0); // degrees to radians
  134. float cos = (float) Math.Cos (angle);
  135. float sin = (float) Math.Sin (angle);
  136. float e4 = -point.X * cos + point.Y * sin + point.X;
  137. float e5 = -point.X * sin - point.Y * cos + point.Y;
  138. float[] m = this.Elements;
  139. if (order == MatrixOrder.Prepend)
  140. GDIPlus.GdipSetMatrixElements (nativeMatrix,
  141. cos * m[0] + sin * m[2],
  142. cos * m[1] + sin * m[3],
  143. -sin * m[0] + cos * m[2],
  144. -sin * m[1] + cos * m[3],
  145. e4 * m[0] + e5 * m[2] + m[4],
  146. e4 * m[1] + e5 * m[3] + m[5]);
  147. else
  148. GDIPlus.GdipSetMatrixElements (nativeMatrix,
  149. m[0] * cos + m[1] * -sin,
  150. m[0] * sin + m[1] * cos,
  151. m[2] * cos + m[3] * -sin,
  152. m[2] * sin + m[3] * cos,
  153. m[4] * cos + m[5] * -sin + e4,
  154. m[4] * sin + m[5] * cos + e5);
  155. }
  156. public void Scale (float scaleX, float scaleY)
  157. {
  158. Scale (scaleX, scaleY, MatrixOrder.Prepend);
  159. }
  160. public void Scale (float scaleX, float scaleY, MatrixOrder order)
  161. {
  162. GDIPlus.GdipScaleMatrix (nativeMatrix, scaleX, scaleY, order);
  163. }
  164. public void Shear (float shearX, float shearY)
  165. {
  166. Shear (shearX, shearY, MatrixOrder.Prepend);
  167. }
  168. public void Shear (float shearX, float shearY, MatrixOrder order)
  169. {
  170. GDIPlus.GdipShearMatrix (nativeMatrix, shearX, shearY, order);
  171. }
  172. public void TransformPoints (Point[] pts)
  173. {
  174. GDIPlus.GdipTransformMatrixPointsI (nativeMatrix, pts, pts.Length);
  175. }
  176. public void TransformPoints (PointF[] pts)
  177. {
  178. GDIPlus.GdipTransformMatrixPoints (nativeMatrix, pts, pts.Length);
  179. }
  180. public void TransformVectors (Point[] pts)
  181. {
  182. GDIPlus.GdipVectorTransformMatrixPointsI (nativeMatrix, pts, pts.Length);
  183. }
  184. public void TransformVectors (PointF[] pts)
  185. {
  186. GDIPlus.GdipVectorTransformMatrixPoints (nativeMatrix, pts, pts.Length);
  187. }
  188. public void Translate (float offsetX, float offsetY)
  189. {
  190. Translate (offsetX, offsetY, MatrixOrder.Prepend);
  191. }
  192. public void Translate (float offsetX, float offsetY, MatrixOrder order)
  193. {
  194. GDIPlus.GdipTranslateMatrix (nativeMatrix, offsetX, offsetY, order);
  195. }
  196. public void VectorTransformPoints (Point[] pts)
  197. {
  198. TransformVectors (pts);
  199. }
  200. }
  201. }