aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/utils/rectangle.hpp23
-rw-r--r--test/utils/rectangle.cpp26
2 files changed, 49 insertions, 0 deletions
diff --git a/src/utils/rectangle.hpp b/src/utils/rectangle.hpp
index fc5d8ac3..e2fddc78 100644
--- a/src/utils/rectangle.hpp
+++ b/src/utils/rectangle.hpp
@@ -104,6 +104,29 @@ namespace usdx
{
return point2.get_y() - point1.get_y();
}
+
+ const Rectangle<T> intersect(const Rectangle<T>& inner) const
+ {
+ Rectangle<T> result(*this);
+
+ if (inner.point1.get_x() > result.point1.get_x()) {
+ result.point1.set_x(inner.point1.get_x());
+ }
+
+ if (inner.point1.get_y() > result.point1.get_y()) {
+ result.point1.set_y(inner.point1.get_y());
+ }
+
+ if (inner.point2.get_x() < result.point2.get_x()) {
+ result.point2.set_x(inner.point2.get_x());
+ }
+
+ if (inner.point2.get_y() < result.point2.get_y()) {
+ result.point2.set_y(inner.point2.get_y());
+ }
+
+ return result;
+ }
};
};
diff --git a/test/utils/rectangle.cpp b/test/utils/rectangle.cpp
index f2fe93ea..0cfbece2 100644
--- a/test/utils/rectangle.cpp
+++ b/test/utils/rectangle.cpp
@@ -35,6 +35,8 @@ namespace usdx
class RectangleTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(RectangleTest);
CPPUNIT_TEST(testInit);
+ CPPUNIT_TEST(testIntersectInner);
+ CPPUNIT_TEST(testIntersectOuter);
CPPUNIT_TEST_SUITE_END();
private:
static log4cpp::Category& log;
@@ -57,6 +59,30 @@ namespace usdx
CPPUNIT_ASSERT(rect.get_width() == 200);
CPPUNIT_ASSERT(rect.get_height() == 250);
}
+
+ void testIntersectInner()
+ {
+ Rectangle<int> outer(100, 150, 200, 250);
+ Rectangle<int> inner(120, 170, 180, 200);
+ Rectangle<int> intersect = outer.intersect(inner);
+
+ CPPUNIT_ASSERT(intersect.get_point1().get_x() == 120);
+ CPPUNIT_ASSERT(intersect.get_point1().get_y() == 170);
+ CPPUNIT_ASSERT(intersect.get_point2().get_x() == 180);
+ CPPUNIT_ASSERT(intersect.get_point2().get_y() == 200);
+ }
+
+ void testIntersectOuter()
+ {
+ Rectangle<int> outer(120, 170, 180, 200);
+ Rectangle<int> inner(100, 150, 200, 250);
+ Rectangle<int> intersect = outer.intersect(inner);
+
+ CPPUNIT_ASSERT(intersect.get_point1().get_x() == 120);
+ CPPUNIT_ASSERT(intersect.get_point1().get_y() == 170);
+ CPPUNIT_ASSERT(intersect.get_point2().get_x() == 180);
+ CPPUNIT_ASSERT(intersect.get_point2().get_y() == 200);
+ }
};
log4cpp::Category& RectangleTest::log =