0

I want to assign every a Integer a unique pair of Integers. My first thought was to use a spiral, or to put it visually:

25 10 11 12 13     (-2, 2) (-1, 2) ( 0, 2) ( 1, 2) ( 2, 2)
24 9  2  3  14     (-2, 1) (-1, 1) ( 0, 1) ( 1, 1) ( 2, 1)
23 8  1  4  15 --> (-2, 0) (-1, 0) ( 0, 0) ( 1, 0) ( 2, 0)
22 7  6  5  16     (-2,-1) (-1,-1) ( 0,-1) ( 1,-1) ( 2,-1)
21 20 19 18 17     (-2,-2) (-1,-2) ( 0,-2) ( 1,-2) ( 2,-2)

or

      X   Y
------------
|1 |( 0, 0)
------------
|2 |( 0, 1)
------------
|3 |( 1, 1)
------------
|4 |( 1, 0)
------------
|5 |( 1,-1)
------------
|6 |( 0,-1)
------------
|7 |(-1,-1)
------------
|8 |(-1, 0)
------------
|9 |(-1, 1)
------------
|10|(-1, 2)
------------
|11|( 0, 2)
------------
|12|( 1, 2)
------------

but I am not sure how to implement an efficient function that takes a pair and return it's corresponding value, or even if this is the most efficient pattern to use. I heard that Hilbert curves are used for mapping n dimensional things to lines but I am not sure if that is applicable.

Ace shinigami
  • 866
  • 1
  • 9
  • 20
  • Take a look at [this post](http://stackoverflow.com/questions/919612/mapping-two-integers-to-one-in-a-unique-and-deterministic-way?rq=1), this might give you some direction. – Quima Oct 26 '16 at 17:06
  • The best approach depends on what you want to do with it. The Cantor Pairing function as linked by @AugustoQ is definitely a good option. Hilbert curves or other space-filling curves (most notably the Z-curve due to its computational simplicity) are other good options. If you just need something very simple, just concatenate the bit representations of both numbers. – Nico Schertler Oct 26 '16 at 18:52

0 Answers0