Skip to content

Commit cbd17e8

Browse files
committed
Abstracted away the angle comparison
1 parent 19c70ac commit cbd17e8

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

content/geometry/HalfPlane.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,20 @@ typedef Point<double> P;
1919
typedef array<P, 2> Line;
2020
#define sp(a) a[0], a[1]
2121
#define ang(a) atan2((a[1] - a[0]).y, (a[1] - a[0]).x)
22+
23+
int angDiff(Line a, Line b) { return sgn(ang(a) - ang(b)); }
2224
bool cmp(Line a, Line b) {
23-
auto s = ang(a) - ang(b);
25+
auto s = angDiff(a, b);
2426
return s == 0 ? sideOf(sp(b), a[0]) >= 0 : s < 0;
2527
}
2628
vector<P> halfPlaneIntersection(vector<Line> vs) {
2729
sort(all(vs), cmp);
2830
vector<Line> deq(sz(vs) + 5);
2931
vector<P> ans(sz(vs) + 5);
30-
int dh = 0, dt = 1, ah = 0, at = 0;
3132
deq[0] = vs[0];
33+
int dh = 0, dt = 1, ah = 0, at = 0;
3234
for (int i = 1; i < sz(vs); ++i) {
33-
if (ang(vs[i]) - ang(vs[i - 1]) == 0) continue;
35+
if (angDiff(vs[i], vs[i - 1]) == 0) continue;
3436
while (ah<at && sideOf(sp(vs[i]),ans[at-1]) < 0) at--,dt--;
3537
while (ah<at && sideOf(sp(vs[i]),ans[ah]) < 0) ah++,dh++;
3638
ans[at++] = lineInter(sp(deq[dt - 1]), sp(vs[i])).second;

fuzz-tests/geometry/HalfPlane.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ typedef vector<int> vi;
1212

1313
#include "../../content/geometry/HalfPlane.h"
1414
#include "../../content/geometry/PolygonArea.h"
15-
typedef Point<double> P;
1615

1716
ostream &operator<<(ostream &os, P p) { return cout << "(" << p.x << "," << p.y << ")"; }
1817

@@ -203,8 +202,8 @@ void testEmpty() {
203202
assert(sz(res) == 0);
204203
}
205204
void testRandom() {
206-
int lim = 1e2;
207-
for (int i = 0; i < 1000; i++) {
205+
int lim = 1e1;
206+
for (int i = 0; i < 10000; i++) {
208207
vector<Line> t;
209208
for (int i = 0; i < 6; i++) {
210209
Line cand{P(0, 0), P(0, 0)};
@@ -228,8 +227,8 @@ void testRandom() {
228227
assert(diff < EPS);
229228
}
230229
}
231-
void testConvex() {
232-
230+
void testSelf() {
231+
int lim = 1e2;
233232
}
234233

235234
int main() {
@@ -239,6 +238,13 @@ int main() {
239238
testPoint();
240239
testEmpty();
241240
testRandom();
241+
vector<Line> t({{P(0,0), P(5,0)}, {P(0,1), P(5,1)}});
242+
reverse(all(t));
243+
addInf(t);
244+
245+
auto res = halfPlaneIntersection(t);
246+
for (auto i: res) cout<<i<<' ';
247+
cout<<endl;
242248
// Failure case for MIT's half plane code
243249
// vector<Line> t({{P(9, 8), P(9, 1)}, {P(3, 3), P(3, 5)}, {P(7, 6), P(0, 8)}});
244250
// addInf(t);

0 commit comments

Comments
 (0)