Add basic Point arith

This commit is contained in:
Dmitry Marakasov 2014-02-21 20:09:11 +04:00
parent 7b4b6c051a
commit 8e6bf5fc2f
3 changed files with 68 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -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);
};
}

View File

@ -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);