diff --git a/algorithms/geometry/polygon-lattice-points.cpp b/algorithms/geometry/polygon-lattice-points.cpp new file mode 100644 index 00000000..0ec410cc --- /dev/null +++ b/algorithms/geometry/polygon-lattice-points.cpp @@ -0,0 +1,33 @@ +ll cross(ll x1, ll y1, ll x2, ll y2) { + return x1 * y2 - x2 * y1; +} + +ll polygonArea(vector& pts) { + ll ats = 0; + for (int i = 2; i < len(pts); i++) + ats += cross(pts[i].first - pts[0].first, + pts[i].second - pts[0].second, + pts[i - 1].first - pts[0].first, + pts[i - 1].second - pts[0].second); + return abs(ats / 2ll); +} + +ll boundary(vector& pts) { + ll ats = pts.size(); + for (int i = 0; i < len(pts); i++) { + ll deltax = + (pts[i].first - pts[(i + 1) % pts.size()].first); + ll deltay = + (pts[i].second - pts[(i + 1) % pts.size()].second); + ats += abs(__gcd(deltax, deltay)) - 1; + } + return ats; +} + +pll latticePoints(vector& pts) { + ll bounds = boundary(pts); + ll area = polygonArea(pts); + ll inside = area + 1ll - bounds / 2ll; + + return {inside, bounds}; +} diff --git a/algorithms/geometry/polygon-lattice-points.tex b/algorithms/geometry/polygon-lattice-points.tex new file mode 100644 index 00000000..c9184db7 --- /dev/null +++ b/algorithms/geometry/polygon-lattice-points.tex @@ -0,0 +1,5 @@ +\subsection{Polygon Lattice Points (Pick's Theorem)} + +Given a polygon with $N$ points finds the number of lattice points inside and on boundaries. +Time : $O(N)$ +