X-Git-Url: https://main.carlh.net/gitweb/?p=dcpomatic.git;a=blobdiff_plain;f=src%2Flib%2Frect.h;h=97b90abc5e28546ab74d6c0fa1115460289a0dbb;hp=6f4709c088ce137b2c589df64cb69304c1b62373;hb=aeb835a18c8df347e0ed68fb24631b320abeb611;hpb=a2cca95b459a93906c39ca8bd4b31b995108f6ca diff --git a/src/lib/rect.h b/src/lib/rect.h index 6f4709c08..97b90abc5 100644 --- a/src/lib/rect.h +++ b/src/lib/rect.h @@ -21,20 +21,21 @@ #define DCPOMATIC_RECT_H #include "position.h" +#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) @@ -42,6 +43,13 @@ public: , height (0) {} + Rect (Position p, T w_, T h_) + : x (p.x) + , y (p.y) + , width (w_) + , height (h_) + {} + Rect (T x_, T y_, T w_, T h_) : x (x_) , y (y_) @@ -54,14 +62,16 @@ public: T width; T height; - Position position () const { + Position position () const + { return Position (x, y); } - Rect intersection (Rect const & other) const { + Rect intersection (Rect const & other) const + { T const tx = max (x, other.x); T const ty = max (y, other.y); - + return Rect ( tx, ty, min (x + width, other.x + other.width) - tx, @@ -69,7 +79,16 @@ public: ); } - bool contains (Position p) const { + void extend (Rect const & other) + { + x = std::min (x, other.x); + y = std::min (y, other.y); + width = std::max (x + width, other.x + other.width) - x; + height = std::max (y + height, other.y + other.height) - y; + } + + bool contains (Position p) const + { return (p.x >= x && p.x <= (x + width) && p.y >= y && p.y <= (y + height)); } };