piecemeal icon indicating copy to clipboard operation
piecemeal copied to clipboard

Implement operator== and hashCode for Rect.

Open thomcc opened this issue 11 years ago • 1 comments

I've implemented equality and hashCode operations for Rect. I wasn't sure if it was better to compare based on top/left/bottom/right, or pos/size, but went with pos/size because those are externally visible (It would be pretty crappy for r1 == r2 to be true but r1.x == r2.x not to be).

The downside of this is that now two Rects might contain an identical set of points (err, Vecs), but not compare as equal.

I have tests for the operator== but not for the hashCode because I'm not sure it would make sense to test hashCode.

thomcc avatar Sep 21 '14 21:09 thomcc

I wasn't sure if it was better to compare based on top/left/bottom/right, or pos/size, but went with pos/size because those are externally visible

Normally, I think that would be the right call since those are what Rect actually stores.

The downside of this is that now two Rects might contain an identical set of points (err, Vecs), but not compare as equal.

It will do the right thing if you construct a Rect using Vec, but not if you use Direction (which implements Vec). This is because of an annoying limitation in Dart: you can't switch on a type that implements ==.

Being able to use Directions in a switch is obviously really useful. But it's also quite useful to be able to treat a Direction like unit-distance Vec. So I do this weird workaround where there's a VecBase that has most of the functionality but no == and Vec and Direction extend that. Vec does add == on top of that, but Direction doesn't.

So, now that I think about it, testing the actual corners is probably best in Rect since that handles Direction too.

munificent avatar Sep 27 '14 18:09 munificent