0
\$\begingroup\$

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?

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

1 Answer 1

1
\$\begingroup\$

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!

\$\endgroup\$
2
  • \$\begingroup\$ 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. \$\endgroup\$
    – DMGregory
    Sep 2, 2020 at 0:53
  • \$\begingroup\$ Just what i needed, thanks \$\endgroup\$ Apr 25, 2021 at 11:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .