1

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?

veta
  • 716
  • 9
  • 22

1 Answers1

0

I would start by finding the point with fractional co-ordinates (that is, not necessarily integers) which is closest to the given point. If only one co-ordinate is outside the range of the grid then just set this co-ordinate to the nearest in-range co-ordinate. If both co-ordinates are outside the range of the grid, then the closest point will be one of the corners.

Given that starting point you need to find N points with integer co-ordinates. If the closest point was a corner it already has integer co-ordinates. Otherwise the closest point is one of the grid points to either side of the closest point.

You know the other N-1 points will be connected to the closest grid point, because you are working out the intersection of two convex shapes - a circle and a rectangle. I would keep a heap of points ordered by distance. Start it off with the neighbours of the closest grid point. Repeatedly remove the point in the heap nearest to the original point outside the grid and put into the heap its neighbours, if they are not there already, until you have extracted the other N-1 points.

mcdowella
  • 19,301
  • 2
  • 19
  • 25
  • This is really great and exactly what I'll do. Thank you! One question, the exact shape I'm using is actually an octagon inscribed in the array grid. This means I have some null cells in the corners. Is there any trick for this? I was thinking to just assign my initial point to that corner or nearest null cell and then instead of adding it to the heap and decrementing N, I check all the points in the nearby region to get a new spotter point for the algorithm. But if there is a better way I love learning this stuff. http://imgur.com/yIuqi9C – veta Mar 08 '15 at 17:02
  • Ok so this ended up working beautifully. I ended up recursing through all the null cells until I added every cell adjacent to them. Then I did the priority queue stuff to those valid cells. Works like a charm. Bless you mcdowella – veta Mar 08 '15 at 20:07