aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlexander Sulfrian <alexander@sulfrian.net>2012-03-21 21:36:59 +0100
committerAlexander Sulfrian <alexander@sulfrian.net>2013-01-05 17:17:51 +0100
commitb489968dcc1416a530b9f5289b47962e39ec4158 (patch)
treeeb299b599bd0f7ae98661d61eb33d971250d7b67
parente762b9d1a0080bfbba36d1b53ada858d05e13db1 (diff)
downloadusdx-b489968dcc1416a530b9f5289b47962e39ec4158.tar.gz
usdx-b489968dcc1416a530b9f5289b47962e39ec4158.tar.xz
usdx-b489968dcc1416a530b9f5289b47962e39ec4158.zip
utils/rectangle: added function to intersect two rectangles
-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 =