29#include <SDL2pp/Optional.hh>
30#include <SDL2pp/Point.hh>
31#include <SDL2pp/Export.hh>
50class SDL2PP_EXPORT
Rect :
public SDL_Rect {
58 constexpr Rect() : SDL_Rect{0, 0, 0, 0} {
67 constexpr Rect(
const SDL_Rect& rect) : SDL_Rect{rect.x, rect.y, rect.w, rect.h} {
77 constexpr Rect(
const Point& corner,
const Point& size) : SDL_Rect{corner.x, corner.y, size.x, size.y} {
89 constexpr Rect(
int x,
int y,
int w,
int h) : SDL_Rect{x, y, w, h} {
102 return Rect(cx - w/2, cy - h/2, w, h);
113 return Rect(center - size / 2, size);
126 return Rect(x1, y1, x2 - x1 + 1, y2 - y1 + 1);
174 constexpr
int GetX()
const {
327 return Point(GetX2(), y);
337 return Point(x, GetY2());
347 return Point(GetX2(), GetY2());
367 return Point(x + w/2, y + h/2);
380 return px >= x && py >= y && px <= GetX2() && py <= GetY2();
392 return point.x >= x && point.y >= y && point.x <= GetX2() && point.y <= GetY2();
404 return rect.x >= x && rect.y >= y && rect.
GetX2() <= GetX2() && rect.
GetY2() <= GetY2();
416 return !(rect.
GetX2() < x || rect.
GetY2() < y || rect.x > GetX2() || rect.y > GetY2());
427 Rect GetUnion(
const Rect& rect)
const;
447 Rect GetExtension(
unsigned int amount)
const;
460 Rect GetExtension(
unsigned int hamount,
unsigned int vamount)
const;
470 Rect& Extend(
unsigned int amount);
483 Rect& Extend(
unsigned int hamount,
unsigned int vamount);
493 Optional<Rect> GetIntersection(
const Rect& rect)
const;
514 bool IntersectLine(
int& x1,
int& y1,
int& x2,
int& y2)
const;
532 bool IntersectLine(
Point& p1,
Point& p2)
const;
543 return Rect(x + offset.x, y + offset.y, w, h);
555 return Rect(x - offset.x, y - offset.y, w, h);
599 return a.x == b.x && a.y == b.y && a.w == b.w && a.h == b.h;
635SDL2PP_EXPORT std::ostream& operator<<(std::ostream& stream,
const SDL2pp::Rect& rect);
644struct hash<SDL2pp::Rect> {
654 size_t seed = std::hash<int>()(r.x);
655 seed ^= std::hash<int>()(r.y) + 0x9e3779b9 + (seed<<6) + (seed>>2);
656 seed ^= std::hash<int>()(r.w) + 0x9e3779b9 + (seed<<6) + (seed>>2);
657 seed ^= std::hash<int>()(r.h) + 0x9e3779b9 + (seed<<6) + (seed>>2);
2D point
Definition: Point.hh:51
2D rectangle
Definition: Rect.hh:50
static constexpr Rect FromCorners(int x1, int y1, int x2, int y2)
Construct the rect from given corners coordinates.
Definition: Rect.hh:125
Rect(Rect &&) noexcept=default
Move constructor.
Rect & operator+=(const Point &offset)
Move by then given offset.
Definition: Rect.hh:566
constexpr int GetY() const
Get Y coordinate of the rect corner.
Definition: Rect.hh:197
Rect & operator-=(const Point &offset)
Move by an opposite of the given offset.
Definition: Rect.hh:580
constexpr bool Intersects(const Rect &rect) const
Check whether the rect intersects another rect.
Definition: Rect.hh:415
Rect(const Rect &) noexcept=default
Copy constructor.
constexpr Point GetTopRight() const
Get top right corner of the rect.
Definition: Rect.hh:326
Rect & SetW(int nw)
Set width of the rect.
Definition: Rect.hh:232
constexpr Rect operator+(const Point &offset) const
Get rectangle moved by a given offset.
Definition: Rect.hh:542
Rect & SetH(int nh)
Set height of the rect.
Definition: Rect.hh:255
constexpr Point GetBottomLeft() const
Get bottom left corner of the rect.
Definition: Rect.hh:336
constexpr int GetY2() const
Get Y coordinate of the rect second corner.
Definition: Rect.hh:291
static constexpr Rect FromCenter(const Point ¢er, const Point &size)
Construct the rect from given center coordinates and size.
Definition: Rect.hh:112
constexpr Point GetCentroid() const
Get centroid of the rect.
Definition: Rect.hh:366
Rect & SetY(int ny)
Set Y coordinate of the rect corner.
Definition: Rect.hh:209
static constexpr Rect FromCorners(const Point &p1, const Point &p2)
Construct the rect from given centers coordinates.
Definition: Rect.hh:136
Rect & SetX(int nx)
Set X coordinate of the rect corner.
Definition: Rect.hh:186
constexpr int GetH() const
Get height of the rect.
Definition: Rect.hh:243
constexpr Rect(int x, int y, int w, int h)
Construct the rect from given corner coordinates, width and height.
Definition: Rect.hh:89
constexpr bool Contains(const Rect &rect) const
Check whether the rect contains another rect.
Definition: Rect.hh:403
constexpr Rect(const SDL_Rect &rect)
Construct a rect from existing SDL_Rect.
Definition: Rect.hh:67
Rect & SetX2(int x2)
Set X coordinate of the rect second corner.
Definition: Rect.hh:280
constexpr Point GetSize() const
Get size of the rect.
Definition: Rect.hh:356
constexpr Point GetBottomRight() const
Get bottom right corner of the rect.
Definition: Rect.hh:346
constexpr Rect operator-(const Point &offset) const
Get rectangle moved by an opposite of given offset.
Definition: Rect.hh:554
constexpr int GetX2() const
Get X coordinate of the rect second corner.
Definition: Rect.hh:266
constexpr Point GetTopLeft() const
Get top left corner of the rect.
Definition: Rect.hh:316
constexpr int GetW() const
Get width of the rect.
Definition: Rect.hh:220
constexpr bool Contains(int px, int py) const
Check whether the rect contains given point.
Definition: Rect.hh:379
Rect & SetY2(int y2)
Set Y coordinate of the rect second corner.
Definition: Rect.hh:305
constexpr Rect(const Point &corner, const Point &size)
Construct the rect from given corner coordinates, and size.
Definition: Rect.hh:77
constexpr Rect()
Default constructor.
Definition: Rect.hh:58
static constexpr Rect FromCenter(int cx, int cy, int w, int h)
Construct the rect from given center coordinates, width and height.
Definition: Rect.hh:101
constexpr bool Contains(const Point &point) const
Check whether the rect contains given point.
Definition: Rect.hh:391
size_t operator()(const SDL2pp::Rect &r) const
Hash function for SDL2pp::Rect.
Definition: Rect.hh:653