An IEqualityComparer for a Point3d

_config.yml

Lady Justice: who’s blind and weighs objects impartially. Isn’t she beautiful?

But a problem I faced was: how to implement an IEqualityComparer for a Point3d when a tolerance is involved. I didn’t have to think at all, before this answer was proffered by Gilles. The problem with hashcodes are that they more or less return different values. But what happens when you want to return the same hashcode for similar objects, yet different ones for different objects. How do you handle that? See below:

The MSDN documentation suggests that we should not implement the IEqualityComparer but should rather implement the EqualityComparer which is an abstract class, as opposed to an interface. Hence, the code below is not necessary accurate.

The beauty of it lies in the fact that similar points return similar hash codes. Which is what you want.

Enjoy!

Written on December 15, 2016