Skip to content

Commit

Permalink
Add operator+ and operator+= to Rect<T> (#875)
Browse files Browse the repository at this point in the history
Co-authored-by: Lars Skiba <[email protected]>
  • Loading branch information
LarsSkiba and LarsSkibaModuleWorks authored Jul 22, 2024
1 parent 7f41c18 commit 6a346b4
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
16 changes: 16 additions & 0 deletions CPP/Clipper2Lib/include/clipper2/clipper.core.h
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,22 @@ namespace Clipper2Lib
top == other.top && bottom == other.bottom;
}

Rect<T>& operator+=(const Rect<T>& 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<T> operator+(const Rect<T>& other) const
{
Rect<T> result = *this;
result += other;
return result;
}

friend std::ostream& operator<<(std::ostream& os, const Rect<T>& rect) {
os << "(" << rect.left << "," << rect.top << "," << rect.right << "," << rect.bottom << ") ";
return os;
Expand Down
62 changes: 62 additions & 0 deletions CPP/Tests/TestRect.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <gtest/gtest.h>
#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);
}
}
}

0 comments on commit 6a346b4

Please sign in to comment.