LIne2DTest.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // MonoGame - Copyright (C) MonoGame Foundation, Inc
  2. // This file is subject to the terms and conditions defined in
  3. // file 'LICENSE.txt', which is part of this source code package.
  4. using System;
  5. using Microsoft.Xna.Framework;
  6. namespace MonoGame.Extended.Tests
  7. {
  8. public sealed class Line2DTest
  9. {
  10. #region Constructor Tests
  11. [Fact]
  12. public void Constructor()
  13. {
  14. var normal = new Vector2(0, 1);
  15. var distance = 5.0f;
  16. var line = new Line2D(normal, distance);
  17. Assert.Equal(normal, line.Normal);
  18. Assert.Equal(distance, line.Distance);
  19. }
  20. #endregion
  21. #region Factory Method Tests
  22. [Fact]
  23. public void CreateFromPointAndNormal()
  24. {
  25. var point = new Vector2(0, 5);
  26. var normal = new Vector2(0, 1);
  27. var line = Line2D.CreateFromPointAndNormal(point, normal);
  28. Assert.Equal(new Vector2(0, 1), line.Normal);
  29. Assert.Equal(5.0f, line.Distance, Collision2D.Epsilon);
  30. }
  31. [Fact]
  32. public void CreateFromTwoPoints()
  33. {
  34. var p1 = new Vector2(0, 0);
  35. var p2 = new Vector2(10, 0);
  36. var line = Line2D.CreateFromTwoPoints(p1, p2);
  37. Assert.Equal(0, MathF.Abs(line.Normal.X), Collision2D.Epsilon);
  38. Assert.Equal(1, MathF.Abs(line.Normal.Y), Collision2D.Epsilon);
  39. }
  40. [Fact]
  41. public void CreateFromTwoPoints_ThrowsWhenPointsTooClose()
  42. {
  43. var p1 = new Vector2(0, 0);
  44. var p2 = new Vector2(1e-7f, 0);
  45. Assert.Throws<ArgumentException>(() => Line2D.CreateFromTwoPoints(p1, p2));
  46. }
  47. [Fact]
  48. public void CreateFromPointAndDirection()
  49. {
  50. var point = new Vector2(5, 0);
  51. var direction = new Vector2(1, 0);
  52. var line = Line2D.CreateFromPointAndDirection(point, direction);
  53. Assert.Equal(0, MathF.Abs(line.Normal.X), Collision2D.Epsilon);
  54. Assert.Equal(1, MathF.Abs(line.Normal.Y), Collision2D.Epsilon);
  55. }
  56. #endregion
  57. #region Distance and Projection Tests (Delegation Spot Checks)
  58. [Fact]
  59. public void DistanceToPoint_PointOnLine()
  60. {
  61. var line = new Line2D(new Vector2(0, 1), 5);
  62. var point = new Vector2(0, 5);
  63. float distance = line.DistanceToPoint(point);
  64. Assert.Equal(0.0f, distance, Collision2D.Epsilon);
  65. }
  66. [Fact]
  67. public void DistanceToPoint_PointOffLine()
  68. {
  69. var line = new Line2D(new Vector2(0, 1), 5);
  70. var point = new Vector2(0, 8);
  71. float distance = line.DistanceToPoint(point);
  72. Assert.Equal(3.0f, distance, Collision2D.Epsilon);
  73. }
  74. [Fact]
  75. public void ClosestPoint_ReturnsProjection()
  76. {
  77. var line = new Line2D(new Vector2(0, 1), 5);
  78. var point = new Vector2(3, 8);
  79. var closest = line.ClosestPoint(point, out float distanceAlongLine);
  80. Assert.Equal(new Vector2(3, 5), closest);
  81. }
  82. #endregion
  83. #region Normalize Tests
  84. [Fact]
  85. public void Normalize_Static_CreatesUnitNormal()
  86. {
  87. var line = new Line2D(new Vector2(3, 4), 10);
  88. var normalized = Line2D.Normalize(line);
  89. Assert.Equal(1.0f, normalized.Normal.Length(), Collision2D.Epsilon);
  90. }
  91. [Fact]
  92. public void Normalize_StaticRef_CreatesUnitNormal()
  93. {
  94. var line = new Line2D(new Vector2(3, 4), 10);
  95. Line2D.Normalize(ref line, out var normalized);
  96. Assert.Equal(1.0f, normalized.Normal.Length(), Collision2D.Epsilon);
  97. }
  98. [Fact]
  99. public void Normalize_Instance_ModifiesInPlace()
  100. {
  101. var line = new Line2D(new Vector2(3, 4), 10);
  102. line.Normalize();
  103. Assert.Equal(1.0f, line.Normal.Length(), Collision2D.Epsilon);
  104. }
  105. [Fact]
  106. public void Normalize_AlreadyNormalized_RemainsUnchanged()
  107. {
  108. var line = new Line2D(new Vector2(0, 1), 5);
  109. var original = line;
  110. line.Normalize();
  111. Assert.Equal(original.Normal, line.Normal);
  112. Assert.Equal(original.Distance, line.Distance);
  113. }
  114. #endregion
  115. #region Deconstruct Test
  116. [Fact]
  117. public void Deconstruct()
  118. {
  119. var line = new Line2D(new Vector2(0, 1), 5);
  120. var (normal, distance) = line;
  121. Assert.Equal(new Vector2(0, 1), normal);
  122. Assert.Equal(5.0f, distance);
  123. }
  124. #endregion
  125. }
  126. }