Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find all integer coordinates in a given radius

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.

like image 930
danijar Avatar asked Jan 11 '13 19:01

danijar


1 Answers

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));
}
like image 184
Eric Avatar answered Sep 23 '22 01:09

Eric