Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Halfplane Intersection #90

Open
wants to merge 48 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
93c9b26
Updated onSegment.h and both segment intersections
Chillee Apr 26, 2019
3b6ac0e
Updated headers
Chillee Apr 26, 2019
0a0261b
Updated some line width issues
Chillee Apr 26, 2019
bccd1cb
Fixed broken dependency
Chillee Apr 26, 2019
d2e700a
removed automatic epsilon checks
Chillee Apr 27, 2019
59f8511
Put it on one line now
Chillee Apr 27, 2019
1bdf870
Moved template up a line to fit parameters on one line
Chillee Apr 29, 2019
2f32d63
Changed tabs to spaces so it'll align
Chillee Apr 29, 2019
28456a1
Fixed some misc issues
Chillee Apr 29, 2019
b7a8157
Readded relevant description
Chillee Apr 29, 2019
ba9395d
Fixed formatting issue
Chillee Apr 30, 2019
fab7b2e
made change
Chillee May 2, 2019
e99191f
Added first commit of halfPlane
Chillee May 2, 2019
95b770b
merged
Chillee May 4, 2019
63425c3
Simplified/shortened the code
Chillee May 5, 2019
888dd17
merged
Chillee May 5, 2019
ed75bf9
Fixed some issues
Chillee May 5, 2019
8571390
Added some unit tests for halfplane
Chillee May 5, 2019
e5b85b7
Add failure example for MIT's halfplane code
Chillee May 5, 2019
0a4e8d3
Added fuzz tests with SJTU code
Chillee May 5, 2019
4df6e0e
Fit stuff within 63 columns
Chillee May 5, 2019
19c70ac
Added option for long double in Halfplane tests
Chillee May 5, 2019
cbd17e8
Abstracted away the angle comparison
Chillee May 5, 2019
2eee5c1
Added newline
Chillee May 5, 2019
a1d85bb
Updated header
Chillee May 5, 2019
b0b03a9
Responded to comments and tried a macro
Chillee May 5, 2019
81f9cd3
removed extraneous deque variables
Chillee May 5, 2019
d283267
Updated description and such
Chillee May 5, 2019
8c6bb7c
Shortened half plane through macros
Chillee May 5, 2019
bf838c6
Shortened half plane a bit
Chillee May 5, 2019
1c47a37
Started fixing the bugs
Chillee May 6, 2019
1aa4b39
Fixed bugs/found bugs
Chillee May 6, 2019
e61acd5
Doing some precision analysis
Chillee May 6, 2019
4cb642a
Updated with current version
Chillee May 6, 2019
50a3915
Shortened a bit
Chillee May 6, 2019
3d7e6f0
removed binary
Chillee May 9, 2019
a8b3ec7
Removed some debug code
Chillee May 14, 2019
b6f7c82
merged
Chillee Oct 23, 2019
8971c03
some debugging stuff
Chillee Oct 26, 2019
5f4ff5a
Added some experimentation code
Chillee Oct 31, 2019
3a94b90
merged
Chillee Jun 26, 2020
74faa81
remove unintended
Chillee Jun 26, 2020
f3916db
removed test cases
Chillee Jun 26, 2020
4330561
removed test cases
Chillee Jun 26, 2020
908a313
Added my current guess about the condition for failure
Chillee Jun 26, 2020
34c550e
Warning fixes
simonlindholm Oct 20, 2020
5fb4588
Use .angle()
simonlindholm Oct 20, 2020
26b50ee
Fix cmp to not say a < a
simonlindholm Oct 20, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions content/geometry/HalfPlane.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Author: chilli
* Date: 2019-05-05
* License: CC0
* Source: https://github.com/zimpha/algorithmic-library/blob/master/computational-geometry/polygon.cc
* Description: Computes the intersection of a set of half-planes. Input is given as a set of planes, facing left.
* Output is the convex polygon representing the intersection. The points may have duplicates and be collinear.
* Will not fail catastrophically if `eps > sqrt(2)(line intersection error)`. Likely to work for more ranges if
* 3 half planes are never guaranteed to intersect at the same point.
* Time: O(n \log n)
* Status: fuzz-tested, submitted on https://maps19.kattis.com/problems/marshlandrescues
* Usage:
*/
#pragma once

#include "Point.h"
#include "sideOf.h"
#include "lineIntersection.h"

typedef Point<double> P;
typedef array<P, 2> Line;
#define sp(a) a[0], a[1]
#define ang(a) (a[1] - a[0]).angle()

int angDiff(Line a, Line b) { return sgn(ang(a) - ang(b)); }
bool cmp(Line a, Line b) {
int s = angDiff(a, b);
return (s ? s : sideOf(sp(a), b[0])) < 0;
}
vector<P> halfPlaneIntersection(vector<Line> vs) {
const double EPS = sqrt(2) * 1e-8;
sort(all(vs), cmp);
vector<Line> deq(sz(vs) + 5);
vector<P> ans(sz(vs) + 5);
deq[0] = vs[0];
int ah = 0, at = 0, n = sz(vs);
rep(i,1,n+1) {
if (i == n) vs.push_back(deq[ah]);
if (angDiff(vs[i], vs[i - 1]) == 0) continue;
while (ah<at && sideOf(sp(vs[i]), ans[at-1], EPS) < 0)
at--;
while (i!=n && ah<at && sideOf(sp(vs[i]),ans[ah],EPS)<0)
ah++;
auto res = lineInter(sp(vs[i]), sp(deq[at]));
if (res.first != 1) continue;
ans[at++] = res.second, deq[at] = vs[i];
}
if (at - ah <= 2) return {};
return {ans.begin() + ah, ans.begin() + at};
}
7 changes: 5 additions & 2 deletions content/geometry/PolygonArea.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
#include "Point.h"

template<class T>
T polygonArea2(vector<Point<T>>& v) {
T polygonArea2(vector<Point<T>> v) {
Chillee marked this conversation as resolved.
Show resolved Hide resolved
if (sz(v) < 3) return 0;
T a = v.back().cross(v[0]);
rep(i,0,sz(v)-1) a += v[i].cross(v[i+1]);
rep(i,0,sz(v)-1) {
a += v[i].cross(v[i+1]);
}
return a;
}
Loading