X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fcore%2Frect.h;h=e118b39b8c82d7da4d7b35cb00544a7ad487b71c;hp=333dbc25f7aa0466bd706acc13f279cb15b9c89e;hb=HEAD;hpb=ce3658993ce2f6b7527a04a36a5e1af349c6f2e9 diff --git a/source/core/rect.h b/source/core/rect.h index 333dbc25..e118b39b 100644 --- a/source/core/rect.h +++ b/source/core/rect.h @@ -1,20 +1,57 @@ #ifndef MSP_GL_RECT_H_ #define MSP_GL_RECT_H_ +#include +#include + namespace Msp { namespace GL { struct Rect { - int left; - int bottom; - unsigned width; - unsigned height; + int left = 0; + int bottom = 0; + unsigned width = 0; + unsigned height = 0; - Rect(): left(0), bottom(0), width(0), height(0) { } + 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