Skip to content

Commit 1c47a37

Browse files
committed
Started fixing the bugs
1 parent bf838c6 commit 1c47a37

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

content/geometry/HalfPlane.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,13 @@ vector<P> halfPlaneIntersection(vector<Line> vs) {
3636
if (angDiff(vs[i], vs[i - 1]) == 0) continue;
3737
while (ah<at && sideOf(sp(vs[i]),ans[at-1]) < 0) at--;
3838
while (i!=n && ah<at && sideOf(sp(vs[i]),ans[ah])<0) ah++;
39-
ans[at++] = lineInter(sp(vs[i]), sp(deq[at])).second;
39+
auto res = lineInter(sp(vs[i]), sp(deq[at]));
40+
auto res2 = lineInter2(sp(vs[i]), sp(deq[at]));
41+
// if (isnan(res2.x) || isinf(res2.x)) continue;
42+
bool b = (isnan(res2.x) || isinf(res2.y)) == (res.first != 1);
43+
assert(b);
44+
if (res.first != 1) continue;
45+
ans[at++] = res.second;
4046
deq[at] = vs[i];
4147
}
4248
if (at - ah <= 2) return {};

content/geometry/lineIntersection.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,8 @@ pair<int, P> lineInter(P s1, P e1, P s2, P e2) {
2828
else
2929
return {1, s2-(e2-s2)*s1.cross(e1, s2)/d};
3030
}
31+
template<class P> P lineInter2(P p1, P p2, P q1, P q2) {
32+
double a1 = q1.cross(q2, p1), a2 = -q1.cross(q2, p2);
33+
auto res = (p1 * a2 + p2 * a1) / (a1 + a2);
34+
return res;
35+
}

fuzz-tests/geometry/HalfPlane.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ typedef long long ll;
1010
typedef pair<int, int> pii;
1111
typedef vector<int> vi;
1212

13-
#include "../../content/geometry/HalfPlane.h"
1413
#include "../../content/geometry/PolygonArea.h"
15-
14+
typedef Point<double> P;
1615
ostream &operator<<(ostream &os, P p) { return cout << "(" << p.x << "," << p.y << ")"; }
16+
#include "../../content/geometry/HalfPlane.h"
17+
1718

1819
namespace sjtu {
1920
typedef double T;
@@ -203,7 +204,7 @@ void testEmpty() {
203204
}
204205
void testRandom() {
205206
int lim = 1e1;
206-
for (int i = 0; i < 10000; i++) {
207+
for (int i = 0; i < 10000000; i++) {
207208
vector<Line> t;
208209
for (int i = 0; i < 6; i++) {
209210
Line cand{P(0, 0), P(0, 0)};
@@ -214,9 +215,11 @@ void testRandom() {
214215
addInf(t, lim);
215216
auto res = halfPlaneIntersection(t);
216217
double area = sjtu::halfPlaneIntersection(t);
217-
double diff = abs(2 * area - polygonArea2(res));
218+
double resArea = polygonArea2(res)/2;
219+
if (isnan(resArea)) resArea = 0;
220+
double diff = abs(area - resArea);
218221
if (diff > EPS) {
219-
cout << diff << ' ' << area << ' ' << endl;
222+
cout << diff << ' ' << area << ' ' <<resArea<< endl;
220223
for (auto i : t)
221224
cout << i[0] << "->" << i[1] << ' ';
222225
cout << endl;
@@ -237,6 +240,10 @@ int main() {
237240
testRandom();
238241
// Failure case for MIT's half plane cod
239242
// vector<Line> t({{P(9, 8), P(9, 1)}, {P(3, 3), P(3, 5)}, {P(7, 6), P(0, 8)}});
243+
// Failure case for old code.
244+
// 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)}});
245+
// addInf(t);
246+
// cout << polygonArea2(halfPlaneIntersection(t)) << endl;
240247
// addInf(t);
241248
// cout << polygonArea2(halfPlaneIntersection(t)) << endl;
242249
// cout << MIT::Intersection_Area(convert(t)) << endl;

0 commit comments

Comments
 (0)