-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestLineUtils.cpp
143 lines (94 loc) · 3.86 KB
/
TestLineUtils.cpp
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
#include "../../headers/catch.hpp"
#include "../../headers/Utils/LineUtils.h"
TEST_CASE("Test determinant", "[LineUtils]") {
SECTION("Determinant is not zero") {
Inters::LineUtils lineUtils;
REQUIRE(lineUtils.determinant(5, 2, 4, 3) == 7);
}
SECTION("Determinant is zero") {
Inters::LineUtils lineUtils;
REQUIRE(lineUtils.determinant(5, 2, 0, 0) == 0);
REQUIRE(lineUtils.determinant(15, 10, 3, 2) == 0);
}
SECTION("Determinant is negative") {
Inters::LineUtils lineUtils;
REQUIRE(lineUtils.determinant(5, 6, 1, 1) == -1);
REQUIRE(lineUtils.determinant(-15, 10, 3, 2) == -60);
}
}
TEST_CASE("Test find intersection", "[LineUtils]") {
Inters::Point *p1, *p2, *p3, *p4, *expectedIntersectionPoint;
Inters::Line *l1, *l2;
Inters::Intersection *intersection;
Inters::LineUtils lineUtils;
SECTION("l1 and l2 have an intersection in the middle") {
p1 = new Inters::Point(0, 5);
p2 = new Inters::Point(10, 5);
p3 = new Inters::Point(5, 0);
p4 = new Inters::Point(5, 10);
l1 = new Inters::Line(p1, p2);
l2 = new Inters::Line(p3, p4);
expectedIntersectionPoint = new Inters::Point(5, 5);
intersection = lineUtils.findIntersection(l1, l2);
REQUIRE(*intersection->getIntersectionPoint() == *expectedIntersectionPoint);
REQUIRE(*intersection->getLines().first == *l1);
REQUIRE(*intersection->getLines().second == *l2);
REQUIRE_FALSE(intersection->isIsColinear());
}
SECTION("l1 and l2 do not have intersections") {
p1 = new Inters::Point(0, 5);
p2 = new Inters::Point(10, 5);
p3 = new Inters::Point(15, 0);
p4 = new Inters::Point(15, 10);
l1 = new Inters::Line(p1, p2);
l2 = new Inters::Line(p3, p4);
intersection = lineUtils.findIntersection(l1, l2);
REQUIRE(intersection == NULL);
}
SECTION("l1 and l2 are colinear") {
p1 = new Inters::Point(0, 5);
p2 = new Inters::Point(10, 5);
p3 = new Inters::Point(4, 5);
p4 = new Inters::Point(15, 5);
l1 = new Inters::Line(p1, p2);
l2 = new Inters::Line(p3, p4);
expectedIntersectionPoint = NULL;
intersection = lineUtils.findIntersection(l1, l2);
REQUIRE(intersection->getIntersectionPoint() == expectedIntersectionPoint);
REQUIRE(*intersection->getLines().first == *l1);
REQUIRE(*intersection->getLines().second == *l2);
REQUIRE(intersection->isIsColinear());
}
}
TEST_CASE("Test vertical intersection", "[LineUtils]") {
Inters::Point *p1, *p2, *expectedIntersectionPoint, *intersectionPoint;
double sweepLine;
Inters::Line *l1;
Inters::LineUtils lineUtils;
SECTION("l1 has an intersection in the middle") {
p1 = new Inters::Point(0, 5);
p2 = new Inters::Point(10, 5);
sweepLine = 5;
l1 = new Inters::Line(p1, p2);
expectedIntersectionPoint = new Inters::Point(5, 5);
intersectionPoint = lineUtils.findVerticalIntersection(l1, sweepLine);
REQUIRE(*intersectionPoint == *expectedIntersectionPoint);
}
SECTION("l1 and the sweepline do not intersect") {
p1 = new Inters::Point(0, 5);
p2 = new Inters::Point(10, 5);
sweepLine = 25;
l1 = new Inters::Line(p1, p2);
intersectionPoint = lineUtils.findVerticalIntersection(l1, sweepLine);
REQUIRE(intersectionPoint == NULL);
}
SECTION("l1 is vertical") {
p1 = new Inters::Point(5, 0);
p2 = new Inters::Point(5, 10);
sweepLine = 5;
l1 = new Inters::Line(p1, p2);
expectedIntersectionPoint = new Inters::Point(5, 0);
intersectionPoint = lineUtils.findVerticalIntersection(l1, sweepLine);
REQUIRE(*intersectionPoint == *expectedIntersectionPoint);
}
}