Skip to content

Commit 5f4ff5a

Browse files
committed
Added some experimentation code
1 parent 8971c03 commit 5f4ff5a

File tree

3 files changed

+36
-26
lines changed

3 files changed

+36
-26
lines changed

content/geometry/HalfPlane.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ bool cmp(Line a, Line b) {
2525
auto s = angDiff(a, b);
2626
return s == 0 ? sideOf(sp(b), a[0]) >= 0 : s < 0;
2727
}
28-
vector<P> halfPlaneIntersection(vector<Line> vs) {
28+
std::random_device rd;
29+
std::mt19937 e2(rd());
30+
vector<P> halfPlaneIntersection(vector<Line> vs, const double EPS=1e-8) {
2931
sort(all(vs), cmp);
3032
// for (auto l: vs) {
3133
// cout<<l[0]<<" -> "<<l[1]<<endl;
@@ -39,10 +41,12 @@ vector<P> halfPlaneIntersection(vector<Line> vs) {
3941
if (angDiff(vs[i], vs[i - 1]) == 0) {
4042
continue;
4143
}
42-
43-
while (ah<at && sideOf(sp(vs[i]),ans[at-1], 1e-8) < 0) at--;
44-
while (i!=n && ah<at && sideOf(sp(vs[i]),ans[ah], 1e-8)<0) ah++;
45-
auto res = lineInter(sp(vs[i]), sp(deq[at]));
44+
const double mult = 1.414;
45+
std::uniform_real_distribution<> dist(-EPS, EPS);
46+
while (ah<at && sideOf(sp(vs[i]),ans[at-1], mult*EPS) < 0) at--;
47+
while (i!=n && ah<at && sideOf(sp(vs[i]),ans[ah], mult*EPS)<0) ah++;
48+
auto res = lineInter(sp(vs[i]), sp(deq[at]));
49+
res.second = res.second + P(dist(e2), dist(e2));
4650
if (res.first != 1) {
4751
// cout<<"46"<<endl;
4852
continue;

content/geometry/lineIntersection.h

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,20 @@ Products of three coordinates are used in intermediate steps so watch out for ov
2323

2424
#include "Point.h"
2525

26-
template<class P>
27-
pair<int, P> lineInter(P s1, P e1, P s2, P e2) {
28-
auto d = (e1-s1).cross(e2-s2);
29-
if (d == 0) //if parallel
30-
return {-((e1-s1).cross(s2-s1)==0 || s2==e2), P(0,0)};
31-
else
32-
return {1, s2-(e2-s2)*(e1-s1).cross(s2-s1)/d};
33-
}
34-
3526
// template<class P>
3627
// pair<int, P> lineInter(P s1, P e1, P s2, P e2) {
37-
// auto d = (e1 - s1).cross(e2 - s2);
38-
// if (d == 0) // if parallel
39-
// return {-(s1.cross(e1, s2) == 0), P(0, 0)};
40-
// auto p = s2.cross(e1, e2), q = s2.cross(e2, s1);
41-
// return {1, (s1 * p + e1 * q) / d};
28+
// auto d = (e1-s1).cross(e2-s2);
29+
// if (d == 0) //if parallel
30+
// return {-((e1-s1).cross(s2-s1)==0 || s2==e2), P(0,0)};
31+
// else
32+
// return {1, s2-(e2-s2)*(e1-s1).cross(s2-s1)/d};
4233
// }
34+
35+
template<class P>
36+
pair<int, P> lineInter(P s1, P e1, P s2, P e2) {
37+
auto d = (e1 - s1).cross(e2 - s2);
38+
if (d == 0) // if parallel
39+
return {-(s1.cross(e1, s2) == 0), P(0, 0)};
40+
auto p = s2.cross(e1, e2), q = s2.cross(e2, s1);
41+
return {1, (s1 * p + e1 * q) / d};
42+
}

fuzz-tests/geometry/HalfPlane.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,9 @@ void testEmpty() {
301301
auto res = halfPlaneIntersection(t);
302302
assert(sz(res) == 0);
303303
}
304-
void testRandom() {
305-
int lim = 5;
304+
void testRandom(const double EPS) {
305+
int lim = 3;
306+
double mxDiff = 0;
306307
for (int i = 0; i < 10000000; i++) {
307308
vector<Line> t;
308309
for (int i = 0; i < 4; i++) {
@@ -327,12 +328,14 @@ void testRandom() {
327328
t.push_back(cand);
328329
}
329330
addInf(t, lim);
330-
auto res = halfPlaneIntersection(t);
331+
auto res = halfPlaneIntersection(t, EPS);
331332
double area = sjtu::halfPlaneIntersection(t);
332333
double resArea = abs(polygonArea2(res) / 2);
333334
// double resArea = mit::Intersection_Area(convert(t));
334335
double diff = abs(area - resArea);
335-
if (diff > EPS || isnan(diff)) {
336+
mxDiff = max(diff, mxDiff);
337+
continue;
338+
if (diff > .1 || isnan(diff)) {
336339
cout << diff << ' ' << area << ' ' << resArea << endl;
337340
for (auto i : t)
338341
cout << i[0] << "->" << i[1] << ' ';
@@ -344,9 +347,11 @@ void testRandom() {
344347
for (auto i : res)
345348
cout << i << ',';
346349
cout << endl;
350+
assert(false);
347351
}
348-
assert(diff <= EPS);
349352
}
353+
cout<<"eps: "<<EPS<<endl;
354+
cout<<"mxDiff: "<<mxDiff<<endl;
350355
}
351356
vector<P> convexHull(vector<P> pts) {
352357
if (sz(pts) <= 1) return pts;
@@ -367,8 +372,9 @@ int main() {
367372
// testInf();
368373
// testLine();
369374
// testPoint();
370-
// testRandom();
371-
// return 0;
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;
372378
// testEmpty();
373379
// Case that messes with precision
374380
// 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)}});

0 commit comments

Comments
 (0)