Lets say you have a double array of any data type. You are given an x and y value, that represents a single value in this array, such as map[x,y]. Now, you want to do something to not only that point in the array, but to the points around in it in a circle.
There are several way I have achieved this, but none as quite good enough. The first method was to just loop through every value in the array and check if its distance from the center point was lower than a given value, if so, it was in the circle. However, the scale of the array that this is in is around 500x500, and I’d like to make that even bigger if I can. The issue is that 500x500 is 250,000. Looping through that and applying functions is death to my game, but has to be done somehow.
I have created a better method that runs only through the local points, by basically looping through a small square of width and height same as the diameter of the circle, which is 999x faster than before. Ex:
void Circle(int x, int y, int radius)
{
Vector2 center = new Vector2(x,y);
for (int i = x-radius; i < x+radius; i++)
{
for (int j = y-radius; j < y+radius; j++)
{
if (Vector2.Distance(center, new Vector2(i,j)) <= radius)
{
//Do Stuff
}
}
}
}
This will be done a lot, so even little improvements make a big difference. What I’m looking for is a way that doesn’t loop through a square and compare distances, but instead produces all points in the circle directly. I’m not sure how it would be done, but I think it might be possible, and then would likely be more efficient.