@@ -302,22 +302,37 @@ void testEmpty() {
302
302
assert (sz (res) == 0 );
303
303
}
304
304
void testRandom () {
305
- int lim = 3 ;
306
- for (int i = 0 ; i < 1000000 ; i++) {
305
+ int lim = 5 ;
306
+ for (int i = 0 ; i < 10000000 ; i++) {
307
307
vector<Line> t;
308
- for (int i = 0 ; i < 6 ; i++) {
308
+ for (int i = 0 ; i < 4 ; i++) {
309
309
Line cand{P (0 , 0 ), P (0 , 0 )};
310
310
while (cand[0 ] == cand[1 ])
311
311
cand = {randPt (lim), randPt (lim)};
312
+ Point<int > s1 = Point<int >(cand[0 ].x , cand[0 ].y );
313
+ Point<int > e1 = Point<int >(cand[1 ].x , cand[1 ].y );
314
+ bool fail = false ;
315
+ for (auto j: t) {
316
+ Point<int > s2 = Point<int >(j[0 ].x , j[0 ].y );
317
+ Point<int > e2 = Point<int >(j[1 ].x , j[1 ].y );
318
+ if (lineInter (s1, e1 , s2, e2 ).first == -1 ) {
319
+ fail = false ;
320
+ break ;
321
+ }
322
+ }
323
+ if (fail) {
324
+ i--;
325
+ continue ;
326
+ }
312
327
t.push_back (cand);
313
328
}
314
329
addInf (t, lim);
315
330
auto res = halfPlaneIntersection (t);
316
331
double area = sjtu::halfPlaneIntersection (t);
317
- // double resArea = polygonArea2(res)/2 ;
318
- double resArea = mit::Intersection_Area (convert (t));
332
+ double resArea = abs ( polygonArea2 (res) / 2 ) ;
333
+ // double resArea = mit::Intersection_Area(convert(t));
319
334
double diff = abs (area - resArea);
320
- if (diff > EPS) {
335
+ if (diff > EPS || isnan (diff) ) {
321
336
cout << diff << ' ' << area << ' ' << resArea << endl;
322
337
for (auto i : t)
323
338
cout << i[0 ] << " ->" << i[1 ] << ' ' ;
@@ -330,36 +345,121 @@ void testRandom() {
330
345
cout << i << ' ,' ;
331
346
cout << endl;
332
347
}
333
- assert (diff < EPS);
348
+ assert (diff <= EPS);
334
349
}
335
350
}
351
+ vector<P> convexHull (vector<P> pts) {
352
+ if (sz (pts) <= 1 ) return pts;
353
+ sort (all (pts));
354
+ vector<P> h (sz (pts)+1 );
355
+ int s = 0 , t = 0 ;
356
+ for (int it = 2 ; it--; s = --t, reverse (all (pts)))
357
+ trav (p, pts) {
358
+ while (t >= s + 2 && h[t-2 ].cross (h[t-1 ], p) <= 0 ) t--;
359
+ h[t++] = p;
360
+ }
361
+ return {h.begin (), h.begin () + t - (t == 2 && h[0 ] == h[1 ])};
362
+ }
363
+
336
364
337
365
int main () {
338
- test1 ();
339
- testInf ();
340
- testLine ();
341
- testPoint ();
342
- testEmpty ();
343
- testRandom ();
366
+ // test1();
367
+ // testInf();
368
+ // testLine();
369
+ // testPoint();
370
+ // testRandom();
371
+ // return 0;
372
+ // testEmpty();
344
373
// Case that messes with precision
345
- vector<Line> t ({{P (8 , 9 ), P (8 , 2 )},
346
- {P (3 , 9 ), P (5 , 2 )},
347
- {P (8 , 2 ), P (8 , 3 )},
348
- {P (7 , 2 ), P (1 , 7 )},
349
- {P (1 , 0 ), P (7 , 1 )},
350
- {P (9 , 2 ), P (5 , 6 )},
351
- {P (10 , 10 ), P (-10 , 10 )},
352
- {P (-10 , 10 ), P (-10 , -10 )},
353
- {P (-10 , -10 ), P (10 , -10 )},
354
- {P (10 , -10 ), P (10 , 10 )}});
355
- auto res = halfPlaneIntersection (t);
356
- cout << polygonArea2 (res) << endl;
357
- cout << sjtu::halfPlaneIntersection (t) << endl;
358
- cout << mit::Intersection_Area (convert (t))<<endl;
374
+ // 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)}});
375
+
376
+ // auto pts = halfPlaneIntersection(cases);
377
+ // for (auto p: pts) {
378
+ // cout<<p<<' ';
379
+ // }
380
+ // cout<<endl;
381
+ // cout<<polygonArea2(pts)<<endl;
382
+ // return 0;
383
+
384
+ array<int , 3 > mn = {1e6 , 1e6 , 10 };
385
+ for (int i = 0 ; i < 10000000 ; i++) {
386
+ ll offset = rand () % ll (1e9 );
387
+ ll mul = rand () % ll (1e9 );
388
+ vector<pair<int , vector<Line>>> cases;
389
+
390
+ cases.push_back ({10 ,
391
+ {{P (offset + mul * 8 , offset + mul * 9 ), P (offset + mul * 8 , offset + mul * 2 )},
392
+ {P (offset + mul * 3 , offset + mul * 9 ), P (offset + mul * 5 , offset + mul * 2 )},
393
+ {P (offset + mul * 8 , offset + mul * 2 ), P (offset + mul * 8 , offset + mul * 3 )},
394
+ {P (offset + mul * 7 , offset + mul * 2 ), P (offset + mul * 1 , offset + mul * 7 )},
395
+ {P (offset + mul * 1 , offset + mul * 0 ), P (offset + mul * 7 , offset + mul * 1 )},
396
+ {P (offset + mul * 9 , offset + mul * 2 ), P (offset + mul * 5 , offset + mul * 6 )},
397
+ {P (offset + mul * 10 , offset + mul * 10 ), P (offset + mul * -10 , offset + mul * 10 )},
398
+ {P (offset + mul * -10 , offset + mul * 10 ), P (offset + mul * -10 , offset + mul * -10 )},
399
+ {P (offset + mul * -10 , offset + mul * -10 ), P (offset + mul * 10 , offset + mul * -10 )},
400
+ {P (offset + mul * 10 , offset + mul * -10 ), P (offset + mul * 10 , offset + mul * 10 )}}});
401
+ cases.push_back ({5 ,
402
+ {{P (offset + mul * 0 , offset + mul * 1 ), P (offset + mul * 4 , offset + mul * 0 )},
403
+ {P (offset + mul * 0 , offset + mul * 2 ), P (offset + mul * 2 , offset + mul * 0 )},
404
+ {P (offset + mul * 1 , offset + mul * 0 ), P (offset + mul * 3 , offset + mul * 4 )},
405
+ {P (offset + mul * 4 , offset + mul * 2 ), P (offset + mul * 0 , offset + mul * 0 )},
406
+ {P (offset + mul * 4 , offset + mul * 2 ), P (offset + mul * 2 , offset + mul * 2 )},
407
+ {P (offset + mul * 0 , offset + mul * 3 ), P (offset + mul * 1 , offset + mul * 1 )},
408
+ {P (offset + mul * 5 , offset + mul * 5 ), P (offset + mul * -5 , offset + mul * 5 )},
409
+ {P (offset + mul * -5 , offset + mul * 5 ), P (offset + mul * -5 , offset + mul * -5 )},
410
+ {P (offset + mul * -5 , offset + mul * -5 ), P (offset + mul * 5 , offset + mul * -5 )},
411
+ {P (offset + mul * 5 , offset + mul * -5 ), P (offset + mul * 5 , offset + mul * 5 )}}});
412
+ cases.push_back ({20 ,
413
+ {{P (offset + mul * 10 , offset + mul * 12 ), P (offset + mul * 16 , offset + mul * 5 )},
414
+ {P (offset + mul * 10 , offset + mul * 12 ), P (offset + mul * 4 , offset + mul * 19 )},
415
+ {P (offset + mul * 18 , offset + mul * 15 ), P (offset + mul * 17 , offset + mul * 7 )},
416
+ {P (offset + mul * 12 , offset + mul * 13 ), P (offset + mul * 14 , offset + mul * 6 )},
417
+ {P (offset + mul * 16 , offset + mul * 3 ), P (offset + mul * 4 , offset + mul * 11 )},
418
+ {P (offset + mul * 12 , offset + mul * 8 ), P (offset + mul * 9 , offset + mul * 0 )},
419
+ {P (offset + mul * 20 , offset + mul * 20 ), P (offset + mul * -20 , offset + mul * 20 )},
420
+ {P (offset + mul * -20 , offset + mul * 20 ), P (offset + mul * -20 , offset + mul * -20 )},
421
+ {P (offset + mul * -20 , offset + mul * -20 ), P (offset + mul * 20 , offset + mul * -20 )},
422
+ {P (offset + mul * 20 , offset + mul * -20 ), P (offset + mul * 20 , offset + mul * 20 )}}});
423
+ // {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)},
424
+ int idx = 0 ;
425
+ for (auto tmp : cases) {
426
+ auto t = tmp.second ;
427
+ auto lim = tmp.first ;
428
+ auto res = halfPlaneIntersection (t);
429
+ auto ours = polygonArea2 (res);
430
+ if (abs (ours) > 1e-3 ) {
431
+ if (mn[0 ] + mn[1 ] * mn[2 ] >= offset + mul * tmp.first ) {
432
+ cout << " case: " << idx << endl;
433
+ cout << offset << ' ' << mul << ' ' << offset + mul * tmp.first << endl;
434
+ cout << ours << ' ' << sjtu::halfPlaneIntersection (t) << endl;
435
+
436
+ double mx = 0 ;
437
+ for (auto i: res) {
438
+ for (auto j: res)
439
+ mx = max (mx, (i - j).dist ());
440
+ }
441
+ cout<<" mx: " <<mx<<endl;
442
+ for (auto i: res) {
443
+ cout<<i<<' ' ;
444
+ }
445
+ cout<<endl;
446
+ cout << endl;
447
+ mn = {offset, mul, tmp.first };
448
+ }
449
+ // cout << ours << endl;
450
+ // cout << sjtu::halfPlaneIntersection(t) << endl;
451
+ // auto mits = mit::Intersection_Area(convert(t));
452
+ // cout << mits<<endl;
453
+ // cout << endl;
454
+ }
455
+ idx++;
456
+ }
457
+ }
458
+ cout << " mn: " << mn[0 ] << ' ' << mn[1 ] << endl;
459
+ cout << mn[0 ] + mn[1 ] * mn[2 ] << endl;
359
460
// Failure case for mit's half plane cod
360
461
// vector<Line> t({{P(9, 8), P(9, 1)}, {P(3, 3), P(3, 5)}, {P(7, 6), P(0, 8)}});
361
462
// Failure case for old code.
362
- // vector<Line> t({{P(3,0),P(3,3)},{P(5,3), P(5,0)}, {P(4,-2), P(0,1)}, {P(3,-1), P(0,-1)}});
363
463
// addInf(t);
364
464
// cout << polygonArea2(halfPlaneIntersection(t)) << endl;
365
465
// addInf(t);
0 commit comments