views:

96

answers:

1

I have myself a linear grid of Vector2s stored in a Vector2[,] array, and i also have another Vector2 that lies within this grid. How can i simply extract both the nearest 4 grid points and their indexes in the array? I'm totally stumped...

+2  A: 

I'm not sure if I'm understanding your question. Can you handle it in a relatively straightforward way?

  1. Declare a type to hold the following information:
    a. Integer index into your existing Vector2 array
    b. Distance that the point is from the test vector
    c. (optional) The Vector2 value.
  2. Declare an array of the newly defined type to contain the results.
  3. Loop through the existing array of Vector2s.
  4. For each Vector2, calculate its distance from the test vector.
  5. Compare that distance to the last result Vector2. If it is less than that distance, replace that result vector with the current Vector2's information.
  6. While the distance of the last result vector is less than the previous one, swap it with the previous result. (Repeat with the same vector, now in the second-to-last position until the result vectors are sorted in order of distance from the test vector.)
  7. Proceed with the next iteration of the loop started in step 3.
BlueMonkMN
That sounds decent, though i'm in a position where performance is required. I'll work out an initial approach based on this though, thanks! (would upvote but i ran out for the day)
RCIX
I guess I didn't understand your point that you have the vector2s stored in a 2D array. Does that mean you can immediately determine if there are any Vector2s within a particular area of the grid? If so, it may help to note that you could begin your search at the grid cell where your test vector would be placed, spiraling outwards in the grid until the cell you're looking at couldn't possibly have any vectors closer than what you've found.
BlueMonkMN