Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hash Algorithm for Point3D

 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone know of a good hashCode() algorithm for a 3D point with doubles for X, Y and Z? The equals() method for the class returns true if X, Y and Z are equal for both objects and the compared object passes an instanceof test.

Thanks in advance,
Michael Morris
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, being lazy myself, I'd probably just use the basic algorithm recommended in Effective Java pp. 38-39:

Bloch advocates this sort of thing as generally yielding "reasonably good" hash functions, though usually not state-of-the-art. I'm not sure what might be better in this case, aside from one obvious possible improvement by precalculating the result 17 * 37 and thus skipping one multiplication. And the numbers 17 and 37 may be replaced by many other possibilities, preferably prime.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had done something similar to that. I probably shouldn't fret so much and could probably even use something involving distance to origin and individual coordinates. I will never have a coordinate over about 15000' and the granularity will be about 1/64".
Michael Morris
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic