mirror of
https://github.com/libSDL2pp/libSDL2pp.git
synced 2025-08-05 11:56:00 -04:00
Add operators to offset a Rect by a Point
This commit is contained in:
parent
18fe309309
commit
f8c6b2a9fb
@ -137,4 +137,34 @@ bool Rect::Contains(const Point& point) const {
|
|||||||
return !(point.GetX() < GetX() || point.GetY() < GetY() || point.GetX() > GetX2() || point.GetY() > GetY2());
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -78,6 +78,12 @@ public:
|
|||||||
void SetY2(int y2);
|
void SetY2(int y2);
|
||||||
|
|
||||||
bool Contains(const Point& point) const;
|
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);
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -173,4 +173,20 @@ BEGIN_TEST()
|
|||||||
EXPECT_TRUE(!r.Contains(Point(15, 20)));
|
EXPECT_TRUE(!r.Contains(Point(15, 20)));
|
||||||
EXPECT_TRUE(!r.Contains(Point(10, 25)));
|
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()
|
END_TEST()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user