#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