ShapeTests.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using Microsoft.Xna.Framework;
  2. using Xunit;
  3. namespace MonoGame.Extended.Tests.Primitives;
  4. public class ShapeTests
  5. {
  6. public class CircleFTests
  7. {
  8. [Fact]
  9. public void CircCircIntersectionSameCircleTest()
  10. {
  11. IShapeF shape1 = new CircleF(Vector2.Zero, 2.0f);
  12. IShapeF shape2 = new CircleF(Vector2.Zero, 2.0f);
  13. Assert.True(shape1.Intersects(shape2));
  14. }
  15. [Fact]
  16. public void CircCircIntersectionOverlappingTest()
  17. {
  18. IShapeF shape1 = new CircleF(new Vector2(1, 2), 2.0f);
  19. IShapeF shape2 = new CircleF(Vector2.Zero, 2.0f);
  20. Assert.True(shape1.Intersects(shape2));
  21. }
  22. [Fact]
  23. public void CircleCircleNotIntersectingTest()
  24. {
  25. IShapeF shape1 = new CircleF(new Vector2(5, 5), 2.0f);
  26. IShapeF shape2 = new CircleF(Vector2.Zero, 2.0f);
  27. Assert.False(shape1.Intersects(shape2));
  28. }
  29. }
  30. public class RectangleFTests
  31. {
  32. [Fact]
  33. public void RectRectSameRectTest()
  34. {
  35. IShapeF shape1 = new RectangleF(Vector2.Zero, new SizeF(5, 5));
  36. IShapeF shape2 = new RectangleF(Vector2.Zero, new SizeF(5, 5));
  37. Assert.True(shape1.Intersects(shape2));
  38. }
  39. [Fact]
  40. public void RectRectIntersectingTest()
  41. {
  42. IShapeF shape1 = new RectangleF(Vector2.Zero, new SizeF(5, 5));
  43. IShapeF shape2 = new RectangleF(new Vector2(3, 3), new SizeF(5, 5));
  44. Assert.True(shape1.Intersects(shape2));
  45. }
  46. [Fact]
  47. public void RectRectNotIntersectingTest()
  48. {
  49. IShapeF shape1 = new RectangleF(Vector2.Zero, new SizeF(5, 5));
  50. IShapeF shape2 = new RectangleF(new Vector2(10, 10), new SizeF(5, 5));
  51. Assert.False(shape1.Intersects(shape2));
  52. }
  53. [Fact]
  54. public void RectCircContainedTest()
  55. {
  56. IShapeF shape1 = new RectangleF(Vector2.Zero, new SizeF(5, 5));
  57. IShapeF shape2 = new CircleF(Vector2.Zero, 4);
  58. Assert.True(shape1.Intersects(shape2));
  59. Assert.True(shape2.Intersects(shape1));
  60. }
  61. [Fact]
  62. public void RectCircOnEdgeTest()
  63. {
  64. IShapeF shape1 = new RectangleF(Vector2.Zero, new SizeF(5, 5));
  65. IShapeF shape2 = new CircleF(new Vector2(5, 0), 4);
  66. Assert.True(shape1.Intersects(shape2));
  67. Assert.True(shape2.Intersects(shape1));
  68. }
  69. }
  70. public class OrientedRectangleTests
  71. {
  72. [Fact]
  73. public void Axis_aligned_rectangle_intersects_circle()
  74. {
  75. /*
  76. * :
  77. * :
  78. * +*+
  79. * ...........* *.........
  80. * +*+
  81. * :
  82. * :
  83. */
  84. IShapeF rectangle = new OrientedRectangle(Vector2.Zero, new SizeF(1, 1), Matrix3x2.Identity);
  85. var circle = new CircleF(Vector2.Zero, 1);
  86. Assert.True(rectangle.Intersects(circle));
  87. }
  88. [Fact]
  89. public void Axis_aligned_rectangle_does_not_intersect_circle_in_top_left()
  90. {
  91. /*
  92. * * :
  93. * * *:
  94. * *+-+
  95. * ...........| |.........
  96. * +-+
  97. * :
  98. * :
  99. */
  100. IShapeF rectangle = new OrientedRectangle(Vector2.Zero, new SizeF(1, 1), Matrix3x2.Identity);
  101. var circle = new CircleF(new Vector2(-2, 1), .99f);
  102. Assert.False(rectangle.Intersects(circle));
  103. }
  104. [Fact]
  105. public void Rectangle_rotated_45_degrees_does_not_intersect_circle_in_bottom_right()
  106. {
  107. /*
  108. * :
  109. * :
  110. * +-.
  111. * .........../ / ........
  112. * +./* *
  113. * * *
  114. * :* *
  115. */
  116. IShapeF rectangle = new OrientedRectangle(new Vector2(-1, 1), new SizeF(1.42f, 1.42f), Matrix3x2.CreateRotationZ(-MathHelper.PiOver4));
  117. var circle = new CircleF(new Vector2(1, -1), 1.4f);
  118. Assert.False(rectangle.Intersects(circle));
  119. }
  120. [Fact]
  121. public void Axis_aligned_rectangle_does_not_intersect_rectangle()
  122. {
  123. /*
  124. * :
  125. * :
  126. * +-+
  127. * ..........| |**.......
  128. * +-+ *
  129. * :**
  130. * :
  131. */
  132. IShapeF rectangle = new OrientedRectangle(new Vector2(-1, 0), new SizeF(1, 1), Matrix3x2.Identity);
  133. var rect = new RectangleF(new Vector2(0.001f, 0), new SizeF(2, 2));
  134. Assert.False(rectangle.Intersects(rect));
  135. }
  136. [Fact]
  137. public void Axis_aligned_rectangle_intersects_rectangle()
  138. {
  139. /*
  140. * :
  141. * :
  142. * +-+
  143. * ..........| |**.......
  144. * +-+ *
  145. * :**
  146. * :
  147. */
  148. IShapeF rectangle = new OrientedRectangle(new Vector2(-1, 0), new SizeF(1, 1), Matrix3x2.Identity);
  149. var rect = new RectangleF(new Vector2(0, 0), new SizeF(2, 2));
  150. Assert.True(rectangle.Intersects(rect));
  151. }
  152. }
  153. }