Matrix.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. //
  2. // System.Drawing.Drawing2D.Matrix.cs
  3. //
  4. // Authors:
  5. // Stefan Maierhofer <[email protected]>
  6. // Dennis Hayes ([email protected])
  7. // Duncan Mak ([email protected])
  8. // Ravindra ([email protected])
  9. //
  10. // (C) Ximian, Inc. http://www.ximian.com
  11. // (C) Novell, Inc. http://www.novell.com
  12. //
  13. //
  14. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  15. //
  16. // Permission is hereby granted, free of charge, to any person obtaining
  17. // a copy of this software and associated documentation files (the
  18. // "Software"), to deal in the Software without restriction, including
  19. // without limitation the rights to use, copy, modify, merge, publish,
  20. // distribute, sublicense, and/or sell copies of the Software, and to
  21. // permit persons to whom the Software is furnished to do so, subject to
  22. // the following conditions:
  23. //
  24. // The above copyright notice and this permission notice shall be
  25. // included in all copies or substantial portions of the Software.
  26. //
  27. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  28. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  29. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  30. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  31. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  32. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  33. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  34. //
  35. using System;
  36. using System.Drawing;
  37. using System.Runtime.InteropServices;
  38. namespace System.Drawing.Drawing2D
  39. {
  40. public sealed class Matrix : MarshalByRefObject, IDisposable
  41. {
  42. internal IntPtr nativeMatrix;
  43. // constructors
  44. internal Matrix (IntPtr ptr)
  45. {
  46. nativeMatrix = ptr;
  47. }
  48. public Matrix ()
  49. {
  50. Status status = GDIPlus.GdipCreateMatrix (out nativeMatrix);
  51. GDIPlus.CheckStatus (status);
  52. }
  53. public Matrix (Rectangle rect , Point[] plgpts)
  54. {
  55. Status status = GDIPlus.GdipCreateMatrix3I (rect, plgpts, out nativeMatrix);
  56. GDIPlus.CheckStatus (status);
  57. }
  58. public Matrix (RectangleF rect , PointF[] pa)
  59. {
  60. Status status = GDIPlus.GdipCreateMatrix3 (rect, pa, out nativeMatrix);
  61. GDIPlus.CheckStatus (status);
  62. }
  63. public Matrix (float m11, float m12, float m21, float m22, float dx, float dy)
  64. {
  65. Status status = GDIPlus.GdipCreateMatrix2 (m11, m12, m21, m22, dx, dy, out nativeMatrix);
  66. GDIPlus.CheckStatus (status);
  67. }
  68. // properties
  69. public float[] Elements {
  70. get {
  71. IntPtr tmp = Marshal.AllocHGlobal (Marshal.SizeOf (typeof (float)) * 6);
  72. float [] retval = new float [6];
  73. Status status = GDIPlus.GdipGetMatrixElements (nativeMatrix, tmp);
  74. GDIPlus.CheckStatus (status);
  75. Marshal.Copy (tmp, retval, 0, 6);
  76. Marshal.FreeHGlobal (tmp);
  77. return retval;
  78. }
  79. }
  80. public bool IsIdentity {
  81. get {
  82. bool retval;
  83. Status status = GDIPlus.GdipIsMatrixIdentity (nativeMatrix, out retval);
  84. GDIPlus.CheckStatus (status);
  85. return retval;
  86. }
  87. }
  88. public bool IsInvertible {
  89. get {
  90. bool retval;
  91. Status status = GDIPlus.GdipIsMatrixInvertible (nativeMatrix, out retval);
  92. GDIPlus.CheckStatus (status);
  93. return retval;
  94. }
  95. }
  96. public float OffsetX {
  97. get {
  98. return this.Elements [4];
  99. }
  100. }
  101. public float OffsetY {
  102. get {
  103. return this.Elements [5];
  104. }
  105. }
  106. public Matrix Clone()
  107. {
  108. IntPtr retval;
  109. Status status = GDIPlus.GdipCloneMatrix (nativeMatrix, out retval);
  110. GDIPlus.CheckStatus (status);
  111. return new Matrix (retval);
  112. }
  113. public void Dispose ()
  114. {
  115. if (nativeMatrix != IntPtr.Zero) {
  116. Status status = GDIPlus.GdipDeleteMatrix (nativeMatrix);
  117. GDIPlus.CheckStatus (status);
  118. nativeMatrix = IntPtr.Zero;
  119. }
  120. GC.SuppressFinalize (this);
  121. }
  122. public override bool Equals (object obj)
  123. {
  124. Matrix m = obj as Matrix;
  125. if (m != null) {
  126. bool retval;
  127. Status status = GDIPlus.GdipIsMatrixEqual (nativeMatrix, m.nativeMatrix, out retval);
  128. GDIPlus.CheckStatus (status);
  129. return retval;
  130. } else
  131. return false;
  132. }
  133. ~Matrix()
  134. {
  135. Dispose ();
  136. }
  137. public override int GetHashCode ()
  138. {
  139. return base.GetHashCode ();
  140. }
  141. public void Invert ()
  142. {
  143. Status status = GDIPlus.GdipInvertMatrix (nativeMatrix);
  144. GDIPlus.CheckStatus (status);
  145. }
  146. public void Multiply (Matrix matrix)
  147. {
  148. Multiply (matrix, MatrixOrder.Prepend);
  149. }
  150. public void Multiply (Matrix matrix, MatrixOrder order)
  151. {
  152. Status status = GDIPlus.GdipMultiplyMatrix (nativeMatrix, matrix.nativeMatrix, order);
  153. GDIPlus.CheckStatus (status);
  154. }
  155. public void Reset()
  156. {
  157. Status status = GDIPlus.GdipSetMatrixElements (nativeMatrix, 1, 0, 0, 1, 0, 0);
  158. GDIPlus.CheckStatus (status);
  159. }
  160. public void Rotate (float angle)
  161. {
  162. Rotate (angle, MatrixOrder.Prepend);
  163. }
  164. public void Rotate (float angle, MatrixOrder order)
  165. {
  166. Status status = GDIPlus.GdipRotateMatrix (nativeMatrix, angle, order);
  167. GDIPlus.CheckStatus (status);
  168. }
  169. public void RotateAt (float angle, PointF point)
  170. {
  171. RotateAt (angle, point, MatrixOrder.Prepend);
  172. }
  173. public void RotateAt (float angle, PointF point, MatrixOrder order)
  174. {
  175. angle *= (float) (Math.PI / 180.0); // degrees to radians
  176. float cos = (float) Math.Cos (angle);
  177. float sin = (float) Math.Sin (angle);
  178. float e4 = -point.X * cos + point.Y * sin + point.X;
  179. float e5 = -point.X * sin - point.Y * cos + point.Y;
  180. float[] m = this.Elements;
  181. Status status;
  182. if (order == MatrixOrder.Prepend)
  183. status = GDIPlus.GdipSetMatrixElements (nativeMatrix,
  184. cos * m[0] + sin * m[2],
  185. cos * m[1] + sin * m[3],
  186. -sin * m[0] + cos * m[2],
  187. -sin * m[1] + cos * m[3],
  188. e4 * m[0] + e5 * m[2] + m[4],
  189. e4 * m[1] + e5 * m[3] + m[5]);
  190. else
  191. status = GDIPlus.GdipSetMatrixElements (nativeMatrix,
  192. m[0] * cos + m[1] * -sin,
  193. m[0] * sin + m[1] * cos,
  194. m[2] * cos + m[3] * -sin,
  195. m[2] * sin + m[3] * cos,
  196. m[4] * cos + m[5] * -sin + e4,
  197. m[4] * sin + m[5] * cos + e5);
  198. GDIPlus.CheckStatus (status);
  199. }
  200. public void Scale (float scaleX, float scaleY)
  201. {
  202. Scale (scaleX, scaleY, MatrixOrder.Prepend);
  203. }
  204. public void Scale (float scaleX, float scaleY, MatrixOrder order)
  205. {
  206. Status status = GDIPlus.GdipScaleMatrix (nativeMatrix, scaleX, scaleY, order);
  207. GDIPlus.CheckStatus (status);
  208. }
  209. public void Shear (float shearX, float shearY)
  210. {
  211. Shear (shearX, shearY, MatrixOrder.Prepend);
  212. }
  213. public void Shear (float shearX, float shearY, MatrixOrder order)
  214. {
  215. Status status = GDIPlus.GdipShearMatrix (nativeMatrix, shearX, shearY, order);
  216. GDIPlus.CheckStatus (status);
  217. }
  218. public void TransformPoints (Point[] pts)
  219. {
  220. Status status = GDIPlus.GdipTransformMatrixPointsI (nativeMatrix, pts, pts.Length);
  221. GDIPlus.CheckStatus (status);
  222. }
  223. public void TransformPoints (PointF[] pts)
  224. {
  225. Status status = GDIPlus.GdipTransformMatrixPoints (nativeMatrix, pts, pts.Length);
  226. GDIPlus.CheckStatus (status);
  227. }
  228. public void TransformVectors (Point[] pts)
  229. {
  230. Status status = GDIPlus.GdipVectorTransformMatrixPointsI (nativeMatrix, pts, pts.Length);
  231. GDIPlus.CheckStatus (status);
  232. }
  233. public void TransformVectors (PointF[] pts)
  234. {
  235. Status status = GDIPlus.GdipVectorTransformMatrixPoints (nativeMatrix, pts, pts.Length);
  236. GDIPlus.CheckStatus (status);
  237. }
  238. public void Translate (float offsetX, float offsetY)
  239. {
  240. Translate (offsetX, offsetY, MatrixOrder.Prepend);
  241. }
  242. public void Translate (float offsetX, float offsetY, MatrixOrder order)
  243. {
  244. Status status = GDIPlus.GdipTranslateMatrix (nativeMatrix, offsetX, offsetY, order);
  245. GDIPlus.CheckStatus (status);
  246. }
  247. public void VectorTransformPoints (Point[] pts)
  248. {
  249. TransformVectors (pts);
  250. }
  251. internal IntPtr NativeObject
  252. {
  253. get{
  254. return nativeMatrix;
  255. }
  256. set {
  257. nativeMatrix = value;
  258. }
  259. }
  260. }
  261. }