Skip to content

Commit

Permalink
Merge pull request #3 from dotnet-campus/t/sewzc/fix_segment_intersec…
Browse files Browse the repository at this point in the history
…tion

修复线段计算没有交点的问题
  • Loading branch information
lindexi authored Sep 11, 2024
2 parents b9f85bf + a3d7774 commit 8fd2a5e
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 7 deletions.
75 changes: 75 additions & 0 deletions DotNetCampus.Numerics.Geometry.Tests/Segment2DTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using JetBrains.Annotations;
using Xunit;

namespace DotNetCampus.Numerics.Geometry.Tests;

[TestSubject(typeof(Segment2D))]
public class Segment2DTest
{
#region 成员方法

[Fact(DisplayName = "测试线段交点。")]
public void IntersectionTest()
{
// Arrange
var point1 = new Point2D(0, 0);
var point2 = new Point2D(4, 4);
var segment1 = Segment2D.Create(point1, point2);

var point3 = new Point2D(0, 4);
var point4 = new Point2D(4, 0);
var segment2 = Segment2D.Create(point3, point4);

var expectedIntersection = new Point2D(2, 2);

// Act
var intersection = segment1.Intersection(segment2);

// Assert
Assert.NotNull(intersection);
Assert.Equal(expectedIntersection, intersection.Value, GeometryNumericsEqualHelper.IsAlmostEqual);
}

[Fact(DisplayName = "测试平行线段无交点。")]
public void ParallelSegmentsNoIntersectionTest()
{
// Arrange
var point1 = new Point2D(0, 0);
var point2 = new Point2D(4, 0);
var segment1 = Segment2D.Create(point1, point2);

var point3 = new Point2D(0, 1);
var point4 = new Point2D(4, 1);
var segment2 = Segment2D.Create(point3, point4);

// Act
var intersection = segment1.Intersection(segment2);

// Assert
Assert.Null(intersection);
}

[Fact(DisplayName = "测试线段端点相交。")]
public void EndpointIntersectionTest()
{
// Arrange
var point1 = new Point2D(4, 8);
var point2 = new Point2D(4, 4);
var segment1 = Segment2D.Create(point1, point2);

var point3 = new Point2D(4, 4);
var point4 = new Point2D(8, 8);
var segment2 = Segment2D.Create(point3, point4);

var expectedIntersection = new Point2D(4, 4);

// Act
var intersection = segment1.Intersection(segment2);

// Assert
Assert.NotNull(intersection);
Assert.Equal(expectedIntersection, intersection.Value, GeometryNumericsEqualHelper.IsAlmostEqual);
}

#endregion
}
2 changes: 1 addition & 1 deletion DotNetCampus.Numerics.Geometry/Segment2D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static Segment2D Create(Point2D point1, Point2D point2)
var position = vector.Det(other.UnitDirectionVector) / det;
var radio = position / Length;
var radio2 = vector.Det(UnitDirectionVector) / det / other.Length;
if (radio.IsInZeroToOne() || radio2.IsInZeroToOne())
if (!radio.IsInZeroToOne() || !radio2.IsInZeroToOne())
return null;

return Line.GetPoint(position);
Expand Down
6 changes: 0 additions & 6 deletions DotNetCampus.Numerics.Tests/NumAssert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,5 @@ public static void NotCloseZero<TNum>(TNum actual)
throw new NotSupportedException($"暂时不支持类型 {typeof(TNum).FullName}。");
}

public static void Equal<TNum>(TNum expected, TNum actual)
where TNum : unmanaged, IFloatingPoint<TNum>
{
Assert.Equal(expected, actual);
}

#endregion
}

0 comments on commit 8fd2a5e

Please sign in to comment.