Add rect::shifted_by

Companion of rect::shift for const objects
This commit is contained in:
Charles Dang 2024-09-04 01:30:08 -04:00
parent dd1f60b753
commit 354418188e
2 changed files with 17 additions and 0 deletions

View file

@ -107,6 +107,19 @@ void rect::shift(const point& other)
this->y += other.y;
}
rect rect::shifted_by(int x, int y) const
{
rect res = *this;
res.x += x;
res.y += y;
return res;
}
rect rect::shifted_by(const point& other) const
{
return shifted_by(other.x, other.y);
}
std::ostream& operator<<(std::ostream& s, const rect& r)
{
s << '[' << r.x << ',' << r.y << '|' << r.w << ',' << r.h << ']';

View file

@ -147,6 +147,10 @@ public:
* The point's X and Y coordinates will be added to the rectangle's.
*/
void shift(const point& p);
/** Returns a new rectangle shifted by the given relative position. */
rect shifted_by(int x, int y) const;
rect shifted_by(const point& p) const;
};
std::ostream& operator<<(std::ostream&, const rect&);