Skip to content

Commit 4330561

Browse files
committed
removed test cases
1 parent f3916db commit 4330561

File tree

5 files changed

+24
-164
lines changed

5 files changed

+24
-164
lines changed

content/geometry/HalfPlane.h

+9-32
Original file line numberDiff line numberDiff line change
@@ -25,46 +25,23 @@ 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-
std::random_device rd;
29-
std::mt19937 e2(rd());
30-
vector<P> halfPlaneIntersection(vector<Line> vs, const double EPS=1e-8) {
28+
vector<P> halfPlaneIntersection(vector<Line> vs) {
29+
const double EPS = sqrt(2) * 1e-8;
3130
sort(all(vs), cmp);
32-
// for (auto l: vs) {
33-
// cout<<l[0]<<" -> "<<l[1]<<endl;
34-
// }
3531
vector<Line> deq(sz(vs) + 5);
3632
vector<P> ans(sz(vs) + 5);
3733
deq[0] = vs[0];
3834
int ah = 0, at = 0, n = sz(vs);
3935
rep(i,1,n+1) {
4036
if (i == n) vs.push_back(deq[ah]);
41-
if (angDiff(vs[i], vs[i - 1]) == 0) {
42-
continue;
43-
}
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));
50-
if (res.first != 1) {
51-
// cout<<"46"<<endl;
52-
continue;
53-
}
37+
if (angDiff(vs[i], vs[i - 1]) == 0) continue;
38+
while (ah<at && sideOf(sp(vs[i]), ans[at-1], EPS) < 0)
39+
at--;
40+
while (i!=n && ah<at && sideOf(sp(vs[i]),ans[ah],EPS)<0)
41+
ah++;
42+
auto res = lineInter(sp(vs[i]), sp(deq[at]));
43+
if (res.first != 1) continue;
5444
ans[at++] = res.second, deq[at] = vs[i];
55-
// cout<<"i: "<<i<<endl;
56-
// cout<<"ans: ";
57-
// cout<<ah<<' '<<at<<endl;
58-
// for (int j=ah; j<at; j++) {
59-
// cout<<ans[j]<<" ";
60-
// }
61-
// cout<<endl;
62-
// cout<<"deq: ";
63-
// for(int j=ah; j<=at; j++) {
64-
// cout<<deq[j][0]<<"-> "<<deq[j][1]<<' ';
65-
// }
66-
// cout<<endl;
67-
// cout<<endl;
6845
}
6946
if (at - ah <= 2) return {};
7047
return {ans.begin() + ah, ans.begin() + at};

content/geometry/lineIntersection.h

-9
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,6 @@ 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) {
3728
auto d = (e1 - s1).cross(e2 - s2);

content/geometry/sideOf.h

+1-7
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,5 @@ template<class P>
1919
int sideOf(const P& s, const P& e, const P& p, double eps) {
2020
auto a = (e-s).cross(p-s);
2121
double l = (e-s).dist()*eps;
22-
if (a<=l && a>=-l) {
23-
return 0;
24-
} else if (a > l) {
25-
return 1;
26-
} else {
27-
return -1;
28-
}
22+
return (a > l) - (a < -l);
2923
}

content/strings/Hashing.h

+4-6
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@
1616
struct H {
1717
typedef uint64_t ull;
1818
ull x; H(ull x=0) : x(x) {}
19-
H operator+(H o) { return x + o.x + (x + o.x < x); }
20-
H operator*(H o) {
21-
ull r = x;
22-
asm("mul %1\n addq %%rdx, %0\n adcq $0,%0" : "+a"(r) : "r"(o.x) : "rdx");
23-
return r;
24-
} H operator-(H o) { return *this + ~o.x; }
19+
#define OP(O,A,B) H operator O(H o) { ull r = x; asm \
20+
(A "addq %%rdx, %0\n adcq $0,%0" : "+a"(r) : B); return r; }
21+
OP(+,,"d"(o.x)) OP(*,"mul %1\n", "r"(o.x) : "rdx")
22+
H operator-(H o) { return *this + ~o.x; }
2523
ull get() const { return x + !~x; }
2624
bool operator==(H o) const { return get() == o.get(); }
2725
bool operator<(H o) const { return get() < o.get(); }

stress-tests/geometry/HalfPlane.cpp

+10-110
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ typedef vector<int> vi;
1212

1313
#include "../../content/geometry/PolygonArea.h"
1414
typedef Point<double> P;
15-
ostream &operator<<(ostream &os, P p) { return cout << "(" << p.x << "," << p.y << ")"; }
15+
1616
#include "../../content/geometry/HalfPlane.h"
1717

1818
namespace sjtu {
1919
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; }
2222
struct Point {
2323
T x, y;
2424
Point() {}
@@ -255,7 +255,6 @@ vector<mit::Line> convert(vector<Line> x) {
255255
}
256256

257257
const double INF = 100;
258-
const double EPS = 1e-8;
259258
void addInf(vector<Line> &res, double INF = INF) {
260259
vector<P> infPts({P(INF, INF), P(-INF, INF), P(-INF, -INF), P(INF, -INF)});
261260
for (int i = 0; i < 4; i++)
@@ -301,7 +300,7 @@ void testEmpty() {
301300
auto res = halfPlaneIntersection(t);
302301
assert(sz(res) == 0);
303302
}
304-
void testRandom(const double EPS) {
303+
void testRandom() {
305304
int lim = 3;
306305
double mxDiff = 0;
307306
for (int i = 0; i < 10000000; i++) {
@@ -328,13 +327,12 @@ void testRandom(const double EPS) {
328327
t.push_back(cand);
329328
}
330329
addInf(t, lim);
331-
auto res = halfPlaneIntersection(t, EPS);
330+
auto res = halfPlaneIntersection(t);
332331
double area = sjtu::halfPlaneIntersection(t);
333332
double resArea = abs(polygonArea2(res) / 2);
334333
// double resArea = mit::Intersection_Area(convert(t));
335334
double diff = abs(area - resArea);
336335
mxDiff = max(diff, mxDiff);
337-
continue;
338336
if (diff > .1 || isnan(diff)) {
339337
cout << diff << ' ' << area << ' ' << resArea << endl;
340338
for (auto i : t)
@@ -350,8 +348,6 @@ void testRandom(const double EPS) {
350348
assert(false);
351349
}
352350
}
353-
cout<<"eps: "<<EPS<<endl;
354-
cout<<"mxDiff: "<<mxDiff<<endl;
355351
}
356352
vector<P> convexHull(vector<P> pts) {
357353
if (sz(pts) <= 1) return pts;
@@ -368,107 +364,11 @@ vector<P> convexHull(vector<P> pts) {
368364

369365

370366
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();
395372

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;
473373
cout << "Tests passed!" << endl;
474374
}

0 commit comments

Comments
 (0)