David's final changes: more profiler features, fixes.
[ipdf/code.git] / src / rect.h
index 25ef94e..78c5a70 100644 (file)
@@ -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 <class B> TRect(const TRect<B> & 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 <class B> TRect<B> Convert() {return TRect<B>(B(x), B(y), B(w), B(h));}
+               template <class B> TRect<B> Convert() const {return TRect<B>(B(x), B(y), B(w), B(h));}
+               
+               template <class B> TRect<T> & operator=(const TRect<B> & 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;
+               }
        };
 
 

UCC git Repository :: git.ucc.asn.au