Addressing/traversing an infinite 2D grid using a Z-line?

74 Views Asked by At

I'm looking for a method to map an infinite 2D grid using a line, so that I would have just one integer from which I would compute the X and Y. I know something like that exists, but can't recall the name. But I suspect what I have seen was just for a quadrant of a surface.

The key property is that neigboring areas should also have integer of "near" values", so it's going to be fractal-ish. And some "spiral" is not appropriate.

Anyone knows what is it called? And where can I find some info, like formulas to convert between the two ways to refer to a grid cell?

Sorry for non-math terminology :)

Update:

The application of this is following: There's a grid-based online massive-multiplayer game, which should span infinitely. Starts at (0,0) and surroundings. It will be database-backed. For performance reasons, the key for a cell should be just one.

The need for having nearby cells also mapped to near indexes is also due to performance - looking up adjacent indexes in the DB/memory is much faster than skipping across.

(The performance is important because could get under heavy load.)