Matrix.jvm.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. using System;
  2. using System.Drawing;
  3. using System.Runtime.InteropServices;
  4. using geom = java.awt.geom;
  5. using JMath = java.lang.Math;
  6. namespace System.Drawing.Drawing2D
  7. {
  8. public sealed class Matrix : MarshalByRefObject, IDisposable
  9. {
  10. #region fields
  11. static internal readonly Matrix IdentityTransform = new Matrix();
  12. readonly geom.AffineTransform _nativeMatrix;
  13. #endregion
  14. #region ctors
  15. internal Matrix (geom.AffineTransform ptr)
  16. {
  17. _nativeMatrix = ptr;
  18. }
  19. public Matrix () : this(new geom.AffineTransform())
  20. {
  21. }
  22. public Matrix (Rectangle rect , Point[] plgpts)
  23. {
  24. double x1 = plgpts[1].X - plgpts[0].X;
  25. double y1 = plgpts[1].Y - plgpts[0].Y;
  26. double x2 = plgpts[2].X - plgpts[0].X;
  27. double y2 = plgpts[2].Y - plgpts[0].Y;
  28. _nativeMatrix = new geom.AffineTransform(x1/rect.Width, y1/rect.Width, x2/rect.Height, y2/rect.Height, plgpts[0].X, plgpts[0].Y);
  29. _nativeMatrix.translate(-rect.X,-rect.Y);
  30. }
  31. public Matrix (RectangleF rect , PointF[] plgpts)
  32. {
  33. double x1 = plgpts[1].X - plgpts[0].X;
  34. double y1 = plgpts[1].Y - plgpts[0].Y;
  35. double x2 = plgpts[2].X - plgpts[0].X;
  36. double y2 = plgpts[2].Y - plgpts[0].Y;
  37. _nativeMatrix = new geom.AffineTransform(x1/rect.Width, y1/rect.Width, x2/rect.Height, y2/rect.Height, plgpts[0].X, plgpts[0].Y);
  38. _nativeMatrix.translate(-rect.X,-rect.Y);
  39. }
  40. public Matrix (float m11, float m12, float m21, float m22, float dx, float dy)
  41. : this(new geom.AffineTransform(m11,m12,m21,m22,dx,dy))
  42. {
  43. }
  44. #endregion
  45. #region properties
  46. public float[] Elements
  47. {
  48. get
  49. {
  50. float [] elems = new float[] {
  51. (float)NativeObject.getScaleX(),
  52. (float)NativeObject.getShearY(),
  53. (float)NativeObject.getShearX(),
  54. (float)NativeObject.getScaleY(),
  55. (float)NativeObject.getTranslateX(),
  56. (float)NativeObject.getTranslateY()};
  57. return elems;
  58. }
  59. }
  60. public bool IsIdentity
  61. {
  62. get
  63. {
  64. return NativeObject.isIdentity();
  65. }
  66. }
  67. public bool IsInvertible
  68. {
  69. get
  70. {
  71. try
  72. {
  73. return NativeObject.getDeterminant() != 0.0;
  74. }
  75. catch(geom.NoninvertibleTransformException)
  76. {
  77. return false;
  78. }
  79. }
  80. }
  81. public float OffsetX
  82. {
  83. get
  84. {
  85. return (float)NativeObject.getTranslateX();
  86. }
  87. }
  88. public float OffsetY
  89. {
  90. get
  91. {
  92. return (float)NativeObject.getTranslateY();
  93. }
  94. }
  95. #endregion
  96. #region methods
  97. public Matrix Clone()
  98. {
  99. return new Matrix ((geom.AffineTransform)NativeObject.clone());
  100. }
  101. public void Dispose ()
  102. {
  103. }
  104. internal void CopyTo(Matrix matrix) {
  105. matrix.NativeObject.setTransform(NativeObject);
  106. }
  107. public override bool Equals (object obj)
  108. {
  109. Matrix m = obj as Matrix;
  110. if (m == null)
  111. return false;
  112. return NativeObject.equals(m.NativeObject);
  113. }
  114. public override int GetHashCode ()
  115. {
  116. return NativeObject.hashCode();
  117. }
  118. public void Invert ()
  119. {
  120. try {
  121. _nativeMatrix.setTransform( _nativeMatrix.createInverse() );
  122. }
  123. catch(geom.NoninvertibleTransformException e) {
  124. throw new ArgumentException(e.Message, e);
  125. }
  126. }
  127. public void Multiply (Matrix matrix)
  128. {
  129. Multiply (matrix, MatrixOrder.Prepend);
  130. }
  131. public void Multiply (Matrix matrix, MatrixOrder order)
  132. {
  133. Multiply(matrix.NativeObject, order);
  134. }
  135. public void Reset()
  136. {
  137. NativeObject.setToIdentity();
  138. }
  139. public void Rotate (float angle)
  140. {
  141. NativeObject.rotate (JMath.toRadians(angle));
  142. }
  143. public void Rotate (float angle, MatrixOrder order)
  144. {
  145. Multiply(geom.AffineTransform.getRotateInstance(JMath.toRadians(angle)), order);
  146. }
  147. public void RotateAt (float angle, PointF point)
  148. {
  149. NativeObject.rotate (JMath.toRadians(angle), point.X, point.Y);
  150. }
  151. public void RotateAt (float angle, PointF point, MatrixOrder order)
  152. {
  153. Multiply(geom.AffineTransform.getRotateInstance(JMath.toRadians(angle),point.X, point.Y), order);
  154. }
  155. public void Scale (float scaleX, float scaleY)
  156. {
  157. NativeObject.scale (scaleX, scaleY);
  158. }
  159. public void Scale (float scaleX, float scaleY, MatrixOrder order)
  160. {
  161. Multiply(geom.AffineTransform.getScaleInstance(scaleX, scaleY), order);
  162. }
  163. public void Shear (float shearX, float shearY)
  164. {
  165. NativeObject.shear(shearX, shearY);
  166. }
  167. public void Shear (float shearX, float shearY, MatrixOrder order)
  168. {
  169. Multiply(geom.AffineTransform.getShearInstance (shearX, shearY), order);
  170. }
  171. public void TransformPoints (Point[] pts)
  172. {
  173. geom.Point2D.Float pt = new geom.Point2D.Float();
  174. for(int i =0;i < pts.Length;i++) {
  175. pt.setLocation(pts[i].X,pts[i].Y);
  176. NativeObject.transform(pt,pt);
  177. pts[i].X=(int)pt.getX();
  178. pts[i].Y=(int)pt.getY();
  179. }
  180. }
  181. public void TransformPoints (PointF[] pts)
  182. {
  183. geom.Point2D.Float pt = new geom.Point2D.Float();
  184. for(int i =0;i < pts.Length;i++) {
  185. pt.setLocation(pts[i].X,pts[i].Y);
  186. NativeObject.transform(pt,pt);
  187. pts[i].X=(float)pt.getX();
  188. pts[i].Y=(float)pt.getY();
  189. }
  190. }
  191. public void TransformVectors (Point[] pts)
  192. {
  193. geom.Point2D.Float pt = new geom.Point2D.Float();
  194. for(int i =0;i < pts.Length;i++) {
  195. pt.setLocation(pts[i].X,pts[i].Y);
  196. NativeObject.deltaTransform(pt,pt);
  197. pts[i].X=(int)pt.getX();
  198. pts[i].Y=(int)pt.getY();
  199. }
  200. }
  201. public void TransformVectors (PointF[] pts)
  202. {
  203. geom.Point2D.Float pt = new geom.Point2D.Float();
  204. for(int i =0;i < pts.Length;i++) {
  205. pt.setLocation(pts[i].X,pts[i].Y);
  206. NativeObject.deltaTransform(pt,pt);
  207. pts[i].X=(float)pt.getX();
  208. pts[i].Y=(float)pt.getY();
  209. }
  210. }
  211. public void Translate (float offsetX, float offsetY)
  212. {
  213. NativeObject.translate (offsetX, offsetY);
  214. }
  215. public void Translate (float offsetX, float offsetY, MatrixOrder order)
  216. {
  217. Multiply(geom.AffineTransform.getTranslateInstance(offsetX, offsetY), order);
  218. }
  219. public void VectorTransformPoints (Point[] pts)
  220. {
  221. TransformVectors (pts);
  222. }
  223. internal geom.AffineTransform NativeObject
  224. {
  225. get
  226. {
  227. return _nativeMatrix;
  228. }
  229. }
  230. void Multiply(geom.AffineTransform at, MatrixOrder order) {
  231. Multiply(NativeObject, at, order);
  232. }
  233. internal static void Multiply(geom.AffineTransform to, geom.AffineTransform add, MatrixOrder order) {
  234. if(order == MatrixOrder.Prepend)
  235. to.concatenate(add);
  236. else
  237. to.preConcatenate(add);
  238. }
  239. #endregion
  240. }
  241. }