X-Git-Url: https://git.ucc.asn.au/?p=ipdf%2Fcode.git;a=blobdiff_plain;f=src%2Frect.h;h=78c5a708665200477645b6f6f9df3f954553aca2;hp=25ef94e7a3955c80f393618ab54d71d7fa2ae45f;hb=HEAD;hpb=326f04a375ce3120f7e8957e3d7cd5f296f513e3 diff --git a/src/rect.h b/src/rect.h index 25ef94e..78c5a70 100644 --- a/src/rect.h +++ b/src/rect.h @@ -12,6 +12,7 @@ namespace IPDF T x; T y; T w; T h; //TRect() = default; // Needed so we can fread/fwrite this struct TRect(T _x=0, T _y=0, T _w=1, T _h=1) : x(_x), y(_y), w(_w), h(_h) {} + template TRect(const TRect & cpy) : x(T(cpy.x)), y(T(cpy.y)), w(T(cpy.w)), h(T(cpy.h)) {} std::string Str() const { @@ -37,8 +38,47 @@ namespace IPDF if (y > other.y + other.h) return false; return true; } + + inline bool Contains(const TRect& other) const + { + return PointIn(other.x, other.y) && PointIn(other.x + other.w, other.y + other.h); + } - template TRect Convert() {return TRect(B(x), B(y), B(w), B(h));} + template TRect Convert() const {return TRect(B(x), B(y), B(w), B(h));} + + template TRect & operator=(const TRect & equ) + { + x = T(equ.x); + y = T(equ.y); + w = T(equ.w); + h = T(equ.h); + return *this; + } + + // Clips "other" to "this" + inline TRect Clip(const TRect& other) const + { + TRect clipped = other; + if (clipped.x < x) + { + clipped.w += clipped.x - x; + clipped.x = x; + } + if (clipped.y < y) + { + clipped.h += clipped.y - y; + clipped.y = 0; + } + if (clipped.x + clipped.w > x + w) + { + clipped.w += ((x + w) - (clipped.x + clipped.w)); + } + if (clipped.y + clipped.h > y + h) + { + clipped.h += ((y + h) - (clipped.y + clipped.h)); + } + return clipped; + } };