@@ -12,13 +12,13 @@ typedef vector<int> vi;
12
12
13
13
#include " ../../content/geometry/PolygonArea.h"
14
14
typedef Point <double > P;
15
- ostream & operator <<(ostream &os, P p) { return cout << " ( " << p. x << " , " << p. y << " ) " ; }
15
+
16
16
#include " ../../content/geometry/HalfPlane.h"
17
17
18
18
namespace sjtu {
19
19
typedef double T;
20
- const T EPS = 1e-8 ;
21
- inline int sign (T a) { return a < -EPS ? -1 : a > EPS ; }
20
+ const T sEPS = 1e-8 ;
21
+ inline int sign (T a) { return a < -sEPS ? -1 : a > sEPS ; }
22
22
struct Point {
23
23
T x, y;
24
24
Point () {}
@@ -255,7 +255,6 @@ vector<mit::Line> convert(vector<Line> x) {
255
255
}
256
256
257
257
const double INF = 100 ;
258
- const double EPS = 1e-8 ;
259
258
void addInf (vector<Line> &res, double INF = INF) {
260
259
vector<P> infPts ({P (INF, INF), P (-INF, INF), P (-INF, -INF), P (INF, -INF)});
261
260
for (int i = 0 ; i < 4 ; i++)
@@ -301,7 +300,7 @@ void testEmpty() {
301
300
auto res = halfPlaneIntersection (t);
302
301
assert (sz (res) == 0 );
303
302
}
304
- void testRandom (const double EPS ) {
303
+ void testRandom () {
305
304
int lim = 3 ;
306
305
double mxDiff = 0 ;
307
306
for (int i = 0 ; i < 10000000 ; i++) {
@@ -328,13 +327,12 @@ void testRandom(const double EPS) {
328
327
t.push_back (cand);
329
328
}
330
329
addInf (t, lim);
331
- auto res = halfPlaneIntersection (t, EPS );
330
+ auto res = halfPlaneIntersection (t);
332
331
double area = sjtu::halfPlaneIntersection (t);
333
332
double resArea = abs (polygonArea2 (res) / 2 );
334
333
// double resArea = mit::Intersection_Area(convert(t));
335
334
double diff = abs (area - resArea);
336
335
mxDiff = max (diff, mxDiff);
337
- continue ;
338
336
if (diff > .1 || isnan (diff)) {
339
337
cout << diff << ' ' << area << ' ' << resArea << endl;
340
338
for (auto i : t)
@@ -350,8 +348,6 @@ void testRandom(const double EPS) {
350
348
assert (false );
351
349
}
352
350
}
353
- cout<<" eps: " <<EPS<<endl;
354
- cout<<" mxDiff: " <<mxDiff<<endl;
355
351
}
356
352
vector<P> convexHull (vector<P> pts) {
357
353
if (sz (pts) <= 1 ) return pts;
@@ -368,107 +364,11 @@ vector<P> convexHull(vector<P> pts) {
368
364
369
365
370
366
int main () {
371
- // test1();
372
- // testInf();
373
- // testLine();
374
- // testPoint();
375
- for (double e : {1e-10 , 1e-9 , 1e-8 , 1e-7 , 1e-6 , 1e-5 , 1e-4 , 1e-3 , 1e-2 })
376
- testRandom (e);
377
- return 0 ;
378
- // testEmpty();
379
- // Case that messes with precision
380
- // vector<Line> cases({{P(1,0),P(0,2)},{P(0,1),P(2,0)},{P(0,0),P(1,1)},{P(2,2),P(1,1)},{P(3,3),P(-3,3)},{P(-3,3),P(-3,-3)},{P(-3,-3),P(3,-3)},{P(3,-3),P(3,3)}});
381
-
382
- // auto pts = halfPlaneIntersection(cases);
383
- // for (auto p: pts) {
384
- // cout<<p<<' ';
385
- // }
386
- // cout<<endl;
387
- // cout<<polygonArea2(pts)<<endl;
388
- // return 0;
389
-
390
- array<int , 3 > mn = {1e6 , 1e6 , 10 };
391
- for (int i = 0 ; i < 10000000 ; i++) {
392
- ll offset = rand () % ll (1e9 );
393
- ll mul = rand () % ll (1e9 );
394
- vector<pair<int , vector<Line>>> cases;
367
+ test1 ();
368
+ testInf ();
369
+ testLine ();
370
+ testPoint ();
371
+ testRandom ();
395
372
396
- cases.push_back ({10 ,
397
- {{P (offset + mul * 8 , offset + mul * 9 ), P (offset + mul * 8 , offset + mul * 2 )},
398
- {P (offset + mul * 3 , offset + mul * 9 ), P (offset + mul * 5 , offset + mul * 2 )},
399
- {P (offset + mul * 8 , offset + mul * 2 ), P (offset + mul * 8 , offset + mul * 3 )},
400
- {P (offset + mul * 7 , offset + mul * 2 ), P (offset + mul * 1 , offset + mul * 7 )},
401
- {P (offset + mul * 1 , offset + mul * 0 ), P (offset + mul * 7 , offset + mul * 1 )},
402
- {P (offset + mul * 9 , offset + mul * 2 ), P (offset + mul * 5 , offset + mul * 6 )},
403
- {P (offset + mul * 10 , offset + mul * 10 ), P (offset + mul * -10 , offset + mul * 10 )},
404
- {P (offset + mul * -10 , offset + mul * 10 ), P (offset + mul * -10 , offset + mul * -10 )},
405
- {P (offset + mul * -10 , offset + mul * -10 ), P (offset + mul * 10 , offset + mul * -10 )},
406
- {P (offset + mul * 10 , offset + mul * -10 ), P (offset + mul * 10 , offset + mul * 10 )}}});
407
- cases.push_back ({5 ,
408
- {{P (offset + mul * 0 , offset + mul * 1 ), P (offset + mul * 4 , offset + mul * 0 )},
409
- {P (offset + mul * 0 , offset + mul * 2 ), P (offset + mul * 2 , offset + mul * 0 )},
410
- {P (offset + mul * 1 , offset + mul * 0 ), P (offset + mul * 3 , offset + mul * 4 )},
411
- {P (offset + mul * 4 , offset + mul * 2 ), P (offset + mul * 0 , offset + mul * 0 )},
412
- {P (offset + mul * 4 , offset + mul * 2 ), P (offset + mul * 2 , offset + mul * 2 )},
413
- {P (offset + mul * 0 , offset + mul * 3 ), P (offset + mul * 1 , offset + mul * 1 )},
414
- {P (offset + mul * 5 , offset + mul * 5 ), P (offset + mul * -5 , offset + mul * 5 )},
415
- {P (offset + mul * -5 , offset + mul * 5 ), P (offset + mul * -5 , offset + mul * -5 )},
416
- {P (offset + mul * -5 , offset + mul * -5 ), P (offset + mul * 5 , offset + mul * -5 )},
417
- {P (offset + mul * 5 , offset + mul * -5 ), P (offset + mul * 5 , offset + mul * 5 )}}});
418
- cases.push_back ({20 ,
419
- {{P (offset + mul * 10 , offset + mul * 12 ), P (offset + mul * 16 , offset + mul * 5 )},
420
- {P (offset + mul * 10 , offset + mul * 12 ), P (offset + mul * 4 , offset + mul * 19 )},
421
- {P (offset + mul * 18 , offset + mul * 15 ), P (offset + mul * 17 , offset + mul * 7 )},
422
- {P (offset + mul * 12 , offset + mul * 13 ), P (offset + mul * 14 , offset + mul * 6 )},
423
- {P (offset + mul * 16 , offset + mul * 3 ), P (offset + mul * 4 , offset + mul * 11 )},
424
- {P (offset + mul * 12 , offset + mul * 8 ), P (offset + mul * 9 , offset + mul * 0 )},
425
- {P (offset + mul * 20 , offset + mul * 20 ), P (offset + mul * -20 , offset + mul * 20 )},
426
- {P (offset + mul * -20 , offset + mul * 20 ), P (offset + mul * -20 , offset + mul * -20 )},
427
- {P (offset + mul * -20 , offset + mul * -20 ), P (offset + mul * 20 , offset + mul * -20 )},
428
- {P (offset + mul * 20 , offset + mul * -20 ), P (offset + mul * 20 , offset + mul * 20 )}}});
429
- // {P(5,8),P(4,9)},{P(9,1),P(5,8)},{P(9,7),P(7,6)},{P(3,7),P(8,5)},{P(6,6),P(8,4)},{P(6,1),P(7,2)},{P(10,10),P(-10,10)},{P(-10,10),P(-10,-10)},{P(-10,-10),P(10,-10)},{P(10,-10),P(10,10)},
430
- int idx = 0 ;
431
- for (auto tmp : cases) {
432
- auto t = tmp.second ;
433
- auto lim = tmp.first ;
434
- auto res = halfPlaneIntersection (t);
435
- auto ours = polygonArea2 (res);
436
- if (abs (ours) > 1e-3 ) {
437
- if (mn[0 ] + mn[1 ] * mn[2 ] >= offset + mul * tmp.first ) {
438
- cout << " case: " << idx << endl;
439
- cout << offset << ' ' << mul << ' ' << offset + mul * tmp.first << endl;
440
- cout << ours << ' ' << sjtu::halfPlaneIntersection (t) << endl;
441
-
442
- double mx = 0 ;
443
- for (auto i: res) {
444
- for (auto j: res)
445
- mx = max (mx, (i - j).dist ());
446
- }
447
- cout<<" mx: " <<mx<<endl;
448
- for (auto i: res) {
449
- cout<<i<<' ' ;
450
- }
451
- cout<<endl;
452
- cout << endl;
453
- mn = {offset, mul, tmp.first };
454
- }
455
- // cout << ours << endl;
456
- // cout << sjtu::halfPlaneIntersection(t) << endl;
457
- // auto mits = mit::Intersection_Area(convert(t));
458
- // cout << mits<<endl;
459
- // cout << endl;
460
- }
461
- idx++;
462
- }
463
- }
464
- cout << " mn: " << mn[0 ] << ' ' << mn[1 ] << endl;
465
- cout << mn[0 ] + mn[1 ] * mn[2 ] << endl;
466
- // Failure case for mit's half plane cod
467
- // vector<Line> t({{P(9, 8), P(9, 1)}, {P(3, 3), P(3, 5)}, {P(7, 6), P(0, 8)}});
468
- // Failure case for old code.
469
- // addInf(t);
470
- // cout << polygonArea2(halfPlaneIntersection(t)) << endl;
471
- // addInf(t);
472
- // cout << polygonArea2(halfPlaneIntersection(t)) << endl;
473
373
cout << " Tests passed!" << endl;
474
374
}
0 commit comments