Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Good hashcode function for 2D coordinates

I would like to use a HashMap to map (x, y) coordinates to values. What is a good hashCode() function definition? In this case, I am only storing integer coordinates of the form (x, y) where y - x = 0, 1, ..., M - 1 for some parameter M.

like image 476
I Like to Code Avatar asked Apr 03 '14 01:04

I Like to Code


1 Answers

To get unique Value from two numbers, you can use bijective algorithm described in here < x; y >= x + (y + ( (( x +1 ) /2) * (( x +1 ) /2) ) )

This will give you unquie value , which can be used for hashcode

public int hashCode()
{
      int tmp = ( y +  ((x+1)/2));
               return x +  ( tmp * tmp);
}
like image 125
Mani Avatar answered Sep 29 '22 18:09

Mani