For a simple polygon whose vertices are lattice points:
where is the number of interior lattice points and the number of lattice points on the boundary.
Computing the three quantities
Area — shoelace, exactly:
long long twiceArea = 0;
for (int i = 0; i < n; i++) twiceArea += cross(p[i], p[(i+1) % n]);
twiceArea = llabs(twiceArea);Boundary points — each edge from to contributes lattice points, counting one endpoint per edge:
long long B = 0;
for (int i = 0; i < n; i++) {
P d = p[(i+1) % n] - p[i];
B += __gcd(llabs(d.x), llabs(d.y));
}Interior points — rearrange Pick:
long long I = (twiceArea - B + 2) / 2; // always an integerEverything is exact integer arithmetic — no floating point anywhere.
Why the edge count is a gcd
The lattice points strictly between and are at multiples of the primitive vector with . There are of them, plus one endpoint, giving per edge when counted consistently around the polygon.
What it is for
| Problem | Use |
|---|---|
| Count lattice points inside a polygon | |
| Count lattice points on the boundary | the gcd sum |
| Count all lattice points in a polygon | |
| Verify a polygon’s area from lattice counts | Pick’s formula |
| Lattice points in a triangle | Pick, or a direct gcd argument |
| Lattice points under a line (floor sums) | a different technique — see below |
What it does not do
- 3D has no analogue. Reeve tetrahedra have the same lattice-point counts but different volumes, so no Pick-style formula exists in three dimensions.
- Non-lattice vertices break it entirely.
- Self-intersecting polygons break it (the “area” is not well defined).
- Polygons with holes: where is the number of holes (using the Euler characteristic correction).
The related counting problem: lattice points under a line
Pick’s theorem does not handle “count ”. That is the floor sum / Euclidean-like algorithm:
long long floorSum(long long n, long long m, long long a, long long b) {
long long ans = 0;
if (a >= m) { ans += (n - 1) * n / 2 * (a / m); a %= m; }
if (b >= m) { ans += n * (b / m); b %= m; }
long long yMax = (a * n + b) / m;
if (yMax == 0) return ans;
ans += yMax * (n - (m * yMax - b + a - 1) / a);
ans += floorSum(yMax, a, m, (a - (m * yMax - b) % a) % a);
return ans;
}by a Euclidean-style recursion — the standard tool for counting lattice points under a line, and a good companion to Pick’s theorem.
Worked example
The triangle :
- , so ;
- boundary: ;
- .
The interior points are — three, as predicted.
See also: Polygon Area · General Number Theory · Euclidean Algorithm