From fa7ba9495717f1a972a18524357c0560927b94cb Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Tue, 20 Jan 2015 02:38:16 +0300 Subject: [PATCH] Implement Point::Wrap --- SDL2pp/Point.cc | 20 ++++++++++++++++++++ SDL2pp/Point.hh | 20 ++++++++++++++++++++ tests/test_pointrect.cc | 23 +++++++++++++++++++++++ 3 files changed, 63 insertions(+) diff --git a/SDL2pp/Point.cc b/SDL2pp/Point.cc index 8fe2505..312f92b 100644 --- a/SDL2pp/Point.cc +++ b/SDL2pp/Point.cc @@ -128,4 +128,24 @@ Point& Point::Clamp(const Rect& rect) { return *this; } +Point Point::GetWrapped(const Rect& rect) const { + Point p = *this; + p.Wrap(rect); + return p; +} + +Point& Point::Wrap(const Rect& rect) { + if (x < rect.x) + x = rect.x + rect.w - 1 - (rect.x - x + rect.w - 1) % rect.w; + else if (x >= rect.x + rect.w) + x = rect.x + (x - rect.x - rect.w) % rect.w; + + if (y < rect.y) + y = rect.y + rect.h - 1 - (rect.y - y + rect.h - 1) % rect.h; + else if (y >= rect.y + rect.h) + y = rect.y + (y - rect.y - rect.h) % rect.h; + + return *this; +} + } diff --git a/SDL2pp/Point.hh b/SDL2pp/Point.hh index 5ea6a63..2149342 100644 --- a/SDL2pp/Point.hh +++ b/SDL2pp/Point.hh @@ -253,6 +253,26 @@ public: /// //////////////////////////////////////////////////////////// Point& Clamp(const Rect& rect); + + //////////////////////////////////////////////////////////// + /// \brief Get a point wrapped within a specified rect + /// + /// \param[in] rect Rectangle to wrap with + /// + /// \returns Wrapped point + /// + //////////////////////////////////////////////////////////// + Point GetWrapped(const Rect& rect) const; + + //////////////////////////////////////////////////////////// + /// \brief Wrap point coordinates within a spedified rect + /// + /// \param[in] rect Rectangle to wrap with + /// + /// \returns Reference to self + /// + //////////////////////////////////////////////////////////// + Point& Wrap(const Rect& rect); }; } diff --git a/tests/test_pointrect.cc b/tests/test_pointrect.cc index 3585ed6..1d9fd6a 100644 --- a/tests/test_pointrect.cc +++ b/tests/test_pointrect.cc @@ -310,4 +310,27 @@ BEGIN_TEST() EXPECT_TRUE(Point(10, 10).GetClamped(rect) == Point(3, 5)); EXPECT_TRUE(Point(10, 10).Clamp(rect) == Point(3, 5)); } + + { + // wrap + Rect rect(10, 20, 30, 40); + + EXPECT_TRUE(Point(10, 20).GetWrapped(rect) == Point(10, 20)); + EXPECT_TRUE(Point(39, 59).GetWrapped(rect) == Point(39, 59)); + + EXPECT_TRUE(Point(9, 20).GetWrapped(rect) == Point(39, 20)); + EXPECT_TRUE(Point(40, 20).GetWrapped(rect) == Point(10, 20)); + EXPECT_TRUE(Point(10, 19).GetWrapped(rect) == Point(10, 59)); + EXPECT_TRUE(Point(10, 60).GetWrapped(rect) == Point(10, 20)); + + EXPECT_TRUE(Point(-50, -60).GetWrapped(rect) == Point(10, 20)); + EXPECT_TRUE(Point(-20, -20).GetWrapped(rect) == Point(10, 20)); + EXPECT_TRUE(Point(10, 20).GetWrapped(rect) == Point(10, 20)); + EXPECT_TRUE(Point(40, 60).GetWrapped(rect) == Point(10, 20)); + EXPECT_TRUE(Point(70, 100).GetWrapped(rect) == Point(10, 20)); + EXPECT_TRUE(Point(100, 140).GetWrapped(rect) == Point(10, 20)); + + EXPECT_TRUE(Point(-19, -19).GetWrapped(rect) == Point(11, 21)); + EXPECT_TRUE(Point(-21, -21).GetWrapped(rect) == Point(39, 59)); + } END_TEST()