From 0f4eea5aab72808fc88f31a74299e201f539e5d3 Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Thu, 8 Jan 2015 06:52:29 +0300 Subject: [PATCH] Add another variant of Rect::Contains() --- COPYING.txt | 2 +- SDL2pp/Rect.cc | 6 +++++- SDL2pp/Rect.hh | 13 ++++++++++++- tests/test_pointrect.cc | 8 ++++++++ 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/COPYING.txt b/COPYING.txt index 2a9f46c..a8290cd 100644 --- a/COPYING.txt +++ b/COPYING.txt @@ -1,6 +1,6 @@ libSDL2pp - C++11 bindings/wrapper for SDL2 -Copyright (C) 2013-2014 Dmitry Marakasov +Copyright (C) 2013-2015 Dmitry Marakasov This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages diff --git a/SDL2pp/Rect.cc b/SDL2pp/Rect.cc index b9a1479..b464835 100644 --- a/SDL2pp/Rect.cc +++ b/SDL2pp/Rect.cc @@ -1,6 +1,6 @@ /* libSDL2pp - C++11 bindings/wrapper for SDL2 - Copyright (C) 2013-2014 Dmitry Marakasov + Copyright (C) 2013-2015 Dmitry Marakasov This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages @@ -144,6 +144,10 @@ void Rect::SetY2(int y2) { h = y2 - y + 1; } +bool Rect::Contains(int px, int py) const { + return px >= x && py >= y && px <= GetX2() && py <= GetY2(); +} + bool Rect::Contains(const Point& point) const { return point.x >= x && point.y >= y && point.x <= GetX2() && point.y <= GetY2(); } diff --git a/SDL2pp/Rect.hh b/SDL2pp/Rect.hh index cbf54e9..675d7be 100644 --- a/SDL2pp/Rect.hh +++ b/SDL2pp/Rect.hh @@ -1,6 +1,6 @@ /* libSDL2pp - C++11 bindings/wrapper for SDL2 - Copyright (C) 2013-2014 Dmitry Marakasov + Copyright (C) 2013-2015 Dmitry Marakasov This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages @@ -281,6 +281,17 @@ public: //////////////////////////////////////////////////////////// void SetY2(int y2); + //////////////////////////////////////////////////////////// + /// \brief Check whether the rect contains given point + /// + /// \param x X coordinate of a point + /// \param y Y coordinate of a point + /// + /// \returns True if the point is contained in the rect + /// + //////////////////////////////////////////////////////////// + bool Contains(int x, int y) const; + //////////////////////////////////////////////////////////// /// \brief Check whether the rect contains given point /// diff --git a/tests/test_pointrect.cc b/tests/test_pointrect.cc index 7c387bb..ae4f9b6 100644 --- a/tests/test_pointrect.cc +++ b/tests/test_pointrect.cc @@ -158,6 +158,14 @@ BEGIN_TEST() EXPECT_TRUE(!r.Contains(Point(15, 20))); EXPECT_TRUE(!r.Contains(Point(10, 25))); + EXPECT_TRUE(r.Contains(10, 20)); + EXPECT_TRUE(r.Contains(14, 24)); + + EXPECT_TRUE(!r.Contains(9, 20)); + EXPECT_TRUE(!r.Contains(10, 19)); + EXPECT_TRUE(!r.Contains(15, 20)); + EXPECT_TRUE(!r.Contains(10, 25)); + // Rect contains rect EXPECT_TRUE(r.Contains(r)); EXPECT_TRUE(r.Contains(Rect(11, 21, 3, 3)));