Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algorithm to Calculate the Number of Lattice Points in a Polygon

Tags:

algorithm

math

I'm trying to find the number of lattice points that strictly lie inside the boundary. I know Pick's theorem is

A = i + b/2 - 1

where A = the area of the polygon, i is the number of lattice points that lie inside the polygon, and b is the number of lattice points on the perimeter of the polygon.

I can easily find the area using the Shoelace formula, but I'm not sure how to get the points on the boundary.

I'm not really sure where to look for resource on this, so I'd appreciate links too.

like image 607
user3651139 Avatar asked May 19 '14 03:05

user3651139


People also ask

How do you find the number of lattice points?

This question already has an answer here: Simple Cubic: 8⋅1/8=1 lattice point per unit volume. Body Centered Cubic: (8⋅1/8)+1=2 lattice points per unit volume. Face Centered Cubic: (8⋅1/8)+(6⋅1/2)=4 lattice points per unit volume.

How do you count lattice points in a circle?

Count Lattice Points Inside a Circle - LeetCode. Given a 2D integer array circles where circles[i] = [xi, yi, ri] represents the center (xi, yi) and radius ri of the ith circle drawn on a grid, return the number of lattice points that are present inside at least one circle.

How do you do the Pick's Theorem?

Pick's Theorem states that if a polygon has vertices with integer coordinates (lattice points) then the area of the polygon is i + {1\over 2}p - 1 where i is the number of lattice points inside the polygon and p is the number of lattice points on the perimeter of the polygon.

How do you find the area of a lattice polygon?

Lattice Polygons A lattice polygon is a shape made of straight lines whose vertices are all lattice points and Pick's theorem gives a formula for the area of a lattice polygon. ( Area of the polygon P ) = I ( P ) + 1 2 B ( P ) − 1.


1 Answers

What a pretty question...

Since you are talking about Pick's Theorem, I will assume all of the vertices have integer coordinates.

Your question reduces to determining how many lattice points lie on the line segment from (x1, y1) to (x2, y2). Since the answer stays the same under translation by an integer, this reduces to determining how many lattice points lie on the line segment from (0, 0) to (x, y) for arbitrary x and y.

If x=0 or y=0, the answer is 1D and trivial (i.e. x+1 or y+1).

Otherwise, the answer is gcd(x,y) + 1. You prove this by showing (a) that any lattice point between (0,0) and (x,y) must be a multiple of the "least" lattice point; and (b) that any lattice point must have coordinates that are factors of (x,y).

Finally, be careful not to double-count the vertices as you walk around the polygon.

like image 145
Nemo Avatar answered Oct 26 '22 23:10

Nemo