+
+ if (r.width < 0 || r.height < 0) {
+ return boost::optional<Rect<T> > ();
+ }
+
+ return r;
+ }
+
+ void extend (Rect<T> const & other)
+ {
+ T old_x = x;
+ T old_y = y;
+ x = std::min (x, other.x);
+ y = std::min (y, other.y);
+ width = std::max (old_x + width, other.x + other.width) - x;
+ height = std::max (old_y + height, other.y + other.height) - y;
+ }
+
+ Rect<T> extended (T amount) const {
+ Rect<T> c = *this;
+ c.x -= amount;
+ c.y -= amount;
+ c.width += amount * 2;
+ c.height += amount * 2;
+ return c;