Skip to content

image: Rectangle.Union() doesn't handle empty rectangles correctly #9895

@robzan8

Description

@robzan8

The function for calculating the union of two rectangles returns wrong values when one or both input rectangles are empty. The code in image/geom.go is currently:

func (r Rectangle) Union(s Rectangle) Rectangle {
    if r.Min.X > s.Min.X {
        r.Min.X = s.Min.X
    }
    if r.Min.Y > s.Min.Y {
        r.Min.Y = s.Min.Y
    }
    if r.Max.X < s.Max.X {
        r.Max.X = s.Max.X
    }
    if r.Max.Y < s.Max.Y {
        r.Max.Y = s.Max.Y
    }
    return r
}

To be correct a couple of lines should be added at the beginning of the function:

    if r.Empty() {
        return s
    }
    if s.Empty() {
        return r
    }

Is this a bug, or is the case ignored for performance reasons? If it's the latter, I think a warning should be added in the function documentation.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions