From b489968dcc1416a530b9f5289b47962e39ec4158 Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Wed, 21 Mar 2012 21:36:59 +0100 Subject: utils/rectangle: added function to intersect two rectangles --- src/utils/rectangle.hpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'src') 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 intersect(const Rectangle& inner) const + { + Rectangle 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; + } }; }; -- cgit v1.2.3