Skip to content

Commit

Permalink
Add Intersection of line and convex
Browse files Browse the repository at this point in the history
  • Loading branch information
baluteshih committed Oct 11, 2023
1 parent de420e5 commit 5f8e8a2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
32 changes: 32 additions & 0 deletions codebook/8_Geometry/Intersection_of_line_and_convex.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
int TangentDir(vector<pll> &C, pll dir) {
return cyc_tsearch(SZ(C), [&](int a, int b) {
return cross(dir, C[a]) > cross(dir, C[b]);
});
}
#define cmpL(i) sign(cross(C[i] - a, b - a))
pii lineHull(pll a, pll b, vector<pll> &C) {
int A = TangentDir(C, a - b);
int B = TangentDir(C, b - a);
int n = SZ(C);
if (cmpL(A) < 0 || cmpL(B) > 0)
return pii(-1, -1); // no collision
auto gao = [&](int l, int r) {
for (int t = l; (l + 1) % n != r; ) {
int m = ((l + r + (l < r ? 0 : n)) / 2) % n;
(cmpL(m) == cmpL(t) ? l : r) = m;
}
return (l + !cmpL(r)) % n;
};
pii res = pii(gao(B, A), gao(A, B)); // (i, j)
if (res.X == res.Y) // touching the corner i
return pii(res.X, -1);
if (!cmpL(res.X) && !cmpL(res.Y)) // along side i, i+1
switch ((res.X - res.Y + n + 1) % n) {
case 0: return pii(res.X, res.X);
case 2: return pii(res.Y, res.Y);
}
/* crossing sides (i, i+1) and (j, j+1)
crossing corner i is treated as side (i, i+1)
returned in the same order as the line hits the convex */
return res;
} // convex cut: (r, l]
2 changes: 2 additions & 0 deletions codebook/content.tex
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ \subsection{PointInConvex*} % test by CF 104114 B, CF 101242 J (World Finals' pr
\lstinputlisting{8_Geometry/PointInConvex.cpp}
\subsection{TangentPointToHull*} % test by CF 104114 B, CF 101242 J (World Finals' problem)
\lstinputlisting{8_Geometry/TangentPointToHull.cpp}
\subsection{Intersection of line and convex} % test by aizu CGL_4_C, probably not enough
\lstinputlisting{8_Geometry/Intersection_of_line_and_convex.cpp}
\subsection{minMaxEnclosingRectangle*} % test by UVA 819
\lstinputlisting{8_Geometry/minMaxEnclosingRectangle.cpp}
\subsection{VectorInPoly*} % test by qoj 5479
Expand Down

0 comments on commit 5f8e8a2

Please sign in to comment.