# Finding a tile number in a grid system

Hi I am trying to find the Tile number of a grid of tiles based on the Z and X coordinates.

Where the Z and X axis are potentially infinite in either direction how can i get the number of the tile, based on the Z and X coordinates.

Sorry for the crude drawing

The only way I know of, or at least can think of right now, is the following:

Prerequisites:

1. You need to know the width and height of your grid
2. the tile numbers must increase successively, so like you have in your drawing atm

If you got those 2 things, then you would use the formula

tile number = height * "index in width direction" + "index in height direction"


Now I have defined this very very general in sorts, since it is very dependant on the orientation of your grid and in which direction the rid grows. So to be more specific here a small, very artistic paint illustration of the situation you have above :)

The blue numbers are the indices in either direction. In your specific case, with z going up and x to the right, where z fills up first, the idea would: be

• use the x index to see "how many columns are filled" (since when you are at x_index = 1, that means x_index = 0 is filled with "height" amount of cells)
• Add to that the z:index, since that is the amount of cells in the partially filled column not counted yet

Don't forget though to make sure you are using the correct values, since as mentioned before these grids can be different and thus examples may not always apply to your situation directly.

Not sure if this will answer your question to your satisfaction, since I do not know if the prerequisites apply in your case.

Using this method you can for example also treat a "normal" one dimensional array as a 2 dimensional one, since you can always calculate which cell in the one dimensional array you need to access via the x and y indices (useful for i.e. graphics programming in some areas)

• Thank you very much for a detailed answer, this is perfect! – Josh Kirkpatrick Aug 23 '16 at 15:22

Throzen's answer may have answered Josh's needs, but it doesn't answer the question as it stands (infinite grid, no grid height nor width available) so I'll leave this answer here if anyone is interested.

One can try a diagonal or a snail tiling. Which one you should use depends on what is meant by:

infinite in either direction

## 1) If you mean X axis is [0, +Inf) and Y-axis is also [0, +Inf)

Then use a diagonal tiling, like so:

9
5 8
2 4 7
0 1 3 6 ...


I'll leave it up to you to figure out how to make a pair (i, j) into a tile number, but it is rather simple. The reverse function is also possible, but (slightly) more complex.

## 2) If you mean X axis is (-Inf, +Inf) and Y-axis is also (-Inf, +Inf)

Then you can use a "snail" tiling like so:

      .
4 3 2 .
5 0 1 .
6 7 8 9


Again, there is a way to make a pair (i, j) into a tile number. The reverse function is also possible.

These answers let you number a tile at arbitrary position, without prior knowledge of the grid size. This comes with the drawback of an increased (though manageable) computing cost.

• Another approach you could try is Morton ordering, in which you form the tile index by interleaving the bits of its coordinates. It generally has good locality properties, and is straightforward to compute both forward and backward. – DMGregory Aug 24 '16 at 13:48