Representing 2d and 3d data in 1 dimension.

When programming applications that operate on some spacial dimension we often work with maps. Specifically, if someone is writing an adventure game they may use a 2d array to store their world data. The programmer would then access elements on the map like so

var map = new Map2DWithDataAlreadyInIt();
var x = 3;
var y = 5;

console.log(map[x][y].someDataValue);

In C that kind of array access is syntactic sugar for

int ndx = map.width*y + x;

MapElement* el = map[ndx];

which shows us that storing two dimensional data in a one dimensional data structure is as simple as specializing the element access mechanism.

Similarly we can store three dimensional data in a one dimensional data structure using this formula for element access

int ndx = map.width*map.height*z + map.width*y + x;

MapElement* el = map[ndx];

You can see that the element access equation for two dimensions is contained inside the equation for three dimensions, which means our equation is recursive, fun huh? So you can guess that next we could use this pattern to store four dimensional data in one dimension or eleven dimensional data in one dimension – just by using a single array and a special access equation.

So – tell me what the access equation would look like for four dimensions…

and…

give me a generalized equation of element access for n-dimensional data.

Leave a Reply


Follow me on GitHub
Follow me on Google+
Follow me on Twitter
Thrilling.
EFNX is proudly powered by WordPress
Entries (RSS) and Comments (RSS).