# Is there a cantor pairing function for spirals?

Similarly to how cantor pairing function works by pairing two numbers >=0 to 1 unique number >=0, and where the ordering goes zig zag like dovetailing, is there a way to map numbers including negative ones to unique numbers but for a spiral pattern?

In this image, the center is centered on (0,0). Given a square of odd length, i.e. 1, 3, 5, 7..., does there exist a function that can map the coordinates to the ones indicated in red font? Like (0,0) -> 0, (1, -2)->11?

Does anyone know?

Thanks

• Are you asking for an explicit function that describes the mapping?
– MT_
Jul 15, 2016 at 17:53
• I would prefer an explicit formula if it exists. Jul 15, 2016 at 17:55

Given two integers $x$ and $y,$ here's a formula for the position $f(x,y)$ of the pair $(x,y)$ in the spiral sequence.
First let $s$ be whichever of $x$ and $y$ has greater absolute value. (If $x$ and $y$ have the same absolute value, just set $s=x.$) Then
$$f(x,y)=\begin{cases} 4s^2-x+y, &\text{if }s \ge 0,\\ 4s^2+(-1)^{\delta_{s,x}}(2s+x+y), &\text{if }s < 0. \end{cases}$$
Here $\delta_{s,x}$ is the Kronecker delta, which is $1$ if $s=x,$ and $0$ otherwise.