forked from jamesra/Viking
/
LineTest.cs
238 lines (203 loc) · 9.87 KB
/
LineTest.cs
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Diagnostics;
using Geometry;
namespace GeometryTests
{
/// <summary>
/// Summary description for LineTest
/// </summary>
[TestClass]
public class LineTest
{
public LineTest()
{
//
// TODO: Add constructor logic here
//
}
private TestContext testContextInstance;
/// <summary>
///Gets or sets the test context which provides
///information about and functionality for the current test run.
///</summary>
public TestContext TestContext
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
#region Additional test attributes
//
// You can use the following additional attributes as you write your tests:
//
// Use ClassInitialize to run code before running the first test in the class
// [ClassInitialize()]
// public static void MyClassInitialize(TestContext testContext) { }
//
// Use ClassCleanup to run code after all tests in a class have run
// [ClassCleanup()]
// public static void MyClassCleanup() { }
//
// Use TestInitialize to run code before running each test
// [TestInitialize()]
// public void MyTestInitialize() { }
//
// Use TestCleanup to run code after each test has run
// [TestCleanup()]
// public void MyTestCleanup() { }
//
#endregion
[TestMethod]
public void GridLineSegmentDistanceToPoint()
{
//Check edge conditions for a horizontal line
{
GridLineSegment lineA = new GridLineSegment(new GridVector2(-5, 3),
new GridVector2(5, 3));
//Check edge conditions for a horizontal line
GridVector2 PointOnLine = new GridVector2(2, 3);
double Distance;
GridVector2 Intersection;
Distance = lineA.DistanceToPoint(PointOnLine, out Intersection);
Debug.Assert(Distance == 0);
Debug.Assert(Intersection == PointOnLine);
//Check if we go past the line in X axis
GridVector2 PointLeftOfLine = new GridVector2(-10, 3);
GridVector2 PointRightOfLine = new GridVector2(10, 3);
Distance = lineA.DistanceToPoint(PointLeftOfLine, out Intersection);
Debug.Assert(Distance == 5);
Debug.Assert(Intersection == lineA.A);
Distance = lineA.DistanceToPoint(PointRightOfLine, out Intersection);
Debug.Assert(Distance == 5);
Debug.Assert(Intersection == lineA.B);
//Check if we go above or below line
GridVector2 PointAboveLine = new GridVector2(3, 8);
GridVector2 PointBelowLine = new GridVector2(3, -2);
Distance = lineA.DistanceToPoint(PointAboveLine, out Intersection);
Debug.Assert(Distance == 5);
Debug.Assert(Intersection == new GridVector2(3, 3));
Distance = lineA.DistanceToPoint(PointBelowLine, out Intersection);
Debug.Assert(Distance == 5);
Debug.Assert(Intersection == new GridVector2(3, 3));
}
//Check edge conditions for a vertical line
{
GridLineSegment lineB = new GridLineSegment(new GridVector2(3, -5),
new GridVector2(3, 5));
GridVector2 PointOnLine = new GridVector2(3, 2);
double Distance;
GridVector2 Intersection;
Distance = lineB.DistanceToPoint(PointOnLine, out Intersection);
Debug.Assert(Distance == 0);
Debug.Assert(Intersection == PointOnLine);
//Check if we go above or below line
GridVector2 PointAboveLine = new GridVector2(3, 10);
GridVector2 PointBelowLine = new GridVector2(3, -10);
Distance = lineB.DistanceToPoint(PointAboveLine, out Intersection);
Debug.Assert(Distance == 5);
Debug.Assert(Intersection == lineB.B);
Distance = lineB.DistanceToPoint(PointBelowLine, out Intersection);
Debug.Assert(Distance == 5);
Debug.Assert(Intersection == lineB.A);
//Check if we go left or right of line
GridVector2 PointLeftOfLine = new GridVector2(-2, 4);
GridVector2 PointRightOfLine = new GridVector2(8, 4);
Distance = lineB.DistanceToPoint(PointLeftOfLine, out Intersection);
Debug.Assert(Distance == 5);
Debug.Assert(Intersection == new GridVector2(3, 4));
Distance = lineB.DistanceToPoint(PointRightOfLine, out Intersection);
Debug.Assert(Distance == 5);
Debug.Assert(Intersection == new GridVector2(3, 4));
}
{ //Check the diagonal line through the axis center
GridLineSegment lineC = new GridLineSegment(new GridVector2(-5, -5),
new GridVector2(5, 5));
GridVector2 PointOnLine = new GridVector2(0, 0);
double Distance;
GridVector2 Intersection;
Distance = lineC.DistanceToPoint(PointOnLine, out Intersection);
Debug.Assert(Distance == 0);
Debug.Assert(Intersection == PointOnLine);
GridVector2 PointOffLine = new GridVector2(-5, 5);
Distance = lineC.DistanceToPoint(PointOffLine, out Intersection);
Debug.Assert(Distance == Math.Sqrt(Math.Pow(5, 2) + Math.Pow(5,2)));
Debug.Assert(Intersection == new GridVector2(0,0));
GridVector2 PointPastEdge = new GridVector2(-10, 0);
Distance = lineC.DistanceToPoint(PointPastEdge, out Intersection);
Debug.Assert(Distance == Math.Sqrt(Math.Pow(5, 2) + Math.Pow(5,2)));
Debug.Assert(Intersection == new GridVector2(-5,-5));
}
{ //Check the diagonal line through the axis center
GridLineSegment lineD = new GridLineSegment(new GridVector2(-6, -4),
new GridVector2(4, 6));
GridVector2 PointOnLine = new GridVector2(-1, 1);
double Distance;
GridVector2 Intersection;
Distance = lineD.DistanceToPoint(PointOnLine, out Intersection);
Debug.Assert(Distance == 0);
Debug.Assert(Intersection == PointOnLine);
GridVector2 PointOffLine = new GridVector2(-6, 6);
Distance = lineD.DistanceToPoint(PointOffLine, out Intersection);
Debug.Assert(Distance == Math.Sqrt(Math.Pow(5, 2) + Math.Pow(5, 2)));
Debug.Assert(Intersection == new GridVector2(-1, 1));
GridVector2 PointPastEdge = new GridVector2(9, 1);
Distance = lineD.DistanceToPoint(PointPastEdge, out Intersection);
Debug.Assert(Distance == Math.Sqrt(Math.Pow(5, 2) + Math.Pow(5, 2)));
Debug.Assert(Intersection == new GridVector2(4, 6));
}
}
[TestMethod]
public void GridLineSegmentIntersects()
{
//
// TODO: Add test logic here
//
GridLineSegment lineA = new GridLineSegment(new GridVector2(-5,3),
new GridVector2(5,3));
GridLineSegment lineB = new GridLineSegment(new GridVector2(3,-5),
new GridVector2(3,5));
GridLineSegment lineC = new GridLineSegment(new GridVector2(-6, -5),
new GridVector2(-6, 5));
GridLineSegment lineD = new GridLineSegment(new GridVector2(-9, 8),
new GridVector2(1, -8));
GridLineSegment lineE = new GridLineSegment(new GridVector2(-9, 8),
new GridVector2(1, -2));
GridVector2 intersect = new GridVector2();
bool result = lineA.Intersects(lineB, out intersect);
Debug.Assert(result == true);
Debug.Assert(intersect.X == 3 && intersect.Y == 3);
result = lineA.Intersects(lineC, out intersect);
Debug.Assert(result == false);
result = lineA.Intersects(lineD, out intersect);
Debug.Assert(result == false);
// Debug.Assert(intersect.X == -4 && intersect.Y == 3);
result = lineA.Intersects(lineE, out intersect);
Debug.Assert(result == true);
Debug.Assert(intersect.X == -4 && intersect.Y == 3);
}
[TestMethod]
public void GridLineIntersects()
{
//
// TODO: Add test logic here
//
GridLine lineA = new GridLine(new GridVector2(-5, 0),
new GridVector2(-10, 0));
GridLine lineB = new GridLine(new GridVector2(0, 5),
new GridVector2(0, -5));
GridVector2 intersect = new GridVector2();
bool result = lineA.Intersects(lineB, out intersect);
Debug.Assert(result == true);
Debug.Assert(intersect.X == 0 && intersect.Y == 0);
}
}
}