From 8e6bf5fc2fb2f466c39b63283b5d408eff68e32a Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Fri, 21 Feb 2014 20:09:11 +0400 Subject: [PATCH] Add basic Point arith --- SDL2pp/Point.cc | 34 ++++++++++++++++++++++++++++++++++ SDL2pp/Point.hh | 6 ++++++ tests/test_pointrect.cc | 28 ++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+) diff --git a/SDL2pp/Point.cc b/SDL2pp/Point.cc index d3c9cd1..d36f005 100644 --- a/SDL2pp/Point.cc +++ b/SDL2pp/Point.cc @@ -82,4 +82,38 @@ void Point::SetY(int y) { point_.y = y; } +Point Point::operator+(const Point& other) const { + if (!valid_ || !other.valid_) + return Point(); + return Point(point_.x + other.point_.x, point_.y + other.point_.y); +} + +Point Point::operator-(const Point& other) const { + if (!valid_ || !other.valid_) + return Point(); + return Point(point_.x - other.point_.x, point_.y - other.point_.y); +} + +Point& Point::operator+=(const Point& other) { + if (!valid_ || !other.valid_) { + valid_ = false; + } else { + point_.x += other.point_.x; + point_.y += other.point_.y; + } + + return *this; +} + +Point& Point::operator-=(const Point& other) { + if (!valid_ || !other.valid_) { + valid_ = false; + } else { + point_.x -= other.point_.x; + point_.y -= other.point_.y; + } + + return *this; +} + } diff --git a/SDL2pp/Point.hh b/SDL2pp/Point.hh index 31d34e9..568fc67 100644 --- a/SDL2pp/Point.hh +++ b/SDL2pp/Point.hh @@ -58,6 +58,12 @@ public: int GetY() const; void SetY(int y); + + Point operator+(const Point& other) const; + Point operator-(const Point& other) const; + + Point& operator+=(const Point& other); + Point& operator-=(const Point& other); }; } diff --git a/tests/test_pointrect.cc b/tests/test_pointrect.cc index a5378d2..ebfd627 100644 --- a/tests/test_pointrect.cc +++ b/tests/test_pointrect.cc @@ -55,6 +55,34 @@ BEGIN_TEST() EXPECT_TRUE(p.GetX() == 14 && p.GetY() == 15); } + { + // 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); + + sum += Point(100, 200); + diff -= Point(100, 200); + + EXPECT_TRUE(sum.GetX() == 111 && sum.GetY() == 222); + EXPECT_TRUE(diff.GetX() == -111 && diff.GetY() == -222); + + sum += Point::Null(); + diff -= Point::Null(); + + EXPECT_TRUE(sum.IsNull()); + EXPECT_TRUE(diff.IsNull()); + + EXPECT_TRUE((Point(1,1) + Point::Null()).IsNull()); + EXPECT_TRUE((Point(1,1) - Point::Null()).IsNull()); + EXPECT_TRUE((Point::Null() + Point(1,1)).IsNull()); + EXPECT_TRUE((Point::Null() - Point(1,1)).IsNull()); + EXPECT_TRUE((Point::Null() - Point::Null()).IsNull()); + EXPECT_TRUE((Point::Null() - Point::Null()).IsNull()); + } + { // Rect basic ops Rect r(1,2,3,4);