-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from dotnet-campus/t/sewzc/fix_segment_intersec…
…tion 修复线段计算没有交点的问题
- Loading branch information
Showing
3 changed files
with
76 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters