OrientedRectangleTests.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. using System.Collections.Generic;
  2. using Microsoft.Xna.Framework;
  3. using Xunit;
  4. using Vector2 = Microsoft.Xna.Framework.Vector2;
  5. namespace MonoGame.Extended.Tests.Primitives;
  6. public class OrientedRectangleTests
  7. {
  8. [Fact]
  9. public void Initializes_oriented_rectangle()
  10. {
  11. var rectangle = new OrientedRectangle(new Vector2(1, 2), new SizeF(3, 4), new Matrix3x2(5, 6, 7, 8, 9, 10));
  12. Assert.Equal(new Vector2(1, 2), rectangle.Center);
  13. Assert.Equal(new Vector2(3, 4), rectangle.Radii);
  14. Assert.Equal(new Matrix3x2(5, 6, 7, 8, 9, 10), rectangle.Orientation);
  15. CollectionAssert.Equal(
  16. new List<Vector2>
  17. {
  18. new(-3, -2),
  19. new(-33, -38),
  20. new(23, 26),
  21. new(53, 62)
  22. },
  23. rectangle.Points);
  24. }
  25. public static readonly IEnumerable<object[]> _equalsComparisons = new[]
  26. {
  27. new object[]
  28. {
  29. "empty compared with empty is true",
  30. new OrientedRectangle(Vector2.Zero, SizeF.Empty, Matrix3x2.Identity),
  31. new OrientedRectangle(Vector2.Zero, SizeF.Empty, Matrix3x2.Identity)
  32. },
  33. new object[]
  34. {
  35. "initialized compared with initialized true",
  36. new OrientedRectangle(new Vector2(1, 2), new SizeF(3, 4), new Matrix3x2(5, 6, 7, 8, 9, 10)),
  37. new OrientedRectangle(new Vector2(1, 2), new SizeF(3, 4), new Matrix3x2(5, 6, 7, 8, 9, 10))
  38. }
  39. };
  40. [Theory]
  41. [MemberData(nameof(_equalsComparisons))]
  42. #pragma warning disable xUnit1026
  43. public void Equals_comparison(string name, OrientedRectangle first, OrientedRectangle second)
  44. #pragma warning restore xUnit1026
  45. {
  46. Assert.True(first == second);
  47. Assert.False(first != second);
  48. }
  49. public class Transform
  50. {
  51. [Fact]
  52. public void Center_point_is_not_translated()
  53. {
  54. var rectangle = new OrientedRectangle(new Vector2(1, 2), new SizeF(), Matrix3x2.Identity);
  55. var transform = Matrix3x2.Identity;
  56. var result = OrientedRectangle.Transform(rectangle, ref transform);
  57. Assert.Equal(new Vector2(1, 2), result.Center);
  58. }
  59. [Fact]
  60. public void Center_point_is_translated()
  61. {
  62. var rectangle = new OrientedRectangle(new Vector2(0, 0), new SizeF(), new Matrix3x2());
  63. var transform = Matrix3x2.CreateTranslation(1, 2);
  64. var result = OrientedRectangle.Transform(rectangle, ref transform);
  65. Assert.Equal(new Vector2(1, 2), result.Center);
  66. }
  67. [Fact]
  68. public void Radii_is_not_changed_by_identity_transform()
  69. {
  70. var rectangle = new OrientedRectangle(new Vector2(), new SizeF(10, 20), new Matrix3x2());
  71. var transform = Matrix3x2.Identity;
  72. var result = OrientedRectangle.Transform(rectangle, ref transform);
  73. Assert.Equal(new Vector2(10, 20), result.Radii);
  74. }
  75. [Fact]
  76. public void Radii_is_not_changed_by_translation()
  77. {
  78. var rectangle = new OrientedRectangle(new Vector2(1, 2), new SizeF(10, 20), new Matrix3x2());
  79. var transform = Matrix3x2.CreateTranslation(1, 2);
  80. var result = OrientedRectangle.Transform(rectangle, ref transform);
  81. Assert.Equal(new Vector2(10, 20), result.Radii);
  82. }
  83. [Fact]
  84. public void Orientation_is_rotated_45_degrees_left()
  85. {
  86. var rectangle = new OrientedRectangle(new Vector2(), new SizeF(), Matrix3x2.Identity);
  87. var transform = Matrix3x2.CreateRotationZ(MathHelper.PiOver4);
  88. var result = OrientedRectangle.Transform(rectangle, ref transform);
  89. Assert.Equal(new Vector2(), result.Center);
  90. Assert.Equal(new Vector2(), result.Radii);
  91. Assert.Equal(Matrix3x2.CreateRotationZ(MathHelper.PiOver4), result.Orientation);
  92. }
  93. [Fact]
  94. public void Orientation_is_rotated_to_45_degrees_from_180()
  95. {
  96. var rectangle = new OrientedRectangle(new Vector2(), new SizeF(), Matrix3x2.CreateRotationZ(MathHelper.Pi));
  97. var transform = Matrix3x2.CreateRotationZ(-3 * MathHelper.PiOver4);
  98. var expectedOrientation = Matrix3x2.CreateRotationZ(MathHelper.PiOver4);
  99. var result = OrientedRectangle.Transform(rectangle, ref transform);
  100. Assert.Equal(new Vector2(), result.Center);
  101. Assert.Equal(new Vector2(), result.Radii);
  102. Assert.Equal(expectedOrientation.M11, result.Orientation.M11, 6);
  103. Assert.Equal(expectedOrientation.M12, result.Orientation.M12, 6);
  104. Assert.Equal(expectedOrientation.M21, result.Orientation.M21, 6);
  105. Assert.Equal(expectedOrientation.M22, result.Orientation.M22, 6);
  106. Assert.Equal(expectedOrientation.M31, result.Orientation.M31, 6);
  107. Assert.Equal(expectedOrientation.M32, result.Orientation.M32, 6);
  108. }
  109. [Fact]
  110. public void Points_are_same_as_center()
  111. {
  112. var rectangle = new OrientedRectangle(new Vector2(1, 2), new SizeF(), Matrix3x2.Identity);
  113. var transform = Matrix3x2.Identity;
  114. var result = OrientedRectangle.Transform(rectangle, ref transform);
  115. CollectionAssert.Equal(
  116. new List<Vector2>
  117. {
  118. new(1, 2),
  119. new(1, 2),
  120. new(1, 2),
  121. new(1, 2)
  122. },
  123. result.Points);
  124. }
  125. [Fact]
  126. public void Points_are_translated()
  127. {
  128. var rectangle = new OrientedRectangle(new Vector2(0, 0), new SizeF(2, 4), Matrix3x2.Identity);
  129. var transform = Matrix3x2.CreateTranslation(10, 20);
  130. var result = OrientedRectangle.Transform(rectangle, ref transform);
  131. CollectionAssert.Equal(
  132. new List<Vector2>
  133. {
  134. new(8, 16),
  135. new(8, 24),
  136. new(12, 24),
  137. new(12, 16)
  138. },
  139. result.Points);
  140. }
  141. [Fact]
  142. public void Applies_rotation_and_translation()
  143. {
  144. /* Rectangle with center point p, aligned in coordinate system with origin 0.
  145. *
  146. * :
  147. * :
  148. * +-+
  149. * | |
  150. * |p|
  151. * | |
  152. * ...............0-+............
  153. * :
  154. * :
  155. * :
  156. *
  157. * Rotate around center point p, 90 degrees around origin 0.
  158. *
  159. * :
  160. * :
  161. * +---+
  162. * | p |
  163. * ...........+---0..............
  164. * :
  165. * :
  166. * :
  167. *
  168. * Then translate rectangle by x=10 and y=20.
  169. * :
  170. * : +---+
  171. * : | p |
  172. * y=21 - - - - - - - -> +---+
  173. * .
  174. * :
  175. * ...............0..............
  176. * :
  177. * :
  178. * :
  179. */
  180. var rectangle = new OrientedRectangle(new Vector2(1, 2), new SizeF(2, 4), Matrix3x2.Identity);
  181. var transform =
  182. Matrix3x2.CreateRotationZ(MathHelper.PiOver2)
  183. *
  184. Matrix3x2.CreateTranslation(10, 20);
  185. var result = OrientedRectangle.Transform(rectangle, ref transform);
  186. Assert.Equal(8, result.Center.X, 6);
  187. Assert.Equal(21, result.Center.Y, 6);
  188. Assert.Equal(2, result.Radii.X, 6);
  189. Assert.Equal(4, result.Radii.Y, 6);
  190. Assert.Equal(Matrix3x2.CreateRotationZ(MathHelper.PiOver2), result.Orientation);
  191. CollectionAssert.Equal(
  192. new List<Vector2>
  193. {
  194. new(4, 23),
  195. new(4, 19),
  196. new(12, 19),
  197. new(12, 23)
  198. },
  199. result.Points);
  200. }
  201. }
  202. }