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-coordinate value.
Finding points in a square around the given point is easy and could be done like that:
for(int x = -radius + point.x; x < radius + point.x; ++x)
for(int y = -radius + point.y; y < radius + point.y; ++y)
{
points.insert(point(x, y));
}
But how can I find the points in a circle around the given point? This algorithm is performance related but not accuracy related. So it doesn't matter if a point closes to the radius than 1 is added or not. In other words, I do not need floating point accuracy.
Simplest solution: take a square and filter it:
Point point(100, 100);
for(int x = -radius; x <= radius; ++x)
for(int y = -radius; y <= radius; ++y)
if(x*x + y*y <= radius* radius) {
points.insert(Point(x + point.x, y + point.y));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With