techbrew

Better 3D hash

Apr 14th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.58 KB | None | 0 0
  1. // Tested with coords x -100 to 100, y 0 to 255, z -100 to 100
  2.  
  3. // Vec3.hashCode() has a 50:1 collision rate.  (Out of 10327500 coords, had 10127195 collisions)
  4. // Algorithm below has a 0.0018:1 collision rate.  (Out of 10327500 coords, had 18640 collisions)
  5.  
  6. // Constants: Horizontal (x,z) and Vertical (y) ranges of values < 0
  7. final int H = 100;
  8. final int V = 0;
  9.  
  10. // Make coords positive with different prime bases
  11. int xh = x + H + 31;
  12. int yh = y + V + 37;
  13. int zh = z + H + 41;
  14.  
  15. // Not pretty, but best I've found yet
  16. hash =  yh + ((yh * 43) * (xh+(xh * 47)) * zh) + (zh * 53);
Advertisement
Add Comment
Please, Sign In to add comment