Lattice points and Pick's theorem
The points with whole-number coordinates form a lattice, and polygons with corners on it have areas that can be found by counting.
pick([[0, 0], [5, 0], [0, 5]]); // { area: 25/2, interior: 6, boundary: 15, formula: 25/2, holds: true }
latticePointsOn([0, 0], [6, 4]); // 3: the ends and (3, 2)
isVisible(3, 5); // true: gcd(3, 5) = 1
visiblePoints(100); // { count: 6087, share: 6087/10000, … }
Pick's theorem
For a polygon whose corners are lattice points and whose edges do not cross, with I lattice points strictly inside and B on the boundary,
pick finds the three quantities independently, so that holds is a check and not a restatement. The area comes from the shoelace formula. B comes from the edges: the segment from (0, 0) to (a, b) passes through gcd(a, b) + 1 lattice points, since the first one after the origin is (a/g, b/g). I is counted by testing every lattice point of the bounding box with the exact point-in-polygon test.
The theorem fails for polygons with holes and has no analogue in three dimensions: a tetrahedron with corners (0,0,0), (1,0,0), (0,1,0), (1,1,n) contains no other lattice points, yet its volume n/6 can be as large as you like.
Visible points
From the origin, the lattice point (x, y) is visible when no other lattice point lies on the segment to it, which is when gcd(x, y) = 1. The share of visible points in a large square tends to
because two numbers avoid sharing the prime factor p with probability , and the product of these over all primes is . visiblePoints(n) gives the exact count in the n by n square and its share as a fraction; that the share approaches is a limit it illustrates and does not prove.
| call | returns |
|---|---|
| pick(vertices | polygon) | { area, interior, boundary, formula, holds } |
| latticePointsOn(p, q) | number of lattice points on the closed segment |
| isVisible(x, y) | boolean |
| visiblePoints(n) | { n, count, share, limit } |
@ratmath/geo and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.