Repository navigation
Expand file tree
/
Copy pathLineIntersection_Tests.cs
More file actions
124 lines (91 loc) · 5.32 KB
/
Copy pathLineIntersection_Tests.cs
File metadata and controls
124 lines (91 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using Advanced.Algorithms.Geometry;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Advanced.Algorithms.Tests.Geometry
{
[TestClass]
public class LineIntersectionTests
{
[TestMethod]
public void LineIntersection_Smoke_Test()
{
var pointComparer = new PointComparer();
var line1 = new Line(new Point(1, 1), new Point(10, 1));
var line2 = new Line(new Point(1, 2), new Point(10, 2));
Assert.IsTrue(pointComparer.Equals(null, LineIntersection.Find(line1, line2)));
line1 = new Line(new Point(10, 0), new Point(0, 10));
line2 = new Line(new Point(0, 10), new Point(10, 10));
Assert.IsTrue(pointComparer.Equals(new Point(0, 10), LineIntersection.Find(line1, line2)));
line1 = new Line(new Point(0, 0), new Point(10, 10));
line2 = new Line(new Point(0, 10), new Point(10, 10));
Assert.IsTrue(pointComparer.Equals(new Point(10, 10), LineIntersection.Find(line1, line2)));
line1 = new Line(new Point(10, 0), new Point(0, 10));
line2 = new Line(new Point(0, 0), new Point(10, 10));
Assert.IsTrue(pointComparer.Equals(new Point(5, 5), LineIntersection.Find(line1, line2)));
line1 = new Line(new Point(-5, -5), new Point(0, 0));
line2 = new Line(new Point(1, 1), new Point(10, 10));
Assert.IsTrue(pointComparer.Equals(default, LineIntersection.Find(line1, line2)));
line1 = new Line(new Point(3, -5), new Point(3, 10));
line2 = new Line(new Point(0, 5), new Point(10, 5));
Assert.IsTrue(pointComparer.Equals(new Point(3, 5), LineIntersection.Find(line1, line2)));
line1 = new Line(new Point(0, 5), new Point(10, 5));
line2 = new Line(new Point(3, -5), new Point(3, 10));
Assert.IsTrue(pointComparer.Equals(new Point(3, 5), LineIntersection.Find(line1, line2)));
line1 = new Line(new Point(0, 5), new Point(10, 5));
line2 = new Line(new Point(3, -5), new Point(5, 15));
Assert.IsTrue(pointComparer.Equals(new Point(4, 5), LineIntersection.Find(line1, line2)));
line1 = new Line(new Point(0, -5), new Point(0, 5));
line2 = new Line(new Point(-3, 0), new Point(3, 0));
Assert.IsTrue(pointComparer.Equals(new Point(0, 0), LineIntersection.Find(line1, line2)));
}
[TestMethod]
public void LineIntersection_Corner_Cases_Test()
{
var pointComparer = new PointComparer();
var verticalA = new Line(new Point(2, 0), new Point(2, 5));
var verticalB = new Line(new Point(4, 0), new Point(4, 5));
Assert.IsNull(LineIntersection.Find(verticalA, verticalB));
var horizontalA = new Line(new Point(0, 3), new Point(5, 3));
var horizontalB = new Line(new Point(0, 7), new Point(5, 7));
Assert.IsNull(LineIntersection.Find(horizontalA, horizontalB));
var overlapVerticalA = new Line(new Point(1, 0), new Point(1, 4));
var overlapVerticalB = new Line(new Point(1, 2), new Point(1, 6));
Assert.IsTrue(pointComparer.Equals(new Point(1, 2),
LineIntersection.Find(overlapVerticalA, overlapVerticalB)));
var overlapHorizontalA = new Line(new Point(0, 1), new Point(4, 1));
var overlapHorizontalB = new Line(new Point(2, 1), new Point(6, 1));
Assert.IsTrue(pointComparer.Equals(new Point(2, 1),
LineIntersection.Find(overlapHorizontalA, overlapHorizontalB)));
var same = new Line(new Point(0, 0), new Point(1, 1));
Assert.ThrowsException<System.ArgumentException>(() => LineIntersection.Find(same, same));
Assert.IsFalse(same.Intersects(new Line(new Point(0, 1), new Point(1, 2))));
Assert.IsTrue(horizontalA.Intersects(verticalA));
Assert.IsTrue(pointComparer.Equals(new Point(2, 3), horizontalA.Intersection(verticalA)));
}
/// <summary>
/// Oracle: hand-computed crossings, T-junction, endpoint touch, diagonal overlap.
/// </summary>
[TestMethod]
public void LineIntersection_Oracle_HandCases_Test()
{
var pointComparer = new PointComparer();
Assert.IsTrue(pointComparer.Equals(new Point(5, 5),
LineIntersection.Find(
new Line(new Point(0, 0), new Point(10, 10)),
new Line(new Point(0, 10), new Point(10, 0)))));
Assert.IsTrue(pointComparer.Equals(new Point(5, 5),
LineIntersection.Find(
new Line(new Point(0, 5), new Point(10, 5)),
new Line(new Point(5, 5), new Point(5, 0)))));
Assert.IsNull(LineIntersection.Find(
new Line(new Point(0, 0), new Point(1, 1)),
new Line(new Point(0, 1), new Point(1, 2))));
Assert.IsTrue(pointComparer.Equals(new Point(2, 2),
LineIntersection.Find(
new Line(new Point(0, 0), new Point(4, 4)),
new Line(new Point(2, 2), new Point(6, 6)))));
Assert.IsNull(LineIntersection.Find(
new Line(new Point(0, 0), new Point(1, 1)),
new Line(new Point(3, 3), new Point(5, 5))));
}
}
}