X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Frect.h;h=602acfc4b8bb5cd6fb691c21861e5a19a54a2248;hb=eb0bdad79aa8a8b32e7d23149cf0a0c8f1dfd380;hp=1feb8ad4fed460d19a9f83439e21bdd0abcc1ea0;hpb=854f2e5bbb7ffb9758b823af87034033033f3cb8;p=dcpomatic.git diff --git a/src/lib/rect.h b/src/lib/rect.h index 1feb8ad4f..602acfc4b 100644 --- a/src/lib/rect.h +++ b/src/lib/rect.h @@ -21,20 +21,22 @@ #define DCPOMATIC_RECT_H #include "position.h" +#include +#include /* Put this inside a namespace as Apple put a Rect in the global namespace */ namespace dcpomatic { - + /** @struct Rect * @brief A rectangle. */ -template +template class Rect { public: - + Rect () : x (0) , y (0) @@ -66,16 +68,24 @@ public: return Position (x, y); } - Rect intersection (Rect const & other) const + boost::optional > intersection (Rect const & other) const { - T const tx = max (x, other.x); - T const ty = max (y, other.y); - - return Rect ( + /* This isn't exactly the paragon of mathematical precision */ + + T const tx = std::max (x, other.x); + T const ty = std::max (y, other.y); + + Rect r ( tx, ty, - min (x + width, other.x + other.width) - tx, - min (y + height, other.y + other.height) - ty + std::min (x + width, other.x + other.width) - tx, + std::min (y + height, other.y + other.height) - ty ); + + if (r.width < 0 || r.height < 0) { + return boost::optional > (); + } + + return r; } void extend (Rect const & other) @@ -86,6 +96,15 @@ public: height = std::max (y + height, other.y + other.height) - y; } + Rect extended (T amount) const { + Rect c = *this; + c.x -= amount; + c.y -= amount; + c.width += amount * 2; + c.height += amount * 2; + return c; + } + bool contains (Position p) const { return (p.x >= x && p.x <= (x + width) && p.y >= y && p.y <= (y + height));