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.

Featured Replies



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

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+

 

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.

Archived

This topic is now archived and is closed to further replies.

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.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.