# Convert infinite 2D plane integer coords to 1D number

Say I have an infinte 2D grid (ex. a procedurally generated world) and I want to get a unique number for each integer coordinate pair. How would I accomplish this?

My idea is to use a square spiral, but I cant find a way to make a formula for the unique number other than an algorythm that just goes in a square spiral and stops at the wanted coords.

The application for this converstion could be for example a way to save an n dimensional shape to a file where each line represents a chunk of the shape (by using $$u(x, y, z) = u(u(x, y), u(y, z))$$ ), or have a very unique random seed for each integer point (ex. a way to hash an integer vector to a data point in an n dimensional array)

• More generally than Ross's answer there are a range of pairing functions, some of which are defined precisely as spirals as you describe. It is a theorem that Ross's answer is the unique quadratic pairing function, modulo exchanging x and y. Nov 19, 2018 at 4:21

You need the Cantor pairing function, tuned up to accept integers instead of naturals. The basic function takes a pair of naturals (including zero) $$x,y$$ and returns a natural $$\pi(x,y)=\frac 12(x+y)(x+y+1)+y$$. It is invertible, so given $$\pi(x,y)$$ you can recover $$x$$ and $$y$$. Now just take your integers to naturals by $$f(z)=\begin {cases} 2z&z\ge 0\\-2z-1& z \lt 0\end {cases}$$ pair them and you have your result.

You can do your idea with the Ulam spiral: The cell numbered $$1$$ is the origin. Note the odd squares follow the downward right diagonal and the even squares start just above $$1$$ and follow an upward left diagonal. If we are given a coordinate $$(x,y)$$ we first find the direction the side is going. If $$x \gt 0, x \gt |y|$$, the cell is on an upward side. The corner at the bottom is $$(x,-x)$$ and has number $$(2x-1)^2$$ The number in our cell is $$y+x$$ cells above, so the number is $$(2x-1)^2+y+x$$. You can go through the other three sides similarly. To go the other direction, given a cell number find the perfect square below it. Find the location of the square on the diagonal, then count the number of squares from there as needed.

Other answers state how to convert integers to naturals, I won't repeat this step. Let's suppose you have two naturals, e.g.:

$$123$$ $$98765$$

$$00123$$ $$98765$$

And "interleave":

$$0908172635$$

Reverting is trivial: you pick digits from either odd or even positions.

Notes:

• the representation depends on the base of the numeral system you use;
• you can expand the method to non-negative reals in a quite obvious way;
• similarly you can create a method that takes any fixed number of numbers and yields one number.
• This is a valid answer, but I cant see this as an effieicient solution, especially in the 1st example use where one would save the data in the file line with index of the unique number. Also as far as I'm concerned numbers dont begin with 0's, so if this would to be stored in a variable, it would have to be a string which is 256 bits per numerical digit where a numerical variable takes ~3.32 bits per numerical digit. I know this is a math forum, but I'm talking about this because my proposed applications are IT ones!
– Peri
Nov 18, 2018 at 19:28
• @Flutterish You need numbers as strings only during conversion. At any other moment you can store numbers as numbers. Nov 18, 2018 at 19:30
• 0908172635, the example you've given is leaded by a zero, which cant be saved as a number. I know I could just cut it off, but then... No, actually yes, you are right, there is no way I will ever end up with 2 leading zeros (except $(0,0)$, but thats not a problem), therefore I will always be able to store it as a number.
– Peri
Nov 18, 2018 at 19:39

There are also tools from number theory. We can first map all integers to non-negative ones, which is easy, just take f(n)=\left\{\begin{align}&2n&n\ge0\\&-2n-1&n<0\end{align}\right. as Ross pointed out. Now us take the pair $$(m,n)$$ to be $$2^{f(m)}3^{f(n)}$$. Since we can uniquely decompose positive integers into prime factors, this function is invertible, and you have your result.

• Unlike Ross's answer, this answer doesn't biject so some wastage occurs insofar as it will only use two thirds of the integers. Nov 19, 2018 at 4:15
• @RobertFrost: In fact, this mapping will skip almost all integers; specifically, its image only includes the powers of 2 and 3 and their products (including 1, as a trivial case), whose asymptomatic density among all integers is zero. For example, the only numbers below 100 this function can return are 1, 2, 3, 4, 6, 8, 9, 12, 16, 18, 24, 27, 32, 36, 48, 54, 64, 72, 81 and 96; they start out pretty dense below 10, but get sparser and sparser quickly. Nov 19, 2018 at 7:29
• @IlmariKaronen the 3 smooth numbers no less, excluding every number having a 5 rough number other than 1 as a factor. Nov 19, 2018 at 7:54

This is an interesting question. I will provide a method to simplify your algorithm, but not necessarily a formula just yet (I'm sure that what I'm about to show you will lead to a formula...probably).

Let's begin with a point in the center. That is, we are not starting at the top corner of a semi-infinite plane, we are instead assuming the plane is infinite. The point in the center is assigned the number 1, and we call it the $$i = 1$$ point. We surround this square with a border of squares. This border has 8 such squares. We repeat the process and get 16 squares. In general, each "border" has $$2(2i-1) + 2(2i-3) = 8i - 8 = 8(i-1)$$ squares.

We now want to form a sum of the first $$N$$ such squares:

$$S_N = \bigg(\sum_{i=2}^{N}8(i-1)\bigg) + 1$$

Now, you want to simplify your life, so let's simplify that sum. We would end up with:

$$S_N = 8\bigg(\sum_{i=2}^{N}(i-1)\bigg) + 1$$

$$S_N = 8\frac{N(N+1)}{2}-1 - (8N - 8) + 1$$ (I'll leave you to simplify) and check my algebra. I'm 99% sure it's accurate.

Now you want to unpack this. This involves several steps. Say you have the number $$M$$. You need to find the largest $$N$$ such that $$S_N \le M \lt S_{N+1}$$. Other than a strict search, I'm afraid I don't know how to do that, sorry.

Once you know the $$N$$, then you need to compute the quantity $$M - S_N$$. This quantity tells you how many squares to "walk" from some starting square on border $$N+1$$ to where you want to be. Since you know where the border squares start, and you know how far you've walked, you know where on the border you are and therefore what the coordinates are.

The method needs some cleanup, but should do it. Good luck doing that in N-D.

If you want to convert $$(a,b)$$ into $$c$$. ($$a$$,$$b$$,$$c$$ are all positive integer) $$c = 2^{a-1} * (2*b-1)$$ This can uniquely map $$(a,b)$$ into $$c$$. It is also invertible. Moreover, $$c$$ is equal to all positive integers, which means there are no waste of rooms(memories) unlike the way, e.x. $$c = 2^{a-1} * 3^{b-1}$$