I'm using 2d arrays for to handle objects in a game. The dimensions of the array act like coordinates on a cartesian grid. When the player selects a point on I want to collect the N nearest grid cells from the array, even if the point is not a valid array index.
Example: An array that goes from [0,0] to [10,10] If the player selects (-1,-1), N=3, the nearest points would be [(0,0),(0,1),(1,0)]
The brute force approach would be to calculate the euclidean distance between the point selected and each array grid cell, put it in a list, and then sort. With very large arrays this might be performance prohibitive. Is there any greedier way to do this, if for example I knew what N was?
I know if the point selected was INSIDE the grid we could use the formula for a circle to get a rough area to check e.g. N/Pi = R^2. We can check the square that this R value creates in the x and y dimensions, which is much faster.
But what about when the point selected is near the edge of the grid. or off it? Or if you want to ignore certain grid cells?