This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Hash Algorithm for Point3D
 
Similar Threads
inc / dec Operations precedence
Iterative Fibonacci Method Problem
Boolean Values
Alphabet Pyramid (this one!)
Some beautiful Quotes