wood burning stoves 2.0*
The moose likes Java in General and the fly likes Hash Algorithm for Point3D Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Hash Algorithm for Point3D" Watch "Hash Algorithm for Point3D" New topic
Author

Hash Algorithm for Point3D

Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
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


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hash Algorithm for Point3D