Matrix.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using Avalonia.Utilities;
  4. using System;
  5. using System.Globalization;
  6. using System.Linq;
  7. namespace Avalonia
  8. {
  9. /// <summary>
  10. /// A 2x3 matrix.
  11. /// </summary>
  12. public struct Matrix
  13. {
  14. private readonly double _m11;
  15. private readonly double _m12;
  16. private readonly double _m21;
  17. private readonly double _m22;
  18. private readonly double _m31;
  19. private readonly double _m32;
  20. /// <summary>
  21. /// Initializes a new instance of the <see cref="Matrix"/> struct.
  22. /// </summary>
  23. /// <param name="m11">The first element of the first row.</param>
  24. /// <param name="m12">The second element of the first row.</param>
  25. /// <param name="m21">The first element of the second row.</param>
  26. /// <param name="m22">The second element of the second row.</param>
  27. /// <param name="offsetX">The first element of the third row.</param>
  28. /// <param name="offsetY">The second element of the third row.</param>
  29. public Matrix(
  30. double m11,
  31. double m12,
  32. double m21,
  33. double m22,
  34. double offsetX,
  35. double offsetY)
  36. {
  37. _m11 = m11;
  38. _m12 = m12;
  39. _m21 = m21;
  40. _m22 = m22;
  41. _m31 = offsetX;
  42. _m32 = offsetY;
  43. }
  44. /// <summary>
  45. /// Returns the multiplicative identity matrix.
  46. /// </summary>
  47. public static Matrix Identity => new Matrix(1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
  48. /// <summary>
  49. /// Returns whether the matrix is the identity matrix.
  50. /// </summary>
  51. public bool IsIdentity => Equals(Identity);
  52. /// <summary>
  53. /// HasInverse Property - returns true if this matrix is invertable, false otherwise.
  54. /// </summary>
  55. public bool HasInverse => GetDeterminant() != 0;
  56. /// <summary>
  57. /// The first element of the first row
  58. /// </summary>
  59. public double M11 => _m11;
  60. /// <summary>
  61. /// The second element of the first row
  62. /// </summary>
  63. public double M12 => _m12;
  64. /// <summary>
  65. /// The first element of the second row
  66. /// </summary>
  67. public double M21 => _m21;
  68. /// <summary>
  69. /// The second element of the second row
  70. /// </summary>
  71. public double M22 => _m22;
  72. /// <summary>
  73. /// The first element of the third row
  74. /// </summary>
  75. public double M31 => _m31;
  76. /// <summary>
  77. /// The second element of the third row
  78. /// </summary>
  79. public double M32 => _m32;
  80. /// <summary>
  81. /// Multiplies two matrices together and returns the resulting matrix.
  82. /// </summary>
  83. /// <param name="value1">The first source matrix.</param>
  84. /// <param name="value2">The second source matrix.</param>
  85. /// <returns>The product matrix.</returns>
  86. public static Matrix operator *(Matrix value1, Matrix value2)
  87. {
  88. return new Matrix(
  89. (value1.M11 * value2.M11) + (value1.M12 * value2.M21),
  90. (value1.M11 * value2.M12) + (value1.M12 * value2.M22),
  91. (value1.M21 * value2.M11) + (value1.M22 * value2.M21),
  92. (value1.M21 * value2.M12) + (value1.M22 * value2.M22),
  93. (value1._m31 * value2.M11) + (value1._m32 * value2.M21) + value2._m31,
  94. (value1._m31 * value2.M12) + (value1._m32 * value2.M22) + value2._m32);
  95. }
  96. /// <summary>
  97. /// Negates the given matrix by multiplying all values by -1.
  98. /// </summary>
  99. /// <param name="value">The source matrix.</param>
  100. /// <returns>The negated matrix.</returns>
  101. public static Matrix operator -(Matrix value)
  102. {
  103. return value.Invert();
  104. }
  105. /// <summary>
  106. /// Returns a boolean indicating whether the given matrices are equal.
  107. /// </summary>
  108. /// <param name="value1">The first source matrix.</param>
  109. /// <param name="value2">The second source matrix.</param>
  110. /// <returns>True if the matrices are equal; False otherwise.</returns>
  111. public static bool operator ==(Matrix value1, Matrix value2)
  112. {
  113. return value1.Equals(value2);
  114. }
  115. /// <summary>
  116. /// Returns a boolean indicating whether the given matrices are not equal.
  117. /// </summary>
  118. /// <param name="value1">The first source matrix.</param>
  119. /// <param name="value2">The second source matrix.</param>
  120. /// <returns>True if the matrices are not equal; False if they are equal.</returns>
  121. public static bool operator !=(Matrix value1, Matrix value2)
  122. {
  123. return !value1.Equals(value2);
  124. }
  125. /// <summary>
  126. /// Creates a rotation matrix using the given rotation in radians.
  127. /// </summary>
  128. /// <param name="radians">The amount of rotation, in radians.</param>
  129. /// <returns>A rotation matrix.</returns>
  130. public static Matrix CreateRotation(double radians)
  131. {
  132. double cos = Math.Cos(radians);
  133. double sin = Math.Sin(radians);
  134. return new Matrix(cos, sin, -sin, cos, 0, 0);
  135. }
  136. /// <summary>
  137. /// Creates a skew matrix from the given axis skew angles in radians.
  138. /// </summary>
  139. /// <param name="xAngle">The amount of skew along the X-axis, in radians.</param>
  140. /// <param name="yAngle">The amount of skew along the Y-axis, in radians.</param>
  141. /// <returns>A rotation matrix.</returns>
  142. public static Matrix CreateSkew(double xAngle, double yAngle)
  143. {
  144. double tanX = Math.Tan(xAngle);
  145. double tanY = Math.Tan(yAngle);
  146. return new Matrix(1.0, tanY, tanX, 1.0, 0.0, 0.0);
  147. }
  148. /// <summary>
  149. /// Creates a scale matrix from the given X and Y components.
  150. /// </summary>
  151. /// <param name="xScale">Value to scale by on the X-axis.</param>
  152. /// <param name="yScale">Value to scale by on the Y-axis.</param>
  153. /// <returns>A scaling matrix.</returns>
  154. public static Matrix CreateScale(double xScale, double yScale)
  155. {
  156. return CreateScale(new Vector(xScale, yScale));
  157. }
  158. /// <summary>
  159. /// Creates a scale matrix from the given vector scale.
  160. /// </summary>
  161. /// <param name="scales">The scale to use.</param>
  162. /// <returns>A scaling matrix.</returns>
  163. public static Matrix CreateScale(Vector scales)
  164. {
  165. return new Matrix(scales.X, 0, 0, scales.Y, 0, 0);
  166. }
  167. /// <summary>
  168. /// Creates a translation matrix from the given vector.
  169. /// </summary>
  170. /// <param name="position">The translation position.</param>
  171. /// <returns>A translation matrix.</returns>
  172. public static Matrix CreateTranslation(Vector position)
  173. {
  174. return CreateTranslation(position.X, position.Y);
  175. }
  176. /// <summary>
  177. /// Creates a translation matrix from the given X and Y components.
  178. /// </summary>
  179. /// <param name="xPosition">The X position.</param>
  180. /// <param name="yPosition">The Y position.</param>
  181. /// <returns>A translation matrix.</returns>
  182. public static Matrix CreateTranslation(double xPosition, double yPosition)
  183. {
  184. return new Matrix(1.0, 0.0, 0.0, 1.0, xPosition, yPosition);
  185. }
  186. /// <summary>
  187. /// Converts an ange in degrees to radians.
  188. /// </summary>
  189. /// <param name="angle">The angle in degrees.</param>
  190. /// <returns>The angle in radians.</returns>
  191. public static double ToRadians(double angle)
  192. {
  193. return angle * 0.0174532925;
  194. }
  195. /// <summary>
  196. /// Calculates the determinant for this matrix.
  197. /// </summary>
  198. /// <returns>The determinant.</returns>
  199. /// <remarks>
  200. /// The determinant is calculated by expanding the matrix with a third column whose
  201. /// values are (0,0,1).
  202. /// </remarks>
  203. public double GetDeterminant()
  204. {
  205. return (_m11 * _m22) - (_m12 * _m21);
  206. }
  207. /// <summary>
  208. /// Returns a boolean indicating whether the matrix is equal to the other given matrix.
  209. /// </summary>
  210. /// <param name="other">The other matrix to test equality against.</param>
  211. /// <returns>True if this matrix is equal to other; False otherwise.</returns>
  212. public bool Equals(Matrix other)
  213. {
  214. return _m11 == other.M11 &&
  215. _m12 == other.M12 &&
  216. _m21 == other.M21 &&
  217. _m22 == other.M22 &&
  218. _m31 == other.M31 &&
  219. _m32 == other.M32;
  220. }
  221. /// <summary>
  222. /// Returns a boolean indicating whether the given Object is equal to this matrix instance.
  223. /// </summary>
  224. /// <param name="obj">The Object to compare against.</param>
  225. /// <returns>True if the Object is equal to this matrix; False otherwise.</returns>
  226. public override bool Equals(object obj)
  227. {
  228. if (!(obj is Matrix))
  229. {
  230. return false;
  231. }
  232. return Equals((Matrix)obj);
  233. }
  234. /// <summary>
  235. /// Returns the hash code for this instance.
  236. /// </summary>
  237. /// <returns>The hash code.</returns>
  238. public override int GetHashCode()
  239. {
  240. return M11.GetHashCode() + M12.GetHashCode() +
  241. M21.GetHashCode() + M22.GetHashCode() +
  242. M31.GetHashCode() + M32.GetHashCode();
  243. }
  244. /// <summary>
  245. /// Returns a String representing this matrix instance.
  246. /// </summary>
  247. /// <returns>The string representation.</returns>
  248. public override string ToString()
  249. {
  250. CultureInfo ci = CultureInfo.CurrentCulture;
  251. return string.Format(
  252. ci,
  253. "{{ {{M11:{0} M12:{1}}} {{M21:{2} M22:{3}}} {{M31:{4} M32:{5}}} }}",
  254. M11.ToString(ci),
  255. M12.ToString(ci),
  256. M21.ToString(ci),
  257. M22.ToString(ci),
  258. M31.ToString(ci),
  259. M32.ToString(ci));
  260. }
  261. /// <summary>
  262. /// Inverts the Matrix.
  263. /// </summary>
  264. /// <returns>The inverted matrix.</returns>
  265. public Matrix Invert()
  266. {
  267. if (GetDeterminant() == 0)
  268. {
  269. throw new InvalidOperationException("Transform is not invertible.");
  270. }
  271. double d = GetDeterminant();
  272. return new Matrix(
  273. _m22 / d,
  274. -_m12 / d,
  275. -_m21 / d,
  276. _m11 / d,
  277. ((_m21 * _m32) - (_m22 * _m31)) / d,
  278. ((_m12 * _m31) - (_m11 * _m32)) / d);
  279. }
  280. /// <summary>
  281. /// Parses a <see cref="Matrix"/> string.
  282. /// </summary>
  283. /// <param name="s">The string.</param>
  284. /// <returns>The <see cref="Matrix"/>.</returns>
  285. public static Matrix Parse(string s)
  286. {
  287. using (var tokenizer = new StringTokenizer(s, CultureInfo.InvariantCulture, exceptionMessage: "Invalid Matrix"))
  288. {
  289. return new Matrix(
  290. tokenizer.ReadDouble(),
  291. tokenizer.ReadDouble(),
  292. tokenizer.ReadDouble(),
  293. tokenizer.ReadDouble(),
  294. tokenizer.ReadDouble(),
  295. tokenizer.ReadDouble()
  296. );
  297. }
  298. }
  299. }
  300. }