Matrix3x2.cs 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  1. using System;
  2. using System.Diagnostics;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.Runtime.CompilerServices;
  5. using Microsoft.Xna.Framework;
  6. namespace MonoGame.Extended;
  7. /// <summary>
  8. /// Represents a 3x2 matrix using floating point values for each component that can store two dimensional translation,
  9. /// scale, and rotation information for a right-handed coordinate system.
  10. /// </summary>
  11. /// <remarks>
  12. /// <para>
  13. /// Matrices use a row vector layout in the XNA / MonoGame Framework but, in general, matrices can be either
  14. /// have a row vector or column vector layout. Row vector matrices view vectors as a row from left to right,
  15. /// while column vector matrices view vectors as a column from top to bottom. For example, the
  16. /// <see cref="Translation" /> corresponds to the fields <see cref="M31" /> and <see cref="M32" />.
  17. /// </para>
  18. /// <para>
  19. /// The fields see <b>M13</b> and <b>M23</b> always have a value of <c>0.0f</c>, and thus are removed from
  20. /// the <see cref="Matrix3x2" /> to reduce its memory footprint. Same is true for the field <b>M33</b>, except
  21. /// it always has a value of <c>1.0f</c>.
  22. /// </para>
  23. /// </remarks>
  24. [DebuggerDisplay($"{nameof(DebugDisplayString)},nq")]
  25. public struct Matrix3x2 : IEquatable<Matrix3x2>
  26. {
  27. /// <summary>The first element of the first row.</summary>
  28. /// <remarks>Represents the scaling factor on the x-axis or a combination of scaling and rotation.</remarks>
  29. public float M11;
  30. /// <summary>The second element of the first row.</summary>
  31. /// <remarks>Represents the shearing factor on the y-axis or a combination of shearing and rotation.</remarks>
  32. public float M12;
  33. /// <summary>The first element of the second row.</summary>
  34. /// <remarks>Represents the shearing factor on the x-axis or a combination of shear and rotation.</remarks>
  35. public float M21;
  36. /// <summary>The second element of the second row.</summary>
  37. /// <remarks>Represents the scaling factor on the y-axis or a combination of scale and rotation.</remarks>
  38. public float M22;
  39. /// <summary>The first element of the third row.</summary>
  40. /// <remarks>Represents the translation on the x-axis</remarks>
  41. public float M31;
  42. /// <summary>The second element of the third row.</summary>
  43. /// <remarks>Represents the translation on the y-axis</remarks>
  44. public float M32;
  45. /// <summary>
  46. /// Gets or Sets the vector formed by the first row of this <see cref="Matrix3x2"/>
  47. /// </summary>
  48. /// <remarks>
  49. /// <para>
  50. /// The <see cref="Vector2.X"/> component of the vector represents the scaling factor on the x-axis or a
  51. /// combination of scaling and rotation.
  52. /// </para>
  53. /// <para>
  54. /// The <see cref="Vector2.Y"/> component of the vector represents the shearing factor on the y-axis or a
  55. /// combination of shearing and rotation.
  56. /// </para>
  57. /// </remarks>
  58. public Vector2 X
  59. {
  60. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  61. readonly get => Unsafe.As<float, Vector2>(ref Unsafe.AsRef(in M11));
  62. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  63. set => Unsafe.As<float, Vector2>(ref M11) = value;
  64. }
  65. /// <summary>
  66. /// Gets the vector formed by the second row of this <see cref="Matrix3x2"/>
  67. /// </summary>
  68. /// <remarks>
  69. /// <para>
  70. /// The <see cref="Vector2.X"/> component of the vector represents the shearing factor on the x-axis or a
  71. /// combination of shearing and rotation.
  72. /// </para>
  73. /// <para>
  74. /// The <see cref="Vector2.Y"/> component of the vector represents the scaling factor on the y-axis or a
  75. /// combination of scaling and rotation.
  76. /// </para>
  77. /// </remarks>
  78. public Vector2 Y
  79. {
  80. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  81. readonly get => Unsafe.As<float, Vector2>(ref Unsafe.AsRef(in M21));
  82. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  83. set => Unsafe.As<float, Vector2>(ref M21) = value;
  84. }
  85. /// <summary>
  86. /// Gets or Sets the vector formed by the third row of this <see cref="Matrix3x2"/>
  87. /// </summary>
  88. /// <remarks>
  89. /// <para>
  90. /// The <see cref="Vector2.X"/> component of the vector represents the translation on the x-axis.
  91. /// </para>
  92. /// <para>
  93. /// The <see cref="Vector2.Y"/> component of the vector represents the translation on the y-axis.
  94. /// </para>
  95. /// </remarks>
  96. public Vector2 Z
  97. {
  98. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  99. readonly get => Unsafe.As<float, Vector2>(ref Unsafe.AsRef(in M31));
  100. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  101. set => Unsafe.As<float, Vector2>(ref M31) = value;
  102. }
  103. /// <summary>
  104. /// Gets the multiplicative identity matrix.
  105. /// </summary>
  106. /// <remarks>
  107. /// <para>The first row of the identity matrix is equal to <see cref="Vector2.UnitX"/></para>
  108. /// <para>The second row of the identity matrix is equal to <see cref="Vector2.UnitY"/></para>
  109. /// <para>The third row of the identity matrix is equal to <see cref="Vector2.Zero"/></para>
  110. /// </remarks>
  111. public static readonly Matrix3x2 Identity = new Matrix3x2(Vector2.UnitX, Vector2.UnitY, Vector2.Zero);
  112. /// <summary>
  113. /// Gets the translation component of this <see cref="Matrix3x2"/>.
  114. /// </summary>
  115. /// <remarks>
  116. /// The translation is equal to the third row vector <see cref="Z"/> composed of the <see cref="M31"/> and
  117. /// <see cref="M32"/> values.
  118. /// </remarks>
  119. [Obsolete("Use Decompose method. This property will be removed in 4.0")]
  120. public readonly Vector2 Translation
  121. {
  122. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  123. get => Z;
  124. }
  125. /// <summary>
  126. /// Gets the rotation component of this <see cref="Matrix3x2"/>.
  127. /// </summary>
  128. /// <remarks>
  129. /// The rotation is equal to the arctangent of the <see cref="M21"/> and <see cref="M11"/>.
  130. /// <code>Math.Atan2(M21, M11);</code>
  131. /// </remarks>
  132. [Obsolete("Use Decompose method. This property will be removed in 4.0")]
  133. public readonly float Rotation
  134. {
  135. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  136. get => (float)Math.Atan2(M21, M11);
  137. }
  138. /// <summary>
  139. /// Gets the scale component of this <see cref="Matrix3x2"/>.
  140. /// </summary>
  141. /// <remarks>
  142. /// The scale is equal to equal to the square root of the sum of the squares of matrix elements, with sign
  143. /// adjustment.
  144. /// </remarks>
  145. [Obsolete("Use Decompose method. This property will be removed in 4.0")]
  146. public readonly Vector2 Scale
  147. {
  148. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  149. get
  150. {
  151. var xSign = Math.Sign((M11 * M11) + (M21 * M21)) < 0 ? -1 : 1;
  152. var ySign = Math.Sign((M11 * M12) + (M22 * M22)) < 0 ? -1 : 1;
  153. Vector2 scale;
  154. scale.X = xSign * (float)Math.Sqrt((M11 * M11) + (M21 * M21));
  155. scale.Y = ySign * (float)Math.Sqrt((M11 * M12) + (M22 * M22));
  156. return scale;
  157. }
  158. }
  159. /// <summary>
  160. /// Creates a 3x2 matrix from the specified components.
  161. /// </summary>
  162. /// <param name="m11">The value to assign to the first element of the first row.</param>
  163. /// <param name="m12">The value to assign to the second element of the first row.</param>
  164. /// <param name="m21">The value to assign to the first element of the second row.</param>
  165. /// <param name="m22">The value to assign to the second element of the second row.</param>
  166. /// <param name="m31">The value to assign to the first element of the third row.</param>
  167. /// <param name="m32">The value to assign to the second element of the third row.</param>
  168. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  169. public Matrix3x2(float m11, float m12,
  170. float m21, float m22,
  171. float m31, float m32)
  172. {
  173. M11 = m11;
  174. M12 = m12;
  175. M21 = m21;
  176. M22 = m22;
  177. M31 = m31;
  178. M32 = m32;
  179. }
  180. /// <summary>
  181. /// Creates a new 3x2 matrix from the specified components.
  182. /// </summary>
  183. /// <param name="x">The value to assign to the elements of the first row.</param>
  184. /// <param name="y">The value to assign to the elements of the second row.</param>
  185. /// <param name="z">The value to assign to the elements of the third row.</param>
  186. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  187. public Matrix3x2(Vector2 x, Vector2 y, Vector2 z)
  188. : this(x.X, x.Y, y.X, y.Y, z.X, z.Y) { }
  189. /// <summary>
  190. /// Transforms the given vector by this <see cref="Matrix3x2"/>
  191. /// </summary>
  192. /// <param name="vector">The vector to transform.</param>
  193. /// <returns>The result of the transformation.</returns>
  194. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  195. public Vector2 Transform(Vector2 vector) => Transform(vector.X, vector.Y);
  196. /// <summary>
  197. /// Transforms the given vector by this <see cref="Matrix3x2"/>
  198. /// </summary>
  199. /// <param name="vector">The vector to transform.</param>
  200. /// <param name="result">
  201. /// When this method returns, contains the result of the transformation. This parameter is passed uninitialized.
  202. /// </param>
  203. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  204. public void Transform(Vector2 vector, out Vector2 result) => Transform(vector.X, vector.Y, out result);
  205. /// <summary>
  206. /// Transforms a vector with the specified x- and y-coordinate component values by this <see cref="Matrix3x2"/>
  207. /// </summary>
  208. /// <param name="x">The x-coordinate component value of the vector to transform.</param>
  209. /// <param name="y">The y-coordinate component value of the vector to transform.</param>
  210. /// <returns>The result of the transformation.</returns>
  211. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  212. public Vector2 Transform(in float x, in float y)
  213. {
  214. Vector2 result;
  215. Transform(x, y, out result);
  216. return result;
  217. }
  218. /// <summary>
  219. /// Transforms a vector with the specified x- and y-coordinate component values by this <see cref="Matrix3x2"/>
  220. /// </summary>
  221. /// <param name="x">The x-coordinate component value of the vector to transform.</param>
  222. /// <param name="y">The y-coordinate component value of the vector to transform.</param>
  223. /// <param name="result">
  224. /// When this method returns, contains the result of the transformation. This parameter is passed uninitialized.
  225. /// </param>
  226. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  227. public void Transform(in float x, in float y, out Vector2 result)
  228. {
  229. result.X = (x * M11) + (y * M21) + M31;
  230. result.Y = (x * M12) + (y * M22) + M32;
  231. }
  232. /// <summary>
  233. /// Transforms a vector with the specified x- and y-coordinate component values by this <see cref="Matrix3x2"/>
  234. /// </summary>
  235. /// <param name="x">The x-coordinate component value of the vector to transform.</param>
  236. /// <param name="y">The y-coordinate component value of the vector to transform.</param>
  237. /// <param name="result">When this method returns, contains the result of the transformation.</param>
  238. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  239. public void Transform(in float x, in float y, ref Vector3 result)
  240. {
  241. result.X = (x * M11) + (y * M21) + M31;
  242. result.Y = (x * M12) + (y * M22) + M32;
  243. }
  244. /// <summary>
  245. /// Calculates the determinant of this <see cref="Matrix3x2"/>.
  246. /// </summary>
  247. /// <remarks>
  248. /// The determinant is calculated by expanding this matrix with a third column whose values are (0, 0, 1).
  249. /// </remarks>
  250. /// <returns>The determinant of this <see cref="Matrix3x2"/>.</returns>
  251. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  252. public float Determinant() => (M11 * M22) - (M12 * M21);
  253. /// <summary>
  254. /// Deconstructs this <see cref="Matrix3x2"/> into its translation, rotation, and scale component representations.
  255. /// </summary>
  256. /// <param name="translation">
  257. /// When this method returns, contains the translation component of this <see cref="Matrix3x2"/>. This parameter is
  258. /// passed uninitialized.
  259. /// </param>
  260. /// <param name="rotation">
  261. /// When this method returns, contains the rotation component of this <see cref="Matrix3x2"/>. This parameter is
  262. /// passed uninitialized.
  263. /// </param>
  264. /// <param name="scale">
  265. /// When this method returns, contains the scale component of this <see cref="Matrix3x2"/>. This parameter is
  266. /// passed uninitialized.
  267. /// </param>
  268. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  269. public void Decompose(out Vector2 translation, out float rotation, out Vector2 scale)
  270. {
  271. translation.X = M31;
  272. translation.Y = M32;
  273. rotation = (float)Math.Atan2(M21, M11);
  274. var x = M11 * M11 + M21 * M21;
  275. var y = M12 * M12 + M22 * M22;
  276. var xSign = Math.Sign(x) < 0 ? -1 : 1;
  277. var ySign = Math.Sign(y) < 0 ? -1 : 1;
  278. scale.X = xSign * (float)Math.Sqrt(x);
  279. scale.Y = ySign * (float)Math.Sqrt(y);
  280. }
  281. /// <summary>
  282. /// Creates a new <see cref="Matrix3x2"/> value for 2D translation, rotation, and scale.
  283. /// </summary>
  284. /// <remarks>Rotation is performed along the z-axis.</remarks>
  285. /// <param name="position">The amount to translate the matrix by on the x- and y-axis.</param>
  286. /// <param name="rotation">The amount to rotate, in radians, the matrix along the z-axis. </param>
  287. /// <param name="scale">The amount to scale the matrix along the x- and y-axis.</param>
  288. /// <param name="origin">The origin point at which to scale and rotate the matrix around.</param>
  289. /// <returns>The resulting <see cref="Matrix3x2"/> value created by this method.</returns>
  290. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  291. public static Matrix3x2 CreateFrom(Vector2 position, in float rotation, Vector2? scale, Vector2? origin)
  292. {
  293. CreateFrom(position, rotation, scale, origin, out Matrix3x2 result);
  294. return result;
  295. }
  296. /// <summary>
  297. /// Creates a new <see cref="Matrix3x2"/> value for 2D translation, rotation, and scale.
  298. /// </summary>
  299. /// <remarks>Rotation is performed along the z-axis.</remarks>
  300. /// <param name="position">The amount to translate the matrix by on the x- and y-axis.</param>
  301. /// <param name="rotation">The amount to rotate, in radians, the matrix along the z-axis. </param>
  302. /// <param name="scale">The amount to scale the matrix along the x- and y-axis.</param>
  303. /// <param name="origin">The origin point at which to scale and rotate the matrix around.</param>
  304. /// <param name="result">
  305. /// When this method returns, contains the resulting <see cref="Matrix3x2"/> value for 2D translation rotation, and
  306. /// scale. This parameter is passed uninitialized.
  307. /// </param>
  308. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  309. public static void CreateFrom(Vector2 position, in float rotation, Vector2? scale, Vector2? origin, out Matrix3x2 result)
  310. {
  311. result = Identity;
  312. if (origin.HasValue)
  313. {
  314. result.Z = -origin.Value;
  315. }
  316. if (scale.HasValue)
  317. {
  318. CreateScale(scale.Value, out Matrix3x2 scaleMatrix);
  319. Multiply(result, scaleMatrix, out result);
  320. }
  321. if (rotation != 0.0f)
  322. {
  323. CreateRotationZ(-rotation, out Matrix3x2 rotationMatrix);
  324. Multiply(result, rotationMatrix, out result);
  325. }
  326. CreateTranslation(position, out Matrix3x2 translationMatrix);
  327. Multiply(result, translationMatrix, out result);
  328. }
  329. /// <summary>
  330. /// Creates a <see cref="Matrix3x2"/> value for 2D rotation.
  331. /// </summary>
  332. /// <remarks>Rotation is performed along the z-axis.</remarks>
  333. /// <param name="radians">The mount to rotate, in radians, the matrix along the z-axis.</param>
  334. /// <returns>The resulting <see cref="Matrix3x2"/> value created by this method.</returns>
  335. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  336. public static Matrix3x2 CreateRotationZ(in float radians)
  337. {
  338. CreateRotationZ(radians, out Matrix3x2 result);
  339. return result;
  340. }
  341. /// <summary>
  342. /// Creates a <see cref="Matrix3x2"/> value for 2D rotation.
  343. /// </summary>
  344. /// <remarks>Rotation is performed along the z-axis.</remarks>
  345. /// <param name="radians">The mount to rotate, in radians, the matrix along the z-axis.</param>
  346. /// <param name="result">
  347. /// When this method returns, contains the resulting <see cref="Matrix3x2"/> value for 2D rotation. This parameter
  348. /// is passed uninitialized.
  349. /// </param>
  350. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  351. public static void CreateRotationZ(in float radians, out Matrix3x2 result)
  352. {
  353. var cos = (float)Math.Cos(radians);
  354. var sin = (float)Math.Sin(radians);
  355. result.M11 = cos;
  356. result.M12 = sin;
  357. result.M21 = -sin;
  358. result.M22 = cos;
  359. result.M31 = 0;
  360. result.M32 = 0;
  361. }
  362. /// <summary>
  363. /// Creates a <see cref="Matrix3x2"/> value for 2D scaling.
  364. /// </summary>
  365. /// <param name="scale">The amount to scale the matrix along the x- and y-axis.</param>
  366. /// <returns>The <see cref="Matrix3x2"/> value created by this method.</returns>
  367. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  368. public static Matrix3x2 CreateScale(in float scale)
  369. {
  370. CreateScale(scale, scale, out Matrix3x2 result);
  371. return result;
  372. }
  373. /// <summary>
  374. /// Creates a <see cref="Matrix3x2"/> value for 2D scaling.
  375. /// </summary>
  376. /// <param name="scale">The amount to scale the matrix along the x- and y-axis.</param>
  377. /// <param name="result">
  378. /// When this method returns, contains the resulting <see cref="Matrix3x2"/> value for 2D scaling created. This
  379. /// parameter is passed uninitialized.
  380. /// </param>
  381. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  382. public static void CreateScale(in float scale, out Matrix3x2 result) => CreateScale(scale, scale, out result);
  383. /// <summary>
  384. /// Creates a <see cref="Matrix3x2"/> value for 2D scaling.
  385. /// </summary>
  386. /// <param name="scale">A vector that represents the x- and y-axis scale factors.</param>
  387. /// <returns>The <see cref="Matrix3x2"/> value created by this method.</returns>
  388. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  389. public static Matrix3x2 CreateScale(Vector2 scale)
  390. {
  391. CreateScale(scale.X, scale.Y, out Matrix3x2 result);
  392. return result;
  393. }
  394. /// <summary>
  395. /// Creates a <see cref="Matrix3x2"/> value for 2D scaling.
  396. /// </summary>
  397. /// <param name="scale">A vector that represents the x- and y-axis scale factors.</param>
  398. /// <param name="result">
  399. /// When this method returns, contains the resulting <see cref="Matrix3x2"/> value for 2D scaling. This parameter
  400. /// is passed uninitialized.
  401. /// </param>
  402. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  403. public static void CreateScale(in Vector2 scale, out Matrix3x2 result) =>
  404. CreateScale(scale.X, scale.Y, out result);
  405. /// <summary>
  406. /// Creates a <see cref="Matrix3x2"/> value for 2D scaling.
  407. /// </summary>
  408. /// <param name="xScale">The scale factor for the x-axis.</param>
  409. /// <param name="yScale">The scale factor for the y-axis.</param>
  410. /// <returns>The <see cref="Matrix3x2"/> value created by this method.</returns>
  411. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  412. public static Matrix3x2 CreateScale(in float xScale, in float yScale)
  413. {
  414. CreateScale(xScale, yScale, out Matrix3x2 result);
  415. return result;
  416. }
  417. /// <summary>
  418. /// Creates a <see cref="Matrix3x2"/> value for 2D scaling.
  419. /// </summary>
  420. /// <param name="xScale">The scale factor for the x-axis.</param>
  421. /// <param name="yScale">The scale factor for the y-axis.</param>
  422. /// <param name="result">
  423. /// When this method returns, contains the resulting <see cref="Matrix3x2"/> value for 2D scaling. This parameter
  424. /// is passed uninitialized.
  425. /// </param>
  426. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  427. public static void CreateScale(in float xScale, in float yScale, out Matrix3x2 result)
  428. {
  429. result.M11 = xScale;
  430. result.M12 = 0;
  431. result.M21 = 0;
  432. result.M22 = yScale;
  433. result.M31 = 0;
  434. result.M32 = 0;
  435. }
  436. /// <summary>
  437. /// Creates a <see cref="Matrix3x2"/> value for 2D translation.
  438. /// </summary>
  439. /// <param name="vector">The translation vector</param>
  440. /// <returns>The resulting <see cref="Matrix3x2"/> value for 2D translation.</returns>
  441. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  442. public static Matrix3x2 CreateTranslation(Vector2 vector) => CreateTranslation(vector.X, vector.Y);
  443. /// <summary>
  444. /// Creates a <see cref="Matrix3x2"/> for 2D translation.
  445. /// </summary>
  446. /// <param name="vector">The translation vector</param>
  447. /// <param name="result">
  448. /// When this method returns, contains the resulting <see cref="Matrix3x2"/> value for 2D translation. This
  449. /// parameter is passed uninitialized.
  450. /// </param>
  451. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  452. public static void CreateTranslation(Vector2 vector, out Matrix3x2 result) =>
  453. CreateTranslation(vector.X, vector.Y, out result);
  454. /// <summary>
  455. /// Creates a <see cref="Matrix3x2"/> value for 2D translation.
  456. /// </summary>
  457. /// <param name="x">The X-coordinate of the translation vector.</param>
  458. /// <param name="y">The Y-coordinate of the translation vector.</param>
  459. /// <returns>The resulting <see cref="Matrix3x2"/> value for 2D translation.</returns>
  460. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  461. public static Matrix3x2 CreateTranslation(in float x, in float y)
  462. {
  463. CreateTranslation(x, y, out Matrix3x2 result);
  464. return result;
  465. }
  466. /// <summary>
  467. /// Creates a <see cref="Matrix3x2"/> value for 2D translation.
  468. /// </summary>
  469. /// <param name="x">The X-coordinate of the translation vector.</param>
  470. /// <param name="y">The Y-coordinate of the translation vector.</param>
  471. /// <param name="result">
  472. /// When this method returns, contains the resulting <see cref="Matrix3x2"/> value. This parameter is passed
  473. /// uninitialized.
  474. /// </param>
  475. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  476. public static void CreateTranslation(in float x, in float y, out Matrix3x2 result)
  477. {
  478. result.M11 = 1;
  479. result.M12 = 0;
  480. result.M21 = 0;
  481. result.M22 = 1;
  482. result.M31 = x;
  483. result.M32 = y;
  484. }
  485. /// <summary>
  486. /// Inverts the provided <see cref="Matrix3x2"/>
  487. /// </summary>
  488. /// <param name="matrix">The <see cref="Matrix3x2"/> value to invert.</param>
  489. /// <returns>The result of inverting the <paramref name="matrix"/>.</returns>
  490. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  491. public static Matrix3x2 Invert(Matrix3x2 matrix)
  492. {
  493. Invert(ref matrix);
  494. return matrix;
  495. }
  496. /// <summary>
  497. /// Inverts the provided <see cref="Matrix3x2"/> value.
  498. /// </summary>
  499. /// <param name="matrix">The <see cref="Matrix3x2"/> value to invert.</param>
  500. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  501. public static void Invert(ref Matrix3x2 matrix)
  502. {
  503. var det = 1.0f / matrix.Determinant();
  504. if (float.IsInfinity(det)) // Det(M) = 0
  505. {
  506. matrix = Identity;
  507. return;
  508. }
  509. // The new 3x2 matrix is the first and second column of the inverse of the 3x3 matrix given by adding the third column (0, 0, 1) to the input matrix
  510. matrix = new(
  511. matrix.M22 * det,
  512. -matrix.M12 * det,
  513. -matrix.M21 * det,
  514. matrix.M11 * det,
  515. (matrix.M32 * matrix.M21 - matrix.M31 * matrix.M22) * det,
  516. (matrix.M31 * matrix.M12 - matrix.M32 * matrix.M11) * det
  517. );
  518. }
  519. /// <summary>
  520. /// Adds the elements of two <see cref="Matrix3x2"/> values.
  521. /// </summary>
  522. /// <remarks>
  523. /// This operation is performed component-wise.
  524. /// </remarks>
  525. /// <param name="left">The first <see cref="Matrix3x2"/> value.</param>
  526. /// <param name="right">The second <see cref="Matrix3x2"/> value.</param>
  527. /// <returns>The result of the addition.</returns>
  528. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  529. public static Matrix3x2 Add(Matrix3x2 left, Matrix3x2 right) => left + right;
  530. /// <summary>
  531. /// Adds the elements of two <see cref="Matrix3x2"/> values.
  532. /// </summary>
  533. /// <remarks>
  534. /// This operation is performed component-wise.
  535. /// </remarks>
  536. /// <param name="left">The first <see cref="Matrix3x2"/> value.</param>
  537. /// <param name="right">The second <see cref="Matrix3x2"/> value.</param>
  538. /// <param name="result">
  539. /// When this method returns, contains the result of the addition. This parameter is passed uninitialized.
  540. /// </param>
  541. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  542. public static void Add(Matrix3x2 left, Matrix3x2 right, out Matrix3x2 result) => result = Add(left, right);
  543. /// <summary>
  544. /// Subtracts the elements of a <see cref="Matrix3x2"/> from the corresponding elements of another
  545. /// <see cref="Matrix3x2"/>.
  546. /// </summary>
  547. /// <remarks>
  548. /// This operation is performed component-wise.
  549. /// </remarks>
  550. /// <param name="left">The first <see cref="Matrix3x2"/> value.</param>
  551. /// <param name="right">The second <see cref="Matrix3x2"/> value.</param>
  552. /// <returns>The result of the subtraction.</returns>
  553. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  554. public static Matrix3x2 Subtract(Matrix3x2 left, Matrix3x2 right) => left - right;
  555. /// <summary>
  556. /// Subtracts the elements of a <see cref="Matrix3x2"/> from the corresponding elements of another
  557. /// <see cref="Matrix3x2"/>.
  558. /// </summary>
  559. /// <remarks>
  560. /// This operation is performed component-wise.
  561. /// </remarks>
  562. /// <param name="left">The first <see cref="Matrix3x2"/> value.</param>
  563. /// <param name="right">The second <see cref="Matrix3x2"/> value.</param>
  564. /// <param name="result">
  565. /// When this method returns, contains the result of the subtraction. This method is passed uninitialized.
  566. /// </param>
  567. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  568. public static void Subtract(Matrix3x2 left, Matrix3x2 right, out Matrix3x2 result) => result = Subtract(left, right);
  569. /// <summary>
  570. /// Multiplies the elements of a <see cref="Matrix3x2"/> by the elements of another <see cref="Matrix3x2"/>.
  571. /// </summary>
  572. /// <remarks>
  573. /// This operation performs matrix multiplication.
  574. /// </remarks>
  575. /// <param name="left">The first <see cref="Matrix3x2"/> value.</param>
  576. /// <param name="right">The second <see cref="Matrix3x2"/> value.</param>
  577. /// <returns>The result of the multiplication.</returns>
  578. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  579. public static Matrix3x2 Multiply(Matrix3x2 left, Matrix3x2 right) => left * right;
  580. /// <summary>
  581. /// Multiplies the elements of a <see cref="Matrix3x2"/> by the elements of another <see cref="Matrix3x2"/>.
  582. /// </summary>
  583. /// <remarks>
  584. /// This operation performs matrix multiplication.
  585. /// </remarks>
  586. /// <param name="left">The first <see cref="Matrix3x2"/> value.</param>
  587. /// <param name="right">The second <see cref="Matrix3x2"/> value.</param>
  588. /// <param name="result">
  589. /// When this method returns, contains the result of the multiplication. This parameter is passed uninitialized.
  590. /// </param>
  591. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  592. public static void Multiply(Matrix3x2 left, Matrix3x2 right, out Matrix3x2 result) => result = left * right;
  593. /// <summary>
  594. /// Multiplies the elements of a <see cref="Matrix3x2"/> by the elements of another <see cref="Matrix3x2"/>.
  595. /// </summary>
  596. /// <remarks>
  597. /// This operation performs matrix multiplication.
  598. /// </remarks>
  599. /// <param name="left">The first <see cref="Matrix3x2"/> value.</param>
  600. /// <param name="right">The second <see cref="Matrix3x2"/> value.</param>
  601. /// <param name="result">
  602. /// When this method returns, contains the result of the multiplication. This parameter is passed uninitialized.
  603. /// </param>
  604. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  605. public static void Multiply(ref Matrix3x2 left, ref Matrix3x2 right, out Matrix3x2 result) => result = left * right;
  606. /// <summary>
  607. /// Multiplies the elements of a <see cref="Matrix3x2"/> by a scalar value.
  608. /// </summary>
  609. /// <remarks>
  610. /// This operation is performed component-wise.
  611. /// </remarks>
  612. /// <param name="matrix">The source <see cref="Matrix3x2"/>.</param>
  613. /// <param name="scalar">The scalar value.</param>
  614. /// <returns>The result of the multiplication.</returns>
  615. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  616. public static Matrix3x2 Multiply(Matrix3x2 matrix, in float scalar) => matrix * scalar;
  617. /// <summary>
  618. /// Multiplies the elements of a <see cref="Matrix3x2"/> by a scalar value.
  619. /// </summary>
  620. /// <remarks>
  621. /// This operation is performed component-wise.
  622. /// </remarks>
  623. /// <param name="matrix">The source <see cref="Matrix3x2"/>.</param>
  624. /// <param name="scalar">The scalar value.</param>
  625. /// <param name="result">
  626. /// When this method returns, contains the result of the multiplication. This parameter is passed uninitialized.
  627. /// </param>
  628. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  629. public static void Multiply(Matrix3x2 matrix, in float scalar, out Matrix3x2 result) => result = Multiply(matrix, scalar);
  630. /// <summary>
  631. /// Divides the elements of a <see cref="Matrix3x2"/> by the elements of another <see cref="Matrix3x2"/>.
  632. /// </summary>
  633. /// <remarks>
  634. /// This operation is performed component-wise.
  635. /// </remarks>
  636. /// <param name="left">The first <see cref="Matrix3x2"/> value.</param>
  637. /// <param name="right">The second <see cref="Matrix3x2"/> value.</param>
  638. /// <returns>The result of the division.</returns>
  639. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  640. public static Matrix3x2 Divide(Matrix3x2 left, Matrix3x2 right) => left / right;
  641. /// <summary>
  642. /// Divides the elements of a <see cref="Matrix3x2"/> by the elements of another <see cref="Matrix3x2"/>.
  643. /// </summary>
  644. /// <remarks>
  645. /// This operation is performed component-wise.
  646. /// </remarks>
  647. /// <param name="left">The first <see cref="Matrix3x2"/> value.</param>
  648. /// <param name="right">The second <see cref="Matrix3x2"/> value.</param>
  649. /// <param name="result">
  650. /// When this method returns, contains the result of the division. This parameter is passed uninitialized.
  651. /// </param>
  652. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  653. public static void Divide(Matrix3x2 left, Matrix3x2 right, out Matrix3x2 result) => result = Divide(left, right);
  654. /// <summary>
  655. /// Divides the elements of a <see cref="Matrix3x2"/> by a scalar value.
  656. /// </summary>
  657. /// <remarks>
  658. /// This operation is performed component-wise.
  659. /// </remarks>
  660. /// <param name="matrix">The source <see cref="Matrix3x2"/>.</param>
  661. /// <param name="scalar">The scalar value.</param>
  662. /// <returns>The result of the division.</returns>
  663. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  664. public static Matrix3x2 Divide(Matrix3x2 matrix, in float scalar) => matrix / scalar;
  665. /// <summary>
  666. /// Divides the elements of a <see cref="Matrix3x2"/> by a scalar value.
  667. /// </summary>
  668. /// <remarks>
  669. /// This operation is performed component-wise.
  670. /// </remarks>
  671. /// <param name="matrix">The source <see cref="Matrix3x2"/>.</param>
  672. /// <param name="scalar">The scalar value.</param>
  673. /// <param name="result">
  674. /// When this method returns, contains the result of the division. This parameter is passed uninitialized.
  675. /// </param>
  676. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  677. public static void Divide(Matrix3x2 matrix, in float scalar, out Matrix3x2 result) => result = Divide(matrix, scalar);
  678. /// <inheritdoc />
  679. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  680. public override readonly bool Equals([NotNullWhen(true)] object obj) => obj is Matrix3x2 other && Equals(other);
  681. /// <inheritdoc />
  682. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  683. public readonly bool Equals(Matrix3x2 other) => this == other;
  684. /// <inheritdoc />
  685. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  686. public override readonly int GetHashCode() => HashCode.Combine(X, Y, Z);
  687. /// <summary>
  688. /// Converts the specified <see cref="Matrix3x2"/> value into a <see cref="Matrix"/> value.
  689. /// </summary>
  690. /// <remarks>
  691. /// The third row of the resulting <see cref="Matrix"/> is set to (0, 0, 1, 0), and the fourth row is set to
  692. /// (<see cref="M31"/>, <see cref="M32"/>, 0, 1).
  693. /// </remarks>
  694. /// <returns>The resulting <see cref="Matrix"/> value.</returns>
  695. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  696. public Matrix ToMatrix() => ToMatrix(0.0f);
  697. /// <summary>
  698. /// Converts the specified <see cref="Matrix3x2"/> value into a <see cref="Matrix"/> value.
  699. /// </summary>
  700. /// <remarks>
  701. /// The third row of the resulting <see cref="Matrix"/> is set to (0, 0, 1, 0), and the fourth row is set to
  702. /// (<see cref="M31"/>, <see cref="M32"/>, <paramref name="depth"/>, 1).
  703. /// </remarks>
  704. /// <param name="depth">
  705. /// The depth value to be used for the third row of the resulting <see cref="Matrix"/> value.
  706. /// </param>
  707. /// <returns>The resulting <see cref="Matrix"/> value.</returns>
  708. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  709. public Matrix ToMatrix(in float depth)
  710. {
  711. ToMatrix(depth, out Matrix result);
  712. return result;
  713. }
  714. /// <summary>
  715. /// Converts the specified <see cref="Matrix3x2"/> value into a <see cref="Matrix"/> value.
  716. /// </summary>
  717. /// <remarks>
  718. /// The third row of the resulting <see cref="Matrix"/> is set to (0, 0, 1, 0), and the fourth row is set to
  719. /// (<see cref="M31"/>, <see cref="M32"/>, 0, 1).
  720. /// </remarks>
  721. /// <param name="result">
  722. /// When this method returns, contains the resulting <see cref="Matrix"/> value. This parameter is passed
  723. /// uninitialized.
  724. /// </param>
  725. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  726. public void ToMatrix(out Matrix result) => ToMatrix(0.0f, out result);
  727. /// <summary>
  728. /// Converts the specified <see cref="Matrix3x2"/> value into a <see cref="Matrix"/> value.
  729. /// </summary>
  730. /// <remarks>
  731. /// The third row of the resulting <see cref="Matrix"/> is set to (0, 0, 1, 0), and the fourth row is set to
  732. /// (<see cref="M31"/>, <see cref="M32"/>, <paramref name="depth"/>, 1).
  733. /// </remarks>
  734. /// <param name="depth">
  735. /// The depth value to be used for the third row of the resulting <see cref="Matrix"/> value.
  736. /// </param>
  737. /// <param name="result">
  738. /// When this method returns, contains the resulting <see cref="Matrix"/> value. This parameter is passed
  739. /// uninitialized.
  740. /// </param>
  741. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  742. public void ToMatrix(in float depth, out Matrix result) => ToMatrix(this, depth, out result);
  743. /// <summary>
  744. /// Converts the specified <see cref="Matrix3x2"/> value into a <see cref="Matrix"/> value.
  745. /// </summary>
  746. /// <remarks>
  747. /// The third row of the resulting <see cref="Matrix"/> is set to (0, 0, 1, 0), and the fourth row is set to
  748. /// (<see cref="M31"/>, <see cref="M32"/>, 0, 1).
  749. /// </remarks>
  750. /// <param name="matrix">The source <see cref="Matrix3x2"/> value.</param>
  751. /// <returns>The resulting <see cref="Matrix"/> value.</returns>
  752. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  753. public static Matrix ToMatrix(Matrix3x2 matrix) => ToMatrix(matrix, 0.0f);
  754. /// <summary>
  755. /// Converts the specified <see cref="Matrix3x2"/> value into a <see cref="Matrix"/> value.
  756. /// </summary>
  757. /// <remarks>
  758. /// The third row of the resulting <see cref="Matrix"/> is set to (0, 0, 1, 0), and the fourth row is set to
  759. /// (<see cref="M31"/>, <see cref="M32"/>, <paramref name="depth"/>, 1).
  760. /// </remarks>
  761. /// <param name="matrix">The source <see cref="Matrix3x2"/> value.</param>
  762. /// <param name="depth">
  763. /// The depth value to be used for the third row of the resulting <see cref="Matrix"/> value.
  764. /// </param>
  765. /// <returns>The resulting <see cref="Matrix"/> value.</returns>
  766. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  767. public static Matrix ToMatrix(Matrix3x2 matrix, in float depth)
  768. {
  769. ToMatrix(matrix, depth, out Matrix result);
  770. return result;
  771. }
  772. /// <summary>
  773. /// Converts the specified <see cref="Matrix3x2"/> value into a <see cref="Matrix"/> value.
  774. /// </summary>
  775. /// <remarks>
  776. /// The third row of the resulting <see cref="Matrix"/> is set to (0, 0, 1, 0), and the fourth row is set to
  777. /// (<see cref="M31"/>, <see cref="M32"/>, 0, 1).
  778. /// </remarks>
  779. /// <param name="matrix">The source <see cref="Matrix3x2"/> value.</param>
  780. /// <param name="result">
  781. /// When this method returns, contains the resulting <see cref="Matrix"/> value. This parameter is passed
  782. /// uninitialized.
  783. /// </param>
  784. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  785. public static void ToMatrix(Matrix3x2 matrix, out Matrix result) => ToMatrix(matrix, 0.0f, out result);
  786. /// <summary>
  787. /// Converts the specified <see cref="Matrix3x2"/> value into a <see cref="Matrix"/> value.
  788. /// </summary>
  789. /// <remarks>
  790. /// The third row of the resulting <see cref="Matrix"/> is set to (0, 0, 1, 0), and the fourth row is set to
  791. /// (<see cref="M31"/>, <see cref="M32"/>, <paramref name="depth"/>, 1).
  792. /// </remarks>
  793. /// <param name="matrix">The source <see cref="Matrix3x2"/> value.</param>
  794. /// <param name="depth">
  795. /// The depth value to be used for the third row of the resulting <see cref="Matrix"/> value.
  796. /// </param>
  797. /// <param name="result">
  798. /// When this method returns, contains the resulting <see cref="Matrix"/> value. This parameter is passed
  799. /// uninitialized.
  800. /// </param>
  801. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  802. public static void ToMatrix(Matrix3x2 matrix, in float depth, out Matrix result)
  803. {
  804. result.M11 = matrix.M11;
  805. result.M12 = matrix.M12;
  806. result.M13 = 0;
  807. result.M14 = 0;
  808. result.M21 = matrix.M21;
  809. result.M22 = matrix.M22;
  810. result.M23 = 0;
  811. result.M24 = 0;
  812. result.M31 = 0;
  813. result.M32 = 0;
  814. result.M33 = 1;
  815. result.M34 = 0;
  816. result.M41 = matrix.M31;
  817. result.M42 = matrix.M32;
  818. result.M43 = depth;
  819. result.M44 = 1;
  820. }
  821. /// <summary>
  822. /// Converts a <see cref="Matrix3x2"/> value to a <see cref="Matrix"/> value.
  823. /// </summary>
  824. /// <param name="matrix">The source <see cref="Matrix3x2"/> value.</param>
  825. /// <returns>The resulting <see cref="Matrix"/> value.</returns>
  826. public static implicit operator Matrix(Matrix3x2 matrix) => ToMatrix(matrix, 0.0f);
  827. /// <summary>
  828. /// Checks if two <see cref="Matrix3x2"/> values are equal.
  829. /// </summary>
  830. /// <remarks>
  831. /// Two <see cref="Matrix3x2"/> values are considered equal if all their corresponding elements are equal.
  832. /// </remarks>
  833. /// <param name="left">The first <see cref="Matrix3x2"/> value.</param>
  834. /// <param name="right">The second <see cref="Matrix3x2"/> value.</param>
  835. /// <returns>True if the values are equal; otherwise, false.</returns>
  836. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  837. public static bool operator ==(Matrix3x2 left, Matrix3x2 right) =>
  838. (left.X == right.X) && (left.Y == right.Y) && (left.Z == right.Z);
  839. /// <summary>
  840. /// Checks if two <see cref="Matrix3x2"/> values are not equal.
  841. /// </summary>
  842. /// <remarks>
  843. /// Two <see cref="Matrix3x2"/> values are considered not equal if any of their corresponding elements differ.
  844. /// </remarks>
  845. /// <param name="left">The first <see cref="Matrix3x2"/> value.</param>
  846. /// <param name="right">The second <see cref="Matrix3x2"/> value.</param>
  847. /// <returns>True if the values are not equal; otherwise, false.</returns>
  848. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  849. public static bool operator !=(Matrix3x2 left, Matrix3x2 right) =>
  850. (left.X != right.X) || (left.Y != right.Y) || (left.Z != right.Z);
  851. /// <inheritdoc cref="Add(Matrix3x2, Matrix3x2)"/>
  852. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  853. public static Matrix3x2 operator +(Matrix3x2 left, Matrix3x2 right)
  854. {
  855. Matrix3x2 result;
  856. result.M11 = left.M11 + right.M11;
  857. result.M12 = left.M12 + right.M12;
  858. result.M21 = left.M21 + right.M21;
  859. result.M22 = left.M22 + right.M22;
  860. result.M31 = left.M31 + right.M31;
  861. result.M32 = left.M32 + right.M32;
  862. return result;
  863. }
  864. /// <inheritdoc cref="Subtract(Matrix3x2, Matrix3x2)"/>
  865. public static Matrix3x2 operator -(Matrix3x2 left, Matrix3x2 right)
  866. {
  867. Matrix3x2 result;
  868. result.M11 = left.M11 - right.M11;
  869. result.M12 = left.M12 - right.M12;
  870. result.M21 = left.M21 - right.M21;
  871. result.M22 = left.M22 - right.M22;
  872. result.M31 = left.M31 - right.M31;
  873. result.M32 = left.M32 - right.M32;
  874. return result;
  875. }
  876. /// <summary>
  877. /// Negates the elements of a <see cref="Matrix3x2"/>.
  878. /// </summary>
  879. /// <remarks>
  880. /// This operation is performed component-wise.
  881. /// </remarks>
  882. /// <param name="matrix">The source <see cref="Matrix3x2"/>.</param>
  883. /// <returns>The result of the negation.</returns>
  884. public static Matrix3x2 operator -(Matrix3x2 matrix)
  885. {
  886. Matrix3x2 result;
  887. result.M11 = -matrix.M11;
  888. result.M12 = -matrix.M12;
  889. result.M21 = -matrix.M21;
  890. result.M22 = -matrix.M22;
  891. result.M31 = -matrix.M31;
  892. result.M32 = -matrix.M32;
  893. return result;
  894. }
  895. /// <inheritdoc cref="Multiply(Matrix3x2, Matrix3x2)"/>
  896. public static Matrix3x2 operator *(Matrix3x2 left, Matrix3x2 right)
  897. {
  898. Matrix3x2 result;
  899. result.M11 = left.M11 * right.M11 + left.M12 * right.M21;
  900. result.M12 = left.M11 * right.M12 + left.M12 * right.M22;
  901. result.M21 = left.M21 * right.M11 + left.M22 * right.M21;
  902. result.M22 = left.M21 * right.M12 + left.M22 * right.M22;
  903. result.M31 = left.M31 * right.M11 + left.M32 * right.M21 + right.M31;
  904. result.M32 = left.M31 * right.M12 + left.M32 * right.M22 + right.M32;
  905. return result;
  906. }
  907. /// <inheritdoc cref="Multiply(Matrix3x2, in float)"/>
  908. public static Matrix3x2 operator *(Matrix3x2 matrix, in float scalar)
  909. {
  910. Matrix3x2 result;
  911. result.M11 = matrix.M11 * scalar;
  912. result.M12 = matrix.M12 * scalar;
  913. result.M21 = matrix.M21 * scalar;
  914. result.M22 = matrix.M22 * scalar;
  915. result.M31 = matrix.M31 * scalar;
  916. result.M32 = matrix.M32 * scalar;
  917. return result;
  918. }
  919. /// <inheritdoc cref="Divide(Matrix3x2, Matrix3x2)"/>
  920. public static Matrix3x2 operator /(Matrix3x2 left, Matrix3x2 right)
  921. {
  922. Matrix3x2 result;
  923. result.M11 = left.M11 / right.M11;
  924. result.M12 = left.M12 / right.M12;
  925. result.M21 = left.M21 / right.M21;
  926. result.M22 = left.M22 / right.M22;
  927. result.M31 = left.M31 / right.M31;
  928. result.M32 = left.M32 / right.M32;
  929. return result;
  930. }
  931. /// <inheritdoc cref="Divide(Matrix3x2, in float)"/>
  932. public static Matrix3x2 operator /(Matrix3x2 matrix, in float scalar)
  933. {
  934. var num = 1.0f / scalar;
  935. Matrix3x2 result;
  936. result.M11 = matrix.M11 * num;
  937. result.M12 = matrix.M12 * num;
  938. result.M21 = matrix.M21 * num;
  939. result.M22 = matrix.M22 * num;
  940. result.M31 = matrix.M31 * num;
  941. result.M32 = matrix.M32 * num;
  942. return result;
  943. }
  944. /// <summary>
  945. /// Returns a string representation of this <see cref="Matrix3x2"/>
  946. /// </summary>
  947. /// <returns>The string representation of this <see cref="Matrix3x2"/></returns>
  948. public override string ToString() =>
  949. $"{{M11:{M11} M12:{M12}}} {{M21:{M21} M22:{M22}}} {{M31:{M31} M32:{M32}}}";
  950. internal string DebugDisplayString()
  951. {
  952. if (this == Identity)
  953. {
  954. return nameof(Identity);
  955. }
  956. Decompose(out Vector2 translation, out float rotation, out Vector2 scale);
  957. return $"T:({translation.X:0.##},{translation.Y:0.##}), R:{MathHelper.ToDegrees(rotation):0.##}°, S:({scale.X:0.##},{scale.Y:0.##})";
  958. }
  959. }