]> git.tdb.fi Git - libs/gl.git/blobdiff - source/core/rect.h
Check the flat qualifier from the correct member
[libs/gl.git] / source / core / rect.h
index 333dbc25f7aa0466bd706acc13f279cb15b9c89e..e118b39b8c82d7da4d7b35cb00544a7ad487b71c 100644 (file)
@@ -1,20 +1,57 @@
 #ifndef MSP_GL_RECT_H_
 #define MSP_GL_RECT_H_
 
+#include <algorithm>
+#include <limits>
+
 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<unsigned>::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