|
| 1 | +#include "../utilities/template.h" |
| 2 | +#include "./utilities.h" |
| 3 | + |
| 4 | +#include "../../content/geometry/HalfplaneIntersection.h" |
| 5 | +#include "../../content/geometry/ConvexHull.h" |
| 6 | + |
| 7 | +#pragma GCC optimize ("trapv") |
| 8 | + |
| 9 | +// Test against brote force with fractions |
| 10 | +namespace slow { |
| 11 | + |
| 12 | + typedef __int128_t i128; |
| 13 | + |
| 14 | + struct frac { |
| 15 | + i128 num, den; |
| 16 | + frac(i128 num_ = 0, i128 den_ = 1) : num(num_), den(den_) { |
| 17 | + i128 g = __gcd(num, den); |
| 18 | + num /= g; |
| 19 | + den /= g; |
| 20 | + if (den < 0) { |
| 21 | + num = -num; |
| 22 | + den = -den; |
| 23 | + } |
| 24 | + assert(den > 0); |
| 25 | + } |
| 26 | + bool operator<(frac f) const { |
| 27 | + return num * f.den < f.num * den; |
| 28 | + } |
| 29 | + bool operator==(frac f) const { |
| 30 | + return num == f.num && den == f.den; |
| 31 | + } |
| 32 | + bool operator<=(frac f) const { |
| 33 | + return num * f.den <= f.num * den; |
| 34 | + } |
| 35 | + bool operator>(frac f) const { |
| 36 | + return num * f.den > f.num * den; |
| 37 | + } |
| 38 | + frac operator+(frac o) const { |
| 39 | + return frac(num * o.den + o.num * den, den * o.den); |
| 40 | + } |
| 41 | + frac operator-(frac o) const { |
| 42 | + return frac(num * o.den - o.num * den, den * o.den); |
| 43 | + } |
| 44 | + frac operator*(frac o) const { |
| 45 | + return frac(num * o.num, den * o.den); |
| 46 | + } |
| 47 | + frac operator/(frac o) const { |
| 48 | + return frac(num * o.den, den * o.num); |
| 49 | + } |
| 50 | + }; |
| 51 | + |
| 52 | + typedef Point<frac> Pf; |
| 53 | + |
| 54 | + vector<Pf> slow(const vector<pair<Pf,Pf>>& t) { |
| 55 | + ll n = SZ(t); |
| 56 | + vector<Pf> points; |
| 57 | + fore(i, 0, n) fore(j, 0, i) { |
| 58 | + auto [si, ei] = t[i]; |
| 59 | + auto [sj, ej] = t[j]; |
| 60 | + auto [x, p] = lineInter(si, ei, sj, ej); |
| 61 | + if (x == 1) { |
| 62 | + points.push_back(p); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + vector<Pf> ans; |
| 67 | + for (Pf p : points) { |
| 68 | + bool valid = true; |
| 69 | + for (auto [s, e] : t) { |
| 70 | + ll side = sideOf(s, e, p); |
| 71 | + if (side == -1) { |
| 72 | + valid = false; |
| 73 | + break; |
| 74 | + } |
| 75 | + } |
| 76 | + if (valid) { |
| 77 | + ans.push_back(p); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + ans = convexHull(ans); |
| 82 | + return ans; |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +typedef slow::Pf Pf; |
| 87 | + |
| 88 | +P Pf_to_P(Pf p) { |
| 89 | + return P((double)p.x.num / p.x.den, (double)p.y.num / p.y.den); |
| 90 | +} |
| 91 | + |
| 92 | +Pf randIntPt(ll lim) { |
| 93 | + return Pf{rand() % (2 * lim + 1) - lim, rand() % (2 * lim + 1) - lim}; |
| 94 | +} |
| 95 | + |
| 96 | +slow::frac randFrac(ll lim) { |
| 97 | + ll den = rand() % lim + 1; |
| 98 | + ll num = rand() % den; |
| 99 | + return slow::frac(num, den); |
| 100 | +} |
| 101 | + |
| 102 | +Pf randDoublePt(ll lim) { |
| 103 | + Pf ans = randIntPt(lim); |
| 104 | + ans.x = ans.x + randFrac(lim / 2); |
| 105 | + ans.y = ans.y + randFrac(lim / 2); |
| 106 | + return ans; |
| 107 | +} |
| 108 | + |
| 109 | + |
| 110 | +const slow::frac INF = 500; |
| 111 | +void addInf(vector<pair<Pf,Pf>> &ans, slow::frac INF = INF) { |
| 112 | + slow::frac nINF = slow::frac() - INF; |
| 113 | + vector<Pf> infPts({Pf(INF, INF), Pf(nINF, INF), Pf(nINF, nINF), Pf(INF, nINF)}); |
| 114 | + fore(i, 0, 4) { |
| 115 | + ans.push_back({infPts[i], infPts[(i + 1) % 4]}); |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +void test(const vector<pair<Pf,Pf>>& t) { |
| 120 | + const double eps = 1e-11; |
| 121 | + vector<Line> t_(SZ(t)); |
| 122 | + fore(i, 0, SZ(t)) { |
| 123 | + t_[i] = {Pf_to_P(t[i].first), Pf_to_P(t[i].second)}; |
| 124 | + } |
| 125 | + vector<P> ans = halfPlaneIntersection(t_); |
| 126 | + assert(isConvexCCW(ans, eps)); |
| 127 | + ans = convexHull(ans); // Remove colinear |
| 128 | + vector<Pf> ansf = slow::slow(t); |
| 129 | + vector<P> ans2(SZ(ansf)); |
| 130 | + fore(i, 0, SZ(ansf)) { |
| 131 | + ans2[i] = Pf_to_P(ansf[i]); |
| 132 | + } |
| 133 | + assert(polygonEq(ans, ans2, eps)); |
| 134 | +} |
| 135 | + |
| 136 | +void testRandomInt() { |
| 137 | + ll n = rand() % 10 + 1; |
| 138 | + vector<pair<Pf,Pf>> t; |
| 139 | + fore(i, 0, n) { |
| 140 | + Pf p = randIntPt(10); |
| 141 | + Pf q = randIntPt(10); |
| 142 | + if (p == q) continue; |
| 143 | + t.push_back({p, q}); |
| 144 | + } |
| 145 | + addInf(t); |
| 146 | + test(t); |
| 147 | +} |
| 148 | + |
| 149 | +void testRandomDouble() { |
| 150 | + ll n = rand() % 10 + 1; |
| 151 | + vector<pair<Pf,Pf>> t; |
| 152 | + fore(i, 0, n) { |
| 153 | + Pf p = randDoublePt(10); |
| 154 | + Pf q = randDoublePt(10); |
| 155 | + if (p == q) continue; |
| 156 | + t.push_back({p, q}); |
| 157 | + } |
| 158 | + addInf(t); |
| 159 | + test(t); |
| 160 | +} |
| 161 | + |
| 162 | +int main() { |
| 163 | + |
| 164 | + vector<vector<pair<Pf,Pf>>> handmade = { |
| 165 | + {{Pf(0, 0), Pf(5, 0)}, {Pf(5, -2), Pf(5, 2)}, {Pf(5, 2), Pf(2, 2)}, {Pf(0, 3), Pf(0, -3)}}, |
| 166 | + {{Pf(0, 0), Pf(5, 0)}}, |
| 167 | + {{Pf(0, 0), Pf(5, 0)}, {Pf(5, 0), Pf(0, 0)}}, // Line |
| 168 | + {{Pf(0, 0), Pf(5, 0)}, {Pf(5, 0), Pf(0, 0)}, {Pf(0, 0), Pf(0, 5)}, {Pf(0, 5), Pf(0, 0)}}, // Point |
| 169 | + {{Pf(0, 0), Pf(5, 0)}, {Pf(5, 0), Pf(0, 0)}, {Pf(0, 0), Pf(0, 5)}, {Pf(0, 5), Pf(0, 0)}, {Pf(0, 2), Pf(5, 2)}}, // Empty |
| 170 | + {{Pf(0, 0), Pf(5, 0)}, {Pf(5, 0), Pf(5, 5)}, {Pf(5, 5), Pf(0, 5)}, {Pf(0, 5), Pf(0, 0)}, {Pf(1, 5), Pf(1, 0)}}, // Parallel lines |
| 171 | + {{Pf(0, 0), Pf(5, 0)}, {Pf(5, 0), Pf(5, 5)}, {Pf(5, 5), Pf(0, 5)}, {Pf(1, 5), Pf(1, 0)}, {Pf(0, 5), Pf(0, 0)}}, // Parallel lines |
| 172 | + {{Pf(0, 0), Pf(1, 0)}, {Pf(0, 0), Pf(2, 0)}, {Pf(0, 0), Pf(3, 0)}} |
| 173 | + }; |
| 174 | + |
| 175 | + for (auto& t : handmade) { |
| 176 | + addInf(t); |
| 177 | + test(t); |
| 178 | + } |
| 179 | + |
| 180 | + fore(_, 0, 1000) { |
| 181 | + testRandomInt(); |
| 182 | + testRandomDouble(); |
| 183 | + } |
| 184 | + |
| 185 | + cout << "Tests passed!" << endl; |
| 186 | +} |
0 commit comments