1337GameDev Posted September 15, 2013 Posted September 15, 2013 I am making a 2d grid out of a single array and need to access locations around a given index. The array represents a 2d grid, where the array spirals out from an origin location (index 0). The reason for the spiral is to allow my 2d grid to grow large in all directions. How do I calculate the indices of the locations to the right, left, above and below the given index location? Here is an image to help:
Unity+ Posted September 15, 2013 Posted September 15, 2013 (edited) Well, here is something that will help in solving this. Let is take the element and represent it as variable x. Now, one part of the solution would be the following. The following parameters would be the case for finding the elements left, right, up, and down of the current element. If the distance of the element from the origin 0 element is 2 elements, then the following would be the case. Either [math]2x+8n[/math] or [math]2x-8n[/math] And [math]x+1[/math] or [math]x-1[/math]. Each case will depend on whether you are on a horizontal stem of the spiral or the vertical one. I will make the solution more clear if you want. it also depends on whether the element is the corner of a spiral leg or not. I noticed there were some exceptions. I will fix that soon. NOTE: Sato inspired this solution. Edited September 15, 2013 by Unity+
Acme Posted September 21, 2013 Posted September 21, 2013 I am making a 2d grid out of a single array and need to access locations around a given index. The array represents a 2d grid, where the array spirals out from an origin location (index 0). The reason for the spiral is to allow my 2d grid to grow large in all directions. How do I calculate the indices of the locations to the right, left, above and below the given index location? Here is an image to help: You represent a modified/generalized version of the Prime specific Ulam Spiral. When the origin is 1 rather than 0, cells can be indexed using specific forms of the generalized equation 4n2 +bn + c.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now