integer points
lattice points
triangles
geometry
combinatorics

How many integer points within the three points forming a triangle?

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Triangles are fundamental structures in geometry, and determining the number of integer points (points with integer coordinates) within a triangle is an intriguing problem. This article explores how to calculate the number of integer points within a triangle formed by three given points on a Cartesian plane.

Understanding the Problem

A triangle is defined by its vertices, which are given as coordinate pairs on a plane. The task is to determine how many points with integer-coordinate pairs lie inside (and sometimes on the boundary of) the triangle.

Given three points, A(x1,y1)A(x_1, y_1), B(x2,y2)B(x_2, y_2), and C(x3,y3)C(x_3, y_3), we are interested in determining the integer lattice points (points where both coordinates are integers) that lie within the triangle they form.

Pick's Theorem

An essential tool for solving this problem is Pick's Theorem, which applies to simple polygons with vertices on integer points. Pick's Theorem states:

A=I+B21A = I + \frac{B}{2} - 1

Where: • AA is the area of the polygon. • II is the number of interior integer points. • BB is the number of boundary integer points.

Calculating the Area

For a triangle, the area AA can be calculated using the determinant method (the shoelace formula):

A=12x1(y2y3)+x2(y3y1)+x3(y1y2)A = \frac{1}{2} \left| x_1(y_2-y_3) + x_2(y_3-y_1) + x_3(y_1-y_2) \right|

This formula provides the absolute value of the area of the triangle.

Calculating Boundary Points

The boundary points are those that lie on the edges of the triangle. For an edge between two points, say A(x1,y1)A(x_1, y_1) and B(x2,y2)B(x_2, y_2), the number of integer points on this edge, excluding the endpoints, can be calculated using the greatest common divisor (GCD):

BAB=gcd(x2x1,y2y1)B_{AB} = \gcd(|x_2 - x_1|, |y_2 - y_1|)

For the complete boundary count, sum up the points on all three sides and add the 3 vertices:

B=BAB+BBC+BCA+3B = B_{AB} + B_{BC} + B_{CA} + 3

Calculating Interior Points

With the area and boundary points known, the number of interior integer points II can be determined using Pick's theorem rearranged as:

I=AB2+1I = A - \frac{B}{2} + 1

Example Calculation

Consider a triangle with vertices A(0,0)A(0, 0), B(4,0)B(4, 0), and C(0,3)C(0, 3).

  1. Calculate Area:
    Using the shoelace formula:
    A=120(03)+4(30)+0(00)=6A = \frac{1}{2} \left| 0(0-3) + 4(3-0) + 0(0-0) \right| = 6
  2. Calculate Boundary Points:
    BABB_{AB}: The edge from AA to BB has 4 points. • BBCB_{BC}: The edge from BB to CC has 3 points. • BCAB_{CA}: The edge from CC to AA has 3 points.
    Total boundary points including vertices:
    B=(41)+(31)+(31)+3=11B = (4-1) + (3-1) + (3-1) + 3 = 11
  3. Calculate Interior Points:
    Using Pick's theorem:
    I=6112+1=1I = 6 - \frac{11}{2} + 1 = 1

Thus, there is 1 integer point strictly within the triangle.

Summary Table

The table below summarizes how to calculate each component:

ComponentCalculation MethodExample Data
Area (A)(A)\frac{1}{2} \left\\lvert x_1(y_2-y_3) + x_2(y_3-y_1) + x_3(y_1-y_2) \right \\rvert6
Boundary (B)(B)Sum of gcd(lvertxixi+1rvert,lvertyiyi+1rvert)\+3\gcd(\\lvert x_i - x_{i+1} \\rvert, \\lvert y_i - y_{i+1} \\rvert) \+ 3 (for edges)11
Interior (I)(I)AB2+1A - \frac{B}{2} + 11

The calculations effectively capitalize on geometric properties and integer arithmetic, offering a systematic way to address this classic lattice problem.

Further Considerations

  1. Extensions: While this article has focused on triangles, Pick's Theorem can be extended to other polygons as long as their vertices are lattice points.
  2. Generalization: For triangles with non-integer vertices, advanced lattice enumeration techniques become necessary.
  3. Computational Tools: For any scalable application, leveraging libraries in Python (e.g., SymPy or NumPy) could automate these computations on larger datasets of triangular geometries.

By understanding these basics, geometric computations involving integer lattice points become much more approachable, providing foundation for more complex geometric and topological analyses.


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.