# Arrays for a heightmap tile-based map

I'm making a game using in C# and Javascript that uses a map which has tiles, corners and borders. Here's a graphical representation:

I've managed to store tiles and corners in memory but I'm having trouble getting the borders structured.

For the tiles, I have a Map Width * Map Height sized array. For corners I have (Map Width + 1) * (Map Height + 1) sized array.

I've already made up the math needed to access corners from a tile, but I can't figure out how to store and access the borders from a single array.

Tiles store their type (and other game logic variables) and via the array index I can get the X and Y coordinates. From this tile position it is possible to get the array index of the corners (which store the Z index).
The borders will store a game object and accessing corners from only border info would be also required.

If someone even has a better way to store these for better memory and performance I would gladly accept that.

## 2 Answers

Look closely at your own illustration. What you've drawn here is a single two-dimensional array. Make an array of size [MapWidth*2+1, MapHeight*2+1] and store everything there. Elements where both coordinates are even will hold corners, where both coordinates are odd will hold tiles, and the rest will hold borders. You'd have to have a common base class for them, but I don't think that's a problem.

Of course, this is a little unintuitive, but adding a wrapper object around this array will solve it.

The borders will store a game object and accessing corners from only border info would be also required.

Keep a pointer/a reference to the corners in the border object? Do the same for tiles and borders?

Maintaining such a linear array of borders is definitely doable - you just need to separate vertical and horizontal borders. Then you can take all the borders of one direction and order them according to their x,y coordinates (it doesn't matter which axis is considered first), then simple integer algebra can be used to find the borders corresponding to a tile at coordinates (x,y). But still, I think you're on the wrong track. Consider using direct links between corresponding objects, as I noted above.