# Bug: System.Drawing.Rectangle.Union vs empty rectangles

From: Bjoern Hoehrmann <derhoermi@gmx.net>
Date: Fri, 06 Jan 2012 13:15:50 +0100

Message-ID: <qindg7d6v1lktql1s9rsg8iquim0mcj80l@hive.bjoern.hoehrmann.de>
```Hello,

http://msdn.microsoft.com/library/system.drawing.rectangle.union says
"When one of the two rectangles is empty, meaning all of its values are
zero, the Union method returns a rectangle with a starting point of (0,
0), and the height and width of the non-empty rectangle. For example, if
you have two rectangles: A = (0, 0; 0, 0) and B = (1, 1; 2, 2), then the
union of A and B is (0, 0; 2, 2)."

The notation here is confusing, the Rectangle class is a point, width,
hight class (see the constructors and the .ToString method), I actually
did not notice the semi-colon and interpreted this as X, Y, Width, and
Height, but the intent is probably that this is X1, Y1, X2, Y2.

An empty rectangle also isn't one where "all of its values are zero", an
empty rectangle is one of zero width and height, you can place the empty
rectangle anywhere you want, so this does not actually specify what the
method does for (all) empty rectangles, and even for the special case it
does not actually use "the height and width of the non-empty rectangle",

Rectangle.Union(new Rectangle(0, 0, 0, 0), new Rectangle(1, 1, 2, 2))

has a non-empty rectangle with width and height of 2 but the union will
have width and height of three; the union really is the smallest rect-
angle that includes the non-empty rectangle and the empty rectangle's
location. Oh, as an aside, for `Rectangle.Empty` it is said "Represents
a Rectangle structure with its properties left uninitialized." That's
not really possible, it's rather an empty rectangle at 0,0.

(All unfortunately so as this means you can't represent an empty rect-
angle, which you might need for instance as a seed value when you fold
a list of Rectangles using IEnumerable.Aggregate to find the bounding
box that encloses all rectangles since the result would always include
the point 0,0 even if none of the rectangles covers the point, which is
why I looked up the behavior).

regards,
--
Björn Höhrmann · mailto:bjoern@hoehrmann.de · http://bjoern.hoehrmann.de
Am Badedeich 7 · Telefon: +49(0)160/4415681 · http://www.bjoernsworld.de
25899 Dagebüll · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
```
Received on Friday, 6 January 2012 12:22:53 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Wednesday, 7 November 2012 14:18:44 GMT