-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
356 lines (301 loc) · 8.66 KB
/
test.cpp
File metadata and controls
356 lines (301 loc) · 8.66 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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#define CATCH_CONFIG_MAIN
#include <iostream>
#include "catch.hpp.h"
#include "shape.h"
typedef std::vector<Point> points;
void alert_point(Point p, Point expected)
{
CHECK(p.x == Approx(expected.x));
REQUIRE(p.y == Approx(expected.y));
}
void alert_intersection(BaseFigure& b_right, BaseFigure& b_left, std::vector<Point> expected)
{
std::vector<Point> result = b_right.intersection(b_left);
REQUIRE(result.size() == expected.size());
for (int i = 0; i < result.size(); i++) {
alert_point(result[i], expected[i]);
};
}
void alert_intersection(Point p1_begin, Point p1_end, Point p2_begin, Point p2_end, points expected)
{
Segment s1(p1_begin, p1_end), s2(p2_begin, p2_end);
alert_intersection(s1, s2, expected);
};
void alert_intersection(double r1, Point c1, double r2, Point c2, points expected)
{
Circle c(r1, c1), c_(r2, c2);
alert_intersection(c, c_, expected);
};
TEST_CASE("d") {
std::vector<std::shared_ptr<BaseFigure>> p;
p.emplace_back(new Circle(34, Point(12, 12)));
p.emplace_back(new Segment(Point(12, 12), Point(23, 23)));
p.emplace_back(new Circle(1134, Point(12, 12)));
p.emplace_back(new Segment(Point(122, 122), Point(-122, 22)));
for (int i = 0; i < p.size(); i++) {
for (int j = i + 1; j < p.size(); ++j) {
(*p[i]).intersection(*p[j]);
}
}
}
TEST_CASE("create object and initializing variables", "[create], [initializing]")
{
SECTION("Line")
{
Point p1(1.3455, 6755.000);
Point p2(-344.5, 0.99);
Segment s1(p1, p2);
alert_point(s1.get_point_begin(), p2);
alert_point(s1.get_point_end(), p1);
REQUIRE(s1.get_A() == Approx(0.998691548));
REQUIRE(s1.get_B() == Approx(-0.05113895));
REQUIRE(s1.get_C() == Approx(344.099865808));
};
SECTION("Circle")
{
Point p3(3473.34334, -994.33334);
double r = 358745.49;
Circle c1(r, p3);
alert_point(c1.get_center(), p3);
REQUIRE(c1.get_radius() == Approx(r));
};
SECTION("Broken Line")
{
std::vector<Point> nodes;
for (int i = 0; i < 20; i++) {
nodes.push_back(Point(std::rand(), std::rand()));
};
PolyLine br(nodes);
for (int i = 0; i < nodes.size(); i++) {
alert_point(br.get_node(i), nodes[i]);
};
REQUIRE(br.get_num_nodes() == nodes.size());
}
};
void alert_intersection(Point p_begin, Point p_end, double r, Point center, points expected){
Segment segment(p_begin, p_end);
Circle circle(r, center);
alert_intersection(segment, circle, expected);
}
TEST_CASE("intersect segment-segment", "[intersection], [segment]")
{
points expected;
SECTION("intersect point not contain in segments")
{
alert_intersection(
Point(21.0, 226),
Point(43, 112),
Point(58, 115),
Point(98, 24),
expected
);
};
SECTION("lines parallel")
{
Point p1(78.9883, 1.8775);
Point p2(-54.31, -343.99);
alert_intersection(
p1, p2,
(-758.26) * p1,
(-785.26) * p2,
expected
);
p1.set(833.9883, 109.8775);
p2.set(444.31, 323.99);
alert_intersection(
p1, p2,
Point(-100, -100) + p1,
Point(-100, -100) + p2,
expected
);
};
SECTION("line equivalents")
{
alert_intersection(
Point(89.33333, 89.3333),
Point(903.444444, 903.444444),
Point(-5666.666, -5666.666),
Point(-7.333393, -7.333393),
expected
);
};
SECTION("simple intersect")
{
expected = {Point(7.74343, 8.04034)};
alert_intersection(
Point(3.874203 + 3 * (8.4534534 - 3.874203), 38.38574 + 3 * (9.84752 - 38.38574)),
Point(3.874203, 38.38574),
Point(45.45734, 45.756345),
Point(-45.344, -47.5234),
expected
);
expected = {Point(4.16395, 0.46455)};
alert_intersection(
Point(0.3 * 12.284, 0.3 * 1.5485),
Point(0.3 * 15.5485, 0.3 * 1.5485),
Point(4.16395, 0.46455),
Point(0.3 * 13.879848, 0.3 * 20.5154),
expected
);
};
SECTION("segments on one line")
{
expected = {Point(-0.55, 6), Point(23.024556, 6)};
alert_intersection(
Point(-12.5555, 6),
Point(23.024556, 6),
Point(43.5464, 6),
Point(-0.55, 6),
expected
);
expected = {Point(-5, -5.354643), Point(-5, 65.34345)};
alert_intersection(
Point(-5, 89.345342),
Point(-5, -90.435234),
Point(-5, 65.34345),
Point(-5, -5.354643),
expected
);
}
};
TEST_CASE("intersect circle circle")
{
points expected;
SECTION("simple intersect")
{
expected = {Point(88.7591, 102.447), Point(94.0211, 91.478)};
alert_intersection(
10,
Point(98.54646, 100.39545),
7,
Point(88.26684, 95.464),
expected
);
expected = {Point(-421.554, -27.242), Point(-418.292, 34.1061)};
alert_intersection(
44.554,
Point(-452.15, 5.1455),
32.54,
Point(-430.6456, 4.00212),
expected
);
};
SECTION("no intersect")
{
expected = {};
alert_intersection(
4.554,
Point(-452.15, 5.1455),
25.54,
Point(-9.6456, 9.00212),
expected
);
};
SECTION("nested circle")
{
expected = {};
alert_intersection(
44.554,
Point(-452.15, 5.1455),
1.54,
Point(-430.6456, 4.00212),
expected
);
};
SECTION("external contact")
{
expected = {Point(-27.0/50.0, 21111.0/2000)};
alert_intersection(
5.54,
Point(5, 10.5555),
10.56,
Point(-11.1, 10.5555),
expected
);
};
SECTION("internal contact")
{
expected = {Point(-50.54, 10.55555)};
alert_intersection(
55.54,
Point(5, 10.5555),
45.55555,
Point(-4.98445, 10.5555),
expected
);
};
}
TEST_CASE("intersect circle segment")
{
points expected;
SECTION("simple intersect")
{
expected = {Point(-6.85125, 12.3884), Point(19.8913, -7.66844)};
alert_intersection(
Point(23.0, -10),
Point(-25.0, 26),
23.0,
Point(16.0, 15.0),
expected
);
};
SECTION("intersect with one point and segment inside circle")
{
expected = {Point(-30.8029, 38.7436)};
alert_intersection(
Point(23.0054, -4.51211),
Point(-89.5879, 86),
45.55555,
Point(4.98445, 10.5555),
expected
);
};
SECTION("no intersect")
{
expected = {};
alert_intersection(
Point(15, -10),
Point(-25, 26),
12,
Point(16, 15),
expected
);
};
SECTION("contact")
{
expected = {Point(4.0, 2.0)};
alert_intersection(
Point(4.0, 5.0),
Point(4.0, 1.0),
2.0,
Point(2.0, 2.0),
expected
);
};
};
TEST_CASE("intersect segment with broken line")
{
points nodes = {Point(0.0, 0.0), Point(4.0, 4.0), Point(2.0, 0.0), Point(2.0, 4.0)};
PolyLine broken_line(nodes);
Point a(0.0, 3.0), b(4.0, 3.0);
Segment seg(a, b);
points expected = {Point(3.0, 3.0), Point(3.5, 3.0), Point(2.0, 3.0)};
alert_intersection(broken_line, seg, expected);
};
TEST_CASE("intersect broken line with broken line")
{
points nodes1 = {Point(6.0, 4.0), Point(0.0, 1.0), Point(5.0, 1.0)};
points nodes2 = {Point(1.0, 3.0), Point(4.0, 0.0), Point(4.0, 4.0)};
PolyLine bline1(nodes1), bline2(nodes2);
points expected = {Point(2.0, 2.0), Point(4.0, 3.0), Point(3.0, 1.0), Point(4.0, 1.0)};
alert_intersection(bline1, bline2, expected);
}
TEST_CASE("intersect broken line with circle")
{
points nodes = {Point(2.0, 5.0), Point(2.0, -1.0), Point(4.0, -1.0), Point(4.0, 5.0)};
PolyLine broken_line(nodes);
Point a(2.0, 2.0);
Circle circle(2.0, a);
points expected = {Point(2.0, 0.0), Point(2.0, 4.0), Point(4.0, 2.0)};
alert_intersection(broken_line, circle, expected);
};