Matrix.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. Status status = GDIPlus.GdipDeleteMatrix (nativeMatrix);
  116. GDIPlus.CheckStatus (status);
  117. }
  118. public override bool Equals (object obj)
  119. {
  120. Matrix m = obj as Matrix;
  121. if (m != null) {
  122. bool retval;
  123. Status status = GDIPlus.GdipIsMatrixEqual (nativeMatrix, m.nativeMatrix, out retval);
  124. GDIPlus.CheckStatus (status);
  125. return retval;
  126. } else
  127. return false;
  128. }
  129. ~Matrix()
  130. {
  131. Dispose ();
  132. }
  133. public override int GetHashCode ()
  134. {
  135. return base.GetHashCode ();
  136. }
  137. public void Invert ()
  138. {
  139. Status status = GDIPlus.GdipInvertMatrix (nativeMatrix);
  140. GDIPlus.CheckStatus (status);
  141. }
  142. public void Multiply (Matrix matrix)
  143. {
  144. Multiply (matrix, MatrixOrder.Prepend);
  145. }
  146. public void Multiply (Matrix matrix, MatrixOrder order)
  147. {
  148. Status status = GDIPlus.GdipMultiplyMatrix (nativeMatrix, matrix.nativeMatrix, order);
  149. GDIPlus.CheckStatus (status);
  150. }
  151. public void Reset()
  152. {
  153. Status status = GDIPlus.GdipSetMatrixElements (nativeMatrix, 1, 0, 0, 1, 0, 0);
  154. GDIPlus.CheckStatus (status);
  155. }
  156. public void Rotate (float angle)
  157. {
  158. Rotate (angle, MatrixOrder.Prepend);
  159. }
  160. public void Rotate (float angle, MatrixOrder order)
  161. {
  162. Status status = GDIPlus.GdipRotateMatrix (nativeMatrix, angle, order);
  163. GDIPlus.CheckStatus (status);
  164. }
  165. public void RotateAt (float angle, PointF point)
  166. {
  167. RotateAt (angle, point, MatrixOrder.Prepend);
  168. }
  169. public void RotateAt (float angle, PointF point, MatrixOrder order)
  170. {
  171. angle *= (float) (Math.PI / 180.0); // degrees to radians
  172. float cos = (float) Math.Cos (angle);
  173. float sin = (float) Math.Sin (angle);
  174. float e4 = -point.X * cos + point.Y * sin + point.X;
  175. float e5 = -point.X * sin - point.Y * cos + point.Y;
  176. float[] m = this.Elements;
  177. Status status;
  178. if (order == MatrixOrder.Prepend)
  179. status = GDIPlus.GdipSetMatrixElements (nativeMatrix,
  180. cos * m[0] + sin * m[2],
  181. cos * m[1] + sin * m[3],
  182. -sin * m[0] + cos * m[2],
  183. -sin * m[1] + cos * m[3],
  184. e4 * m[0] + e5 * m[2] + m[4],
  185. e4 * m[1] + e5 * m[3] + m[5]);
  186. else
  187. status = GDIPlus.GdipSetMatrixElements (nativeMatrix,
  188. m[0] * cos + m[1] * -sin,
  189. m[0] * sin + m[1] * cos,
  190. m[2] * cos + m[3] * -sin,
  191. m[2] * sin + m[3] * cos,
  192. m[4] * cos + m[5] * -sin + e4,
  193. m[4] * sin + m[5] * cos + e5);
  194. GDIPlus.CheckStatus (status);
  195. }
  196. public void Scale (float scaleX, float scaleY)
  197. {
  198. Scale (scaleX, scaleY, MatrixOrder.Prepend);
  199. }
  200. public void Scale (float scaleX, float scaleY, MatrixOrder order)
  201. {
  202. Status status = GDIPlus.GdipScaleMatrix (nativeMatrix, scaleX, scaleY, order);
  203. GDIPlus.CheckStatus (status);
  204. }
  205. public void Shear (float shearX, float shearY)
  206. {
  207. Shear (shearX, shearY, MatrixOrder.Prepend);
  208. }
  209. public void Shear (float shearX, float shearY, MatrixOrder order)
  210. {
  211. Status status = GDIPlus.GdipShearMatrix (nativeMatrix, shearX, shearY, order);
  212. GDIPlus.CheckStatus (status);
  213. }
  214. public void TransformPoints (Point[] pts)
  215. {
  216. Status status = GDIPlus.GdipTransformMatrixPointsI (nativeMatrix, pts, pts.Length);
  217. GDIPlus.CheckStatus (status);
  218. }
  219. public void TransformPoints (PointF[] pts)
  220. {
  221. Status status = GDIPlus.GdipTransformMatrixPoints (nativeMatrix, pts, pts.Length);
  222. GDIPlus.CheckStatus (status);
  223. }
  224. public void TransformVectors (Point[] pts)
  225. {
  226. Status status = GDIPlus.GdipVectorTransformMatrixPointsI (nativeMatrix, pts, pts.Length);
  227. GDIPlus.CheckStatus (status);
  228. }
  229. public void TransformVectors (PointF[] pts)
  230. {
  231. Status status = GDIPlus.GdipVectorTransformMatrixPoints (nativeMatrix, pts, pts.Length);
  232. GDIPlus.CheckStatus (status);
  233. }
  234. public void Translate (float offsetX, float offsetY)
  235. {
  236. Translate (offsetX, offsetY, MatrixOrder.Prepend);
  237. }
  238. public void Translate (float offsetX, float offsetY, MatrixOrder order)
  239. {
  240. Status status = GDIPlus.GdipTranslateMatrix (nativeMatrix, offsetX, offsetY, order);
  241. GDIPlus.CheckStatus (status);
  242. }
  243. public void VectorTransformPoints (Point[] pts)
  244. {
  245. TransformVectors (pts);
  246. }
  247. internal IntPtr NativeObject
  248. {
  249. get{
  250. return nativeMatrix;
  251. }
  252. set {
  253. nativeMatrix = value;
  254. }
  255. }
  256. }
  257. }