X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fcore%2Frect.h;h=e118b39b8c82d7da4d7b35cb00544a7ad487b71c;hp=4f6bd6bd43e266486d30844716475f9555119cef;hb=HEAD;hpb=38712d8ecc57d043a2419ffbaeeb57f7a6586f14 diff --git a/source/core/rect.h b/source/core/rect.h index 4f6bd6bd..e118b39b 100644 --- a/source/core/rect.h +++ b/source/core/rect.h @@ -1,6 +1,9 @@ #ifndef MSP_GL_RECT_H_ #define MSP_GL_RECT_H_ +#include +#include + namespace Msp { namespace GL { @@ -13,8 +16,42 @@ struct Rect Rect() = default; Rect(int l, int b, unsigned w, unsigned h): left(l), bottom(b), width(w), height(h) { } + + static Rect max() { unsigned s = std::numeric_limits::max(); return Rect(0, 0, s, s); } + + bool operator==(const Rect &) const; + bool operator!=(const Rect &r) const { return !operator==(r); } + + Rect intersect(const Rect &) const; }; +inline bool Rect::operator==(const Rect &other) const +{ + return left==other.left && bottom==other.bottom && width==other.width && height==other.height; +} + +inline Rect Rect::intersect(const Rect &other) const +{ + auto intersect_axis = [](int &s1, unsigned &z1, int s2, unsigned z2){ + if(s2>s1) + { + unsigned d = s2-s1; + z1 = (d>z1 ? 0 : std::min(z1-d, z2)); + s1 = s2; + } + else + { + unsigned d = s1-s2; + z1 = (d>z2 ? 0 : std::min(z2-d, z1)); + } + }; + + Rect result = *this; + intersect_axis(result.left, result.width, other.left, other.width); + intersect_axis(result.bottom, result.height, other.bottom, other.height); + return result; +} + } // namespace GL } // namespace Msp