]> Cypherpunks repositories - gostls13.git/commitdiff
image: fix the overlap check in Rectangle.Intersect.
authorNigel Tao <nigeltao@golang.org>
Fri, 10 Feb 2017 03:40:38 +0000 (14:40 +1100)
committerNigel Tao <nigeltao@golang.org>
Fri, 10 Feb 2017 05:05:59 +0000 (05:05 +0000)
This is a re-roll of a previous commit,
a855da29dbd7a80c4d87a421c1f88a8603c020fa, which was rolled back in
14347ee480968c712ea885a4ea62779fd8a0dc44.

It was rolled back because it broke a unit test in image/gif. The
image/gif code was fixed by 9ef65dbe0683634a2e8a557d12267d0309ae1570
"image/gif: fix frame-inside-image bounds checking".

The original commit message:

image: fix the overlap check in Rectangle.Intersect.

The doc comment for Rectangle.Intersect clearly states, "If the two
rectangles do not overlap then the zero rectangle will be returned."
Prior to this fix, calling Intersect on adjacent but non-overlapping
rectangles would return an empty but non-zero rectangle.

The fix essentially changes
if r.Min.X > r.Max.X || r.Min.Y > r.Max.Y { etc }
to
if r.Min.X >= r.Max.X || r.Min.Y >= r.Max.Y { etc }
(note that the > signs have become >= signs), but changing that line to:
if r.Empty() { etc }
seems clearer (and equivalent).

Change-Id: I2e3af1f1686064a573b2e513b39246fe60c03631
Reviewed-on: https://go-review.googlesource.com/36734
Reviewed-by: Rob Pike <r@golang.org>
Run-TryBot: Nigel Tao <nigeltao@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/image/geom.go
src/image/geom_test.go

index e1cd4dc1e3e0980cf7ca152ac13caba83095399a..ed7dde2c84d05cdd00f85fb99d01547aa73af258 100644 (file)
@@ -161,7 +161,11 @@ func (r Rectangle) Intersect(s Rectangle) Rectangle {
        if r.Max.Y > s.Max.Y {
                r.Max.Y = s.Max.Y
        }
-       if r.Min.X > r.Max.X || r.Min.Y > r.Max.Y {
+       // Letting r0 and s0 be the values of r and s at the time that the method
+       // is called, this next line is equivalent to:
+       //
+       // if max(r0.Min.X, s0.Min.X) >= min(r0.Max.X, s0.Max.X) || likewiseForY { etc }
+       if r.Empty() {
                return ZR
        }
        return r
index 6e9c6a13c2cac87b4fdb86597777f163e2a7c1dc..9fede027218c31e54a478cd6a40bd4e34fac577b 100644 (file)
@@ -28,6 +28,7 @@ func TestRectangle(t *testing.T) {
 
        rects := []Rectangle{
                Rect(0, 0, 10, 10),
+               Rect(10, 0, 20, 10),
                Rect(1, 2, 3, 4),
                Rect(4, 6, 10, 10),
                Rect(2, 3, 12, 5),
@@ -62,9 +63,9 @@ func TestRectangle(t *testing.T) {
                        if err := in(a, s); err != nil {
                                t.Errorf("Intersect: r=%s, s=%s, a=%s, a not in s: %v", r, s, a, err)
                        }
-                       if a.Empty() == r.Overlaps(s) {
-                               t.Errorf("Intersect: r=%s, s=%s, a=%s: empty=%t same as overlaps=%t",
-                                       r, s, a, a.Empty(), r.Overlaps(s))
+                       if isZero, overlaps := a == (Rectangle{}), r.Overlaps(s); isZero == overlaps {
+                               t.Errorf("Intersect: r=%s, s=%s, a=%s: isZero=%t same as overlaps=%t",
+                                       r, s, a, isZero, overlaps)
                        }
                        largerThanA := [4]Rectangle{a, a, a, a}
                        largerThanA[0].Min.X--