Jump to content

Getting indices oI am making a 2d grid out of a single array and need f array locations around a given location when array represents a spiraled grid.


1337GameDev

Recommended Posts



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:


ArraySpiralIndexAccess_zpsc2632742.png

Link to comment
Share on other sites

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 by Unity+
Link to comment
Share on other sites

 

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:
ArraySpiralIndexAccess_zpsc2632742.png

 

 

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.

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.