diff --git a/CPP/Clipper2Lib/include/clipper2/clipper.core.h b/CPP/Clipper2Lib/include/clipper2/clipper.core.h index a157d6e7..d3ad715b 100644 --- a/CPP/Clipper2Lib/include/clipper2/clipper.core.h +++ b/CPP/Clipper2Lib/include/clipper2/clipper.core.h @@ -362,6 +362,22 @@ namespace Clipper2Lib top == other.top && bottom == other.bottom; } + Rect& operator+=(const Rect& other) + { + left = (std::min)(left, other.left); + top = (std::min)(top, other.top); + right = (std::max)(right, other.right); + bottom = (std::max)(bottom, other.bottom); + return *this; + } + + Rect operator+(const Rect& other) const + { + Rect result = *this; + result += other; + return result; + } + friend std::ostream& operator<<(std::ostream& os, const Rect& rect) { os << "(" << rect.left << "," << rect.top << "," << rect.right << "," << rect.bottom << ") "; return os; diff --git a/CPP/Tests/TestRect.cpp b/CPP/Tests/TestRect.cpp new file mode 100644 index 00000000..4eefc5fd --- /dev/null +++ b/CPP/Tests/TestRect.cpp @@ -0,0 +1,62 @@ +#include +#include "clipper2/clipper.core.h" + +using namespace Clipper2Lib; + +TEST(Clipper2Tests, TestRectOpPlus) +{ + { + Rect64 lhs = Rect64::InvalidRect(); + Rect64 rhs(-1, -1, 10, 10); + { + Rect64 sum = lhs + rhs; + EXPECT_EQ(rhs, sum); + } + { + std::swap(lhs, rhs); + Rect64 sum = lhs + rhs; + EXPECT_EQ(lhs, sum); + } + } + { + Rect64 lhs = Rect64::InvalidRect(); + Rect64 rhs(1, 1, 10, 10); + { + Rect64 sum = lhs + rhs; + EXPECT_EQ(rhs, sum); + } + { + std::swap(lhs, rhs); + Rect64 sum = lhs + rhs; + EXPECT_EQ(lhs, sum); + } + } + { + Rect64 lhs(0, 0, 1, 1); + Rect64 rhs(-1, -1, 0, 0); + Rect64 expected(-1, -1, 1, 1); + { + Rect64 sum = lhs + rhs; + EXPECT_EQ(expected, sum); + } + { + std::swap(lhs, rhs); + Rect64 sum = lhs + rhs; + EXPECT_EQ(expected, sum); + } + } + { + Rect64 lhs(-10, -10, -1, -1); + Rect64 rhs(1, 1, 10, 10); + Rect64 expected(-10, -10, 10, 10); + { + Rect64 sum = lhs + rhs; + EXPECT_EQ(expected, sum); + } + { + std::swap(lhs, rhs); + Rect64 sum = lhs + rhs; + EXPECT_EQ(expected, sum); + } + } +} \ No newline at end of file