Add operators to offset a Rect by a Point

This commit is contained in:
Dmitry Marakasov 2014-12-20 17:51:23 +03:00
parent 18fe309309
commit f8c6b2a9fb
3 changed files with 52 additions and 0 deletions

View File

@ -137,4 +137,34 @@ bool Rect::Contains(const Point& point) const {
return !(point.GetX() < GetX() || point.GetY() < GetY() || point.GetX() > GetX2() || point.GetY() > GetY2());
}
Rect Rect::operator+(const Point& offset) const {
assert(!IsNull() && !offset.IsNull());
return Rect(rect_.x + offset.GetX(), rect_.y + offset.GetY(), rect_.w, rect_.h);
}
Rect& Rect::operator+=(const Point& offset) {
assert(!IsNull() && !offset.IsNull());
rect_.x += offset.GetX();
rect_.y += offset.GetY();
return *this;
}
Rect Rect::operator-(const Point& offset) const {
assert(!IsNull() && !offset.IsNull());
return Rect(rect_.x - offset.GetX(), rect_.y - offset.GetY(), rect_.w, rect_.h);
}
Rect& Rect::operator-=(const Point& offset) {
assert(!IsNull() && !offset.IsNull());
rect_.x -= offset.GetX();
rect_.y -= offset.GetY();
return *this;
}
}

View File

@ -78,6 +78,12 @@ public:
void SetY2(int y2);
bool Contains(const Point& point) const;
Rect operator+(const Point& offset) const;
Rect& operator+=(const Point& offset);
Rect operator-(const Point& offset) const;
Rect& operator-=(const Point& offset);
};
}

View File

@ -173,4 +173,20 @@ BEGIN_TEST()
EXPECT_TRUE(!r.Contains(Point(15, 20)));
EXPECT_TRUE(!r.Contains(Point(10, 25)));
}
{
// Rect offset
Rect r(1, 2, 3, 4);
EXPECT_TRUE(r + Point(10, 20) == Rect(11, 22, 3, 4));
EXPECT_TRUE(r - Point(10, 20) == Rect(-9, -18, 3, 4));
r += Point(10, 20);
EXPECT_TRUE(r == Rect(11, 22, 3, 4));
r -= Point(20, 40);
EXPECT_TRUE(r == Rect(-9, -18, 3, 4));
}
END_TEST()