Convert infinite 2D plane integer coords to 1D number

Solution 1:

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: enter image description here 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.

Solution 2:

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 $$

Add leading zeros to obtain equal number of digits:

$$ 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.

Solution 3:

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.

Solution 4:

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.