# Getting points inside a circle with a given radius in a 2D array?

Let's say you have a system that removes everything within a square made up of 4 position points. This square is calculated by taking the center point and adding or subtracting the range from this center point seen here:

int xmin = x - range;
int ymin = y - range;
int xmax = x + range;
int ymax = y + range;

for (int i = xmin; i <= xmax; i++) {
for (int j = ymin; j <= ymax; j++) {
bounds[j, i] = 0;
}
}


How would I change these bounds to be in a circular range instead of a square?

• One method is shown here (don't let the isometric context throw you - the tile space method shown there works on perpendicular grids too) Sep 1, 2020 at 20:30

So my solution for this was actually a lot more simple than I thought it would be. Before removing this cell check if this distance between this cell and the circle is less than or equal to the radius of the circle. So before setting the value of the array check the distance:

                    if (Vector2.Distance(new Vector2(j, i), originOfCircle) <= radius)
{
bounds[j, i] = 0;
}



And it works great!

• Note that this computes the distance O(radius^2) times, instead of O(radius) times as in the version I linked, and you have some extra branching and iterating over cells you ultimately don't use. But for radii less than thousands this probably isn't significant. Sep 2, 2020 at 0:53
• Just what i needed, thanks Apr 25, 2021 at 11:39