Find all integer coordinates in a given radius
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
To find all integer grid points inside a circle, you do not need a complicated geometry algorithm. The standard approach is to scan the bounding square and keep only the points whose squared distance from the center is less than or equal to the squared radius.
This works for graphics, grid simulations, search areas in games, and any problem where coordinates must stay on integer lattice points.
Use Squared Distance Instead of sqrt
For a center point (cx, cy) and radius r, an integer point (x, y) is inside or on the circle if:
(x - cx)^2 + (y - cy)^2 <= r^2
The square root is unnecessary. Comparing squared values is faster and avoids floating-point noise from repeated sqrt calls.
If the center is an integer point and the radius is an integer, the search range is:
- '
xfromcx - rtocx + r' - '
yfromcy - rtocy + r'
That square contains every possible lattice point that could lie in the circle.
A Simple Python Implementation
This is the clearest approach for most practical uses.
Time Complexity and a Small Optimization
The algorithm checks every point in the bounding square, so the work is proportional to (2r + 1)^2, which is O(r^2).
That is usually fine because the number of returned points is also proportional to the area of the circle. In other words, if you truly need every integer point, you cannot avoid work that grows with the area.
A common optimization is to iterate over x and compute the maximum allowed y offset for that x. That avoids checking corner points that are obviously outside the circle:
This still has output-size complexity, but it skips unnecessary distance checks.
Non-Integer Centers
If the center is not an integer, the same idea still works. You just compute the integer bounds with floor and ceiling and test the squared distance against the real-valued center.
The only change is that dx and dy become floating-point values:
- '
dx = x - cx' - '
dy = y - cy'
The inclusion test remains the same.
Common Pitfalls
- Using
sqrtin every iteration when a squared-distance comparison is enough. - Forgetting to include boundary points that lie exactly on the circle.
- Iterating over an area larger than the bounding square, which wastes time.
- Assuming a faster asymptotic algorithm exists even when you need to output every matching point.
Summary
- Scan the bounding square and keep points whose squared distance is at most
r^2. - Comparing squared distances avoids unnecessary square roots.
- The straightforward algorithm runs in
O(r^2), which is appropriate when you need every point. - A per-column optimization can reduce wasted checks while preserving correctness.
- The same method works for integer or non-integer centers.

