This algorithm has been in my mind for a long time, but I cannot find it described anywhere. It\'s so simple though that I can\'t be the only one who has thought of it. Here\'s
Your description doesn't remind me of an algorithm most likely it's data structure and data type. It reminds me of a kd-tree or a quadtree. A kd-tree or quadtree can help you to solve your problem with nearest-neighbor. However if want a mathematical function for grids you can also take a look at space filling curves especially z order morton curves. This works great for power of 2 and reduce the 2d complexity. The function is f(x,y) = (f(x), f(y)).