From 0215d2526ed177022386deb10fc05cb57fbf7e77 Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Fri, 3 Jul 2015 21:52:53 +0300 Subject: [PATCH 1/6] Fix typo --- SDL2pp/Point.hh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SDL2pp/Point.hh b/SDL2pp/Point.hh index 026dbc3..5db0cc3 100644 --- a/SDL2pp/Point.hh +++ b/SDL2pp/Point.hh @@ -181,7 +181,7 @@ public: /// /// \param[in] value Divisor /// - /// \returns New Point representing memberwise division of point by and integer + /// \returns New Point representing memberwise division of point by an integer /// //////////////////////////////////////////////////////////// Point operator/(int value) const; From 1001bf8fa211d539e4f3f4730aac843d34e3bc7e Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Fri, 3 Jul 2015 22:00:59 +0300 Subject: [PATCH 2/6] Add memberwise remainter operators for Point --- SDL2pp/Point.cc | 11 +++++++++++ SDL2pp/Point.hh | 20 ++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/SDL2pp/Point.cc b/SDL2pp/Point.cc index c41cf7e..5ed4146 100644 --- a/SDL2pp/Point.cc +++ b/SDL2pp/Point.cc @@ -78,6 +78,10 @@ Point Point::operator/(int value) const { return Point(x / value, y / value); } +Point Point::operator%(int value) const { + return Point(x % value, y % value); +} + Point Point::operator*(int value) const { return Point(x * value, y * value); } @@ -103,6 +107,13 @@ Point& Point::operator/=(int value) { return *this; } +Point& Point::operator%=(int value) { + x %= value; + y %= value; + + return *this; +} + Point& Point::operator*=(int value) { x *= value; y *= value; diff --git a/SDL2pp/Point.hh b/SDL2pp/Point.hh index 5db0cc3..bae5ba6 100644 --- a/SDL2pp/Point.hh +++ b/SDL2pp/Point.hh @@ -186,6 +186,16 @@ public: //////////////////////////////////////////////////////////// Point operator/(int value) const; + //////////////////////////////////////////////////////////// + /// \brief Get point's memberwise remainder from integer division + /// + /// \param[in] value Divisor + /// + /// \returns New Point representing memberwise remainder from point divided by an integer + /// + //////////////////////////////////////////////////////////// + Point operator%(int value) const; + //////////////////////////////////////////////////////////// /// \brief Get point's memberwise multiplication by an integer /// @@ -226,6 +236,16 @@ public: //////////////////////////////////////////////////////////// Point& operator/=(int value); + //////////////////////////////////////////////////////////// + /// \brief Memberwise remainder from integer division + /// + /// \param[in] value Divisor + /// + /// \returns Reference to self + /// + //////////////////////////////////////////////////////////// + Point& operator%=(int value); + //////////////////////////////////////////////////////////// /// \brief Memberwise multiply by an integer /// From 47f0263ca3c8c9fdff230995335f45ec87cd0282 Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Fri, 3 Jul 2015 22:21:02 +0300 Subject: [PATCH 3/6] Add unary minus operator for Point --- SDL2pp/Point.cc | 4 ++++ SDL2pp/Point.hh | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/SDL2pp/Point.cc b/SDL2pp/Point.cc index 5ed4146..1a24745 100644 --- a/SDL2pp/Point.cc +++ b/SDL2pp/Point.cc @@ -66,6 +66,10 @@ Point& Point::SetY(int ny) { return *this; } +Point Point::operator-() const { + return Point(-x, -y); +} + Point Point::operator+(const Point& other) const { return Point(x + other.x, y + other.y); } diff --git a/SDL2pp/Point.hh b/SDL2pp/Point.hh index bae5ba6..bce691f 100644 --- a/SDL2pp/Point.hh +++ b/SDL2pp/Point.hh @@ -156,6 +156,14 @@ public: //////////////////////////////////////////////////////////// Point& SetY(int ny); + //////////////////////////////////////////////////////////// + /// \brief Get point's memberwise negation + /// + /// \returns New Point representing memberwise negation + /// + //////////////////////////////////////////////////////////// + Point operator-() const; + //////////////////////////////////////////////////////////// /// \brief Get point's memberwise addition with another point /// From 495f8737361b7ff409f98b6645d5a1d8bffbfc44 Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Fri, 3 Jul 2015 22:22:48 +0300 Subject: [PATCH 4/6] Add div/mul/remainder operators with another Point as an argument --- SDL2pp/Point.cc | 33 ++++++++++++++++++ SDL2pp/Point.hh | 89 ++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 114 insertions(+), 8 deletions(-) diff --git a/SDL2pp/Point.cc b/SDL2pp/Point.cc index 1a24745..d64136c 100644 --- a/SDL2pp/Point.cc +++ b/SDL2pp/Point.cc @@ -82,14 +82,26 @@ Point Point::operator/(int value) const { return Point(x / value, y / value); } +Point Point::operator/(const Point& other) const { + return Point(x / other.x, y / other.y); +} + Point Point::operator%(int value) const { return Point(x % value, y % value); } +Point Point::operator%(const Point& other) const { + return Point(x % other.x, y % other.y); +} + Point Point::operator*(int value) const { return Point(x * value, y * value); } +Point Point::operator*(const Point& other) const { + return Point(x * other.x, y * other.y); +} + Point& Point::operator+=(const Point& other) { x += other.x; y += other.y; @@ -111,6 +123,13 @@ Point& Point::operator/=(int value) { return *this; } +Point& Point::operator/=(const Point& other) { + x /= other.x; + y /= other.y; + + return *this; +} + Point& Point::operator%=(int value) { x %= value; y %= value; @@ -118,6 +137,13 @@ Point& Point::operator%=(int value) { return *this; } +Point& Point::operator%=(const Point& other) { + x %= other.x; + y %= other.y; + + return *this; +} + Point& Point::operator*=(int value) { x *= value; y *= value; @@ -125,6 +151,13 @@ Point& Point::operator*=(int value) { return *this; } +Point& Point::operator*=(const Point& other) { + x *= other.x; + y *= other.y; + + return *this; +} + Point Point::GetClamped(const Rect& rect) const { Point p = *this; p.Clamp(rect); diff --git a/SDL2pp/Point.hh b/SDL2pp/Point.hh index bce691f..d1398a9 100644 --- a/SDL2pp/Point.hh +++ b/SDL2pp/Point.hh @@ -189,31 +189,71 @@ public: /// /// \param[in] value Divisor /// - /// \returns New Point representing memberwise division of point by an integer + /// \returns New Point representing memberwise division of + /// point by an integer /// //////////////////////////////////////////////////////////// Point operator/(int value) const; //////////////////////////////////////////////////////////// - /// \brief Get point's memberwise remainder from integer division + /// \brief Get point's memberwise division by another point /// /// \param[in] value Divisor /// - /// \returns New Point representing memberwise remainder from point divided by an integer + /// \returns New Point representing memberwise division of + /// point by another point + /// + //////////////////////////////////////////////////////////// + Point operator/(const Point& other) const; + + //////////////////////////////////////////////////////////// + /// \brief Get point's memberwise remainder from division + /// by an integer + /// + /// \param[in] value Divisor + /// + /// \returns New Point representing memberwise remainder + /// from division by an integer /// //////////////////////////////////////////////////////////// Point operator%(int value) const; //////////////////////////////////////////////////////////// - /// \brief Get point's memberwise multiplication by an integer + /// \brief Get point's memberwise remainder from division + /// by another point + /// + /// \param[in] value Divisor + /// + /// \returns New Point representing memberwise remainder + /// from division by another point + /// + //////////////////////////////////////////////////////////// + Point operator%(const Point& other) const; + + //////////////////////////////////////////////////////////// + /// \brief Get point's memberwise multiplication by an + /// integer /// /// \param[in] value Multiplier /// - /// \returns New Point representing memberwise multiplication of point by an integer + /// \returns New Point representing memberwise multiplication + /// of point by an integer /// //////////////////////////////////////////////////////////// Point operator*(int value) const; + //////////////////////////////////////////////////////////// + /// \brief Get point's memberwise multiplication by anoter + /// point + /// + /// \param[in] value Multiplier + /// + /// \returns New Point representing memberwise multiplication + /// of point by another point + /// + //////////////////////////////////////////////////////////// + Point operator*(const Point& other) const; + //////////////////////////////////////////////////////////// /// \brief Memberwise add another point /// @@ -245,7 +285,17 @@ public: Point& operator/=(int value); //////////////////////////////////////////////////////////// - /// \brief Memberwise remainder from integer division + /// \brief Memberwise divide by another point + /// + /// \param[in] value Divisor + /// + /// \returns Reference to self + /// + //////////////////////////////////////////////////////////// + Point& operator/=(const Point& other); + + //////////////////////////////////////////////////////////// + /// \brief Memberwise remainder from division by an integer /// /// \param[in] value Divisor /// @@ -254,6 +304,17 @@ public: //////////////////////////////////////////////////////////// Point& operator%=(int value); + //////////////////////////////////////////////////////////// + /// \brief Memberwise remainder from division by another + /// point + /// + /// \param[in] value Divisor + /// + /// \returns Reference to self + /// + //////////////////////////////////////////////////////////// + Point& operator%=(const Point& other); + //////////////////////////////////////////////////////////// /// \brief Memberwise multiply by an integer /// @@ -265,7 +326,18 @@ public: Point& operator*=(int value); //////////////////////////////////////////////////////////// - /// \brief Get a point with coordinates modified so it fits into a given rect + /// \brief Memberwise multiply by another point + /// + /// \param[in] value Multiplier + /// + /// \returns Reference to self + /// + //////////////////////////////////////////////////////////// + Point& operator*=(const Point& other); + + //////////////////////////////////////////////////////////// + /// \brief Get a point with coordinates modified so it fits + /// into a given rect /// /// \param[in] rect Rectangle to clamp with /// @@ -275,7 +347,8 @@ public: Point GetClamped(const Rect& rect) const; //////////////////////////////////////////////////////////// - /// \brief Clamp point coordinates to make it fit into a given rect + /// \brief Clamp point coordinates to make it fit into a + /// given rect /// /// \param[in] rect Rectangle to clamp with /// From dacf1cbf04e7dc8738734c88413ca1044d96063e Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Fri, 3 Jul 2015 22:23:06 +0300 Subject: [PATCH 5/6] Update Point arith test --- tests/test_pointrect.cc | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/tests/test_pointrect.cc b/tests/test_pointrect.cc index c9e7bcf..c2676ec 100644 --- a/tests/test_pointrect.cc +++ b/tests/test_pointrect.cc @@ -55,26 +55,30 @@ BEGIN_TEST() { // Point arith - Point sum = Point(1, 2) + Point(10, 20); - Point diff = Point(-1, -2) - Point(10, 20); - EXPECT_TRUE(sum.GetX() == 11 && sum.GetY() == 22); - EXPECT_TRUE(diff.GetX() == -11 && diff.GetY() == -22); + // Unary + EXPECT_EQUAL(-Point(1, 2), Point(-1, -2)); - sum += Point(100, 200); - diff -= Point(100, 200); + // Binary + EXPECT_EQUAL(Point(1, 2) + Point(10, 20), Point(11, 22)); + EXPECT_EQUAL(Point(-1, -2) - Point(10, 20), Point(-11, -22)); + EXPECT_EQUAL(Point(20, 60) / 5, Point(4, 12)); + EXPECT_EQUAL(Point(20, 60) / Point(5, 10), Point(4, 6)); + EXPECT_EQUAL(Point(20, 60) % 11, Point(9, 5)); + EXPECT_EQUAL(Point(20, 60) % Point(11, 13), Point(9, 8)); + EXPECT_EQUAL(Point(2, 3) * 5, Point(10, 15)); + EXPECT_EQUAL(Point(2, 3) * Point(10, 20), Point(20, 60)); - EXPECT_TRUE(sum.GetX() == 111 && sum.GetY() == 222); - EXPECT_TRUE(diff.GetX() == -111 && diff.GetY() == -222); - - sum /= 111; - diff *= 2; - - EXPECT_TRUE(sum == Point(1, 2)); - EXPECT_TRUE(diff == Point(-222, -444)); - - EXPECT_TRUE(sum * 2 == Point(2, 4)); - EXPECT_TRUE(diff / 2 == Point(-111, -222)); + // Assignments + Point p(1, 2); + EXPECT_EQUAL(p += Point(10, 20), Point(11, 22)); + EXPECT_EQUAL(p -= Point(1, 2), Point(10, 20)); + EXPECT_EQUAL(p /= 2, Point(5, 10)); + EXPECT_EQUAL(p %= 7, Point(5, 3)); + EXPECT_EQUAL(p *= 3, Point(15, 9)); + EXPECT_EQUAL(p /= Point(5, 3), Point(3, 3)); + EXPECT_EQUAL(p *= Point(10, 20), Point(30, 60)); + EXPECT_EQUAL(p %= Point(7, 11), Point(2, 5)); } { From 51120adc62838174cd304c952aa95669b6cfcec8 Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Fri, 3 Jul 2015 22:26:57 +0300 Subject: [PATCH 6/6] Fix documentation --- SDL2pp/Point.hh | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/SDL2pp/Point.hh b/SDL2pp/Point.hh index d1398a9..23b4165 100644 --- a/SDL2pp/Point.hh +++ b/SDL2pp/Point.hh @@ -198,7 +198,7 @@ public: //////////////////////////////////////////////////////////// /// \brief Get point's memberwise division by another point /// - /// \param[in] value Divisor + /// \param[in] other Divisor /// /// \returns New Point representing memberwise division of /// point by another point @@ -222,7 +222,7 @@ public: /// \brief Get point's memberwise remainder from division /// by another point /// - /// \param[in] value Divisor + /// \param[in] other Divisor /// /// \returns New Point representing memberwise remainder /// from division by another point @@ -246,7 +246,7 @@ public: /// \brief Get point's memberwise multiplication by anoter /// point /// - /// \param[in] value Multiplier + /// \param[in] other Multiplier /// /// \returns New Point representing memberwise multiplication /// of point by another point @@ -287,7 +287,7 @@ public: //////////////////////////////////////////////////////////// /// \brief Memberwise divide by another point /// - /// \param[in] value Divisor + /// \param[in] other Divisor /// /// \returns Reference to self /// @@ -308,7 +308,7 @@ public: /// \brief Memberwise remainder from division by another /// point /// - /// \param[in] value Divisor + /// \param[in] other Divisor /// /// \returns Reference to self /// @@ -328,7 +328,7 @@ public: //////////////////////////////////////////////////////////// /// \brief Memberwise multiply by another point /// - /// \param[in] value Multiplier + /// \param[in] other Multiplier /// /// \returns Reference to self ///