aboutsummaryrefslogtreecommitdiff
path: root/src/common.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/common.hpp')
-rw-r--r--src/common.hpp32
1 files changed, 30 insertions, 2 deletions
diff --git a/src/common.hpp b/src/common.hpp
index e745ece..d35d1bc 100644
--- a/src/common.hpp
+++ b/src/common.hpp
@@ -13,6 +13,26 @@ struct Pt {
Pt operator-(Pt o) const { return { x - o.x, y - o.y }; }
Pt operator*(int k) const { return { x * k, y * k }; }
Pt operator/(int k) const { return { x / k, y / k }; }
+ Pt& operator+=(Pt o) {
+ x += o.x;
+ y += o.y;
+ return *this;
+ }
+ Pt& operator-=(Pt o) {
+ x -= o.x;
+ y -= o.y;
+ return *this;
+ }
+ Pt& operator*=(Pt o) {
+ x *= o.x;
+ y *= o.y;
+ return *this;
+ }
+ Pt& operator/=(Pt o) {
+ x /= o.x;
+ y /= o.y;
+ return *this;
+ }
bool operator==(const Pt&) const = default;
};
@@ -25,8 +45,6 @@ inline Pt pt_min(Pt a, Pt b) {
inline Pt pt_max(Pt a, Pt b) {
return Pt{ MMAX(a.x, b.x), MMAX(a.y, b.y) };
}
-#undef MMIN
-#undef MMAX
struct Rect {
Pt bl, tr;
@@ -46,3 +64,13 @@ inline Rect rect_union(Rect a, Rect b) {
inline Rect rect_union(Rect a, Pt b) {
return Rect(pt_min(a.bl, b), pt_max(a.tr, b));
}
+inline Rect rect_intersect(Rect a, Rect b) {
+ // https://stackoverflow.com/a/19754915
+ int x0 = MMAX(a.bl.x, b.bl.x);
+ int y0 = MMAX(a.bl.y, b.bl.y);
+ int x1 = MMIN(a.tr.x, b.tr.x);
+ int y1 = MMIN(a.tr.y, b.tr.y);
+ return Rect(x0, y0, x1, y1);
+}
+#undef MMIN
+#undef MMAX