Given a two-dimensional coordinate system how can I find all points with integer coordinates in a radius from a given point? I want the points as x-coordinate and y-coordina
The following code just parses the boundary along a quarter circle to determine the inner area. It does not need to compute the distance for the outer points, nor for the inner points. (edit: but finally, all points of the filled circle are added)
In some mini-Java-benchmarks, for small radius (<10), it is of the same speed as the simple approach with parsing the full square. For radius 20-40 it is about 2 times faster, and it achieves a speedup of about 4 times for radius > 50. For some much larger radius (>200) the speedup decreases again, since for any approach the dominating time is then needed for creating and adding the >100k points - regardless of how they are determined.
// add the full length vertical center line once
for (int y = -radius + point.y; y <= radius + point.y; ++y)
points.insert(Point(point.x, y));
int sqRadius = radius * radius;
// add the shorter vertical lines to the left and to the right
int h = radius;
for (int dx = 1; dx <= radius; ++dx) {
// decrease h
while (dx*dx + h*h > sqRadius && h > 0)
h--;
for (int y = -h + point.y; y <= h + point.y; ++y) {
points.insert(Point(point.x + dx, y));
points.insert(Point(point.x - dx, y));
}
}