diff --git a/SDL2pp/Point.cc b/SDL2pp/Point.cc index c41cf7e..d64136c 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); } @@ -78,10 +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; @@ -103,6 +123,27 @@ 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; + + 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; @@ -110,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 026dbc3..23b4165 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 /// @@ -181,21 +189,71 @@ 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; //////////////////////////////////////////////////////////// - /// \brief Get point's memberwise multiplication by an integer + /// \brief Get point's memberwise division by another point + /// + /// \param[in] other Divisor + /// + /// \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 remainder from division + /// by another point + /// + /// \param[in] other 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] other Multiplier + /// + /// \returns New Point representing memberwise multiplication + /// of point by another point + /// + //////////////////////////////////////////////////////////// + Point operator*(const Point& other) const; + //////////////////////////////////////////////////////////// /// \brief Memberwise add another point /// @@ -226,6 +284,37 @@ public: //////////////////////////////////////////////////////////// Point& operator/=(int value); + //////////////////////////////////////////////////////////// + /// \brief Memberwise divide by another point + /// + /// \param[in] other Divisor + /// + /// \returns Reference to self + /// + //////////////////////////////////////////////////////////// + Point& operator/=(const Point& other); + + //////////////////////////////////////////////////////////// + /// \brief Memberwise remainder from division by an integer + /// + /// \param[in] value Divisor + /// + /// \returns Reference to self + /// + //////////////////////////////////////////////////////////// + Point& operator%=(int value); + + //////////////////////////////////////////////////////////// + /// \brief Memberwise remainder from division by another + /// point + /// + /// \param[in] other Divisor + /// + /// \returns Reference to self + /// + //////////////////////////////////////////////////////////// + Point& operator%=(const Point& other); + //////////////////////////////////////////////////////////// /// \brief Memberwise multiply by an integer /// @@ -237,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] other 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 /// @@ -247,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 /// 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)); } {