Skip to content

Commit b0b03a9

Browse files
committed
Responded to comments and tried a macro
1 parent a1d85bb commit b0b03a9

File tree

5 files changed

+16
-29
lines changed

5 files changed

+16
-29
lines changed

content/geometry/HalfPlane.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,15 @@ vector<P> halfPlaneIntersection(vector<Line> vs) {
3131
vector<P> ans(sz(vs) + 5);
3232
deq[0] = vs[0];
3333
int dh = 0, dt = 1, ah = 0, at = 0;
34+
#define upd(l, pt, op) while (ah < at && sideOf(sp(l), ans[pt]) < 0) op;
3435
for (int i = 1; i < sz(vs); ++i) {
3536
if (angDiff(vs[i], vs[i - 1]) == 0) continue;
36-
while (ah<at && sideOf(sp(vs[i]),ans[at-1]) < 0) at--,dt--;
37-
while (ah<at && sideOf(sp(vs[i]),ans[ah]) < 0) ah++,dh++;
37+
upd(vs[i], at-1, (at--,dt--)) upd(vs[i], ah, (ah++,dh++));
3838
ans[at++] = lineInter(sp(deq[dt - 1]), sp(vs[i])).second;
3939
deq[dt++] = vs[i];
4040
}
41-
while (ah<at && sideOf(sp(deq[dh]),ans[at-1]) < 0) at--,dt--;
42-
while (ah<at && sideOf(sp(deq[dt]),ans[ah]) < 0) ah++, dh++;
41+
upd(deq[dh], at-1, (at--,dt--)); upd(deq[dt], ah, (ah++,dh++));
4342
if (dt - dh <= 2) return {};
44-
ans[at++] = lineInter(sp(deq[dh]), sp(deq[dt - 1])).second;
43+
ans[at++] = lineInter(sp(deq[dt-1]), sp(deq[dh])).second;
4544
return {ans.begin() + ah, ans.begin() + at};
4645
}

content/geometry/PolygonCut.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,8 @@ vector<P> polygonCut(const vector<P>& poly, P s, P e) {
2828
rep(i,0,sz(poly)) {
2929
P cur = poly[i], prev = i ? poly[i-1] : poly.back();
3030
bool side = s.cross(e, cur) < 0;
31-
if (side != (s.cross(e, prev) < 0)) {
32-
res.emplace_back();
33-
lineIntersection(s, e, cur, prev, res.back());
34-
}
31+
if (side != (s.cross(e, prev) < 0))
32+
res.push_back(lineInter(s, e, cur, prev).second);
3533
if (side)
3634
res.push_back(cur);
3735
}
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
/**
2-
* Author: Ulf Lundstrom
3-
* Date: 2009-03-21
2+
* Author: Victor Lecomte, chilli
3+
* Date: 2019-05-05
44
* License: CC0
5-
* Source:
5+
* Source: https://vlecomte.github.io/cp-geo.pdf
66
* Description:\\
77
\begin{minipage}{75mm}
8-
If a unique intersection point of the lines going through s1,e1 and s2,e2 exists r is set to this point and 1 is returned. If no intersection point exists 0 is returned and if infinitely many exists -1 is returned. If s1==e1 or s2==e2 -1 is returned. The wrong position will be returned if P is Point<ll> and the intersection point does not have integer coordinates. Products of three coordinates are used in intermediate steps so watch out for overflow if using int or long long.
8+
If a unique intersection point of the lines going through s1,e1 and s2,e2 exists {1, point} is returned. If no intersection point exists {0, (0,0)} is returned and if infinitely many exists {-1, (0,0)} is returned. The wrong position will be returned if P is Point<ll> and the intersection point does not have integer coordinates. Products of three coordinates are used in intermediate steps so watch out for overflow if using int or long long.
99
\end{minipage}
1010
\begin{minipage}{15mm}
1111
\includegraphics[width=\textwidth]{content/geometry/lineIntersection}
1212
\end{minipage}
13-
* Status: tested
13+
* Status: tested through half-plane tests
1414
* Usage:
15-
* point<double> intersection;
16-
* if (1 == LineIntersection(s1,e1,s2,e2,intersection))
17-
* cout << "intersection point at " << intersection << endl;
15+
* auto res = lineInter(s1,e1,s2,e2);
16+
* if (res.first == 1)
17+
* cout << "intersection point at " << res.second << endl;
1818
*/
1919
#pragma once
2020

@@ -24,7 +24,7 @@ template<class P>
2424
pair<int, P> lineInter(P s1, P e1, P s2, P e2) {
2525
auto d = (e1-s1).cross(e2-s2);
2626
if (d == 0) //if parallel
27-
return {-((e1-s1).cross(s2-s1)==0 || s2==e2), P(0,0)};
27+
return {-(s1.cross(e1, s2)==0 || s2==e2), P(0,0)};
2828
else
29-
return {1, s2-(e2-s2)*(e1-s1).cross(s2-s1)/d};
29+
return {1, s2-(e2-s2)*s1.cross(e1, s2)/d};
3030
}

fuzz-tests/geometry/HalfPlane

542 KB
Binary file not shown.

fuzz-tests/geometry/HalfPlane.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,6 @@ void testRandom() {
227227
assert(diff < EPS);
228228
}
229229
}
230-
void testSelf() {
231-
int lim = 1e2;
232-
}
233230

234231
int main() {
235232
test1();
@@ -238,13 +235,6 @@ int main() {
238235
testPoint();
239236
testEmpty();
240237
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;
248238
// Failure case for MIT's half plane code
249239
// vector<Line> t({{P(9, 8), P(9, 1)}, {P(3, 3), P(3, 5)}, {P(7, 6), P(0, 8)}});
250240
// addInf(t);

0 commit comments

Comments
 (0)