It is well-known, that if one wants to layout square grid(aka matrix) of real numbers he can use array
with row-major order. Let's draw neighbourhood of some element i:
...................................
...|i-width-1|i-width|i-width+1|...
...| i-1 | i | i+1 |...
...|i+width-1|i+width|i+width+1|...
...................................
Let us for simplicity assume that i somewhere in the middle of square grid, so no bordering issues.(We can add %(width*height)
and get connected on borders grid). So if one wants to do something with each element in neighbourhood of i'th element one should do:
//function which does something with element at idx
void DoSomethinWithElement(size_t idx);
//left neighbour
DoSomethinWithElement(i-1);
//right neighbour
DoSomethinWithElement(i+1);
//top neighbour
DoSomethinWithElement(i-width);
//bottom neighbour
DoSomethinWithElement(i+width);
I want to generalize this algorithm for any type of regular polygon grid (i.e. triangle, square, pentagonal, hexagonal etc.) Regular means that it constructed only from one type of polygon (i.e only from triangles).
How to generalize for any type of polygon grid: 1. Layout of other grid in array? 2. These N(for square mesh four) statements in a loop?
The problem "How to find all neighbors of a tile in a polygonal grid?" is solved quickly using graphs. But I want to use arrays so I could copy them to graphics card with CUDA.
Examples of grids: