17 #ifndef HEADER_SUPERTUX_MATH_RECT_HPP 18 #define HEADER_SUPERTUX_MATH_RECT_HPP 26 #include "math/size.hpp" 39 static Rect from_center(
int center_x,
int center_y,
int width,
int height)
41 return Rect(center_x - width / 2,
42 center_y - height / 2,
44 center_y + height / 2);
56 Rect& operator=(
const Rect& rhs) =
default;
58 Rect(
int left_,
int top_,
int right_,
int bottom_) :
65 Rect(
int left_,
int top_,
const Size& size) :
68 right(left_ + size.width),
69 bottom(top_ + size.height)
72 Rect(
const SDL_Rect& rect) :
75 right(rect.x + rect.w),
76 bottom(rect.y + rect.h)
81 bool operator==(
const Rect& other)
const 83 return (left == other.left &&
85 right == other.right &&
86 bottom == other.bottom);
89 bool contains(
int x,
int y)
const 91 return (left <= x && x < right &&
92 top <= y && y < bottom);
95 bool contains(
const Rect& other)
const 97 return (left <= other.left && other.right <= right &&
98 top <= other.top && other.bottom <= bottom);
101 const int& get_left()
const {
return left; }
102 const int& get_right()
const {
return right; }
103 const int& get_top()
const {
return top; }
104 const int& get_bottom()
const {
return bottom; }
106 int get_width()
const {
return right - left; }
107 int get_height()
const {
return bottom - top; }
108 Size get_size()
const {
return Size(right - left, bottom - top); }
109 int get_area()
const {
return get_width() * get_height(); }
113 return (get_width() <= 0 ||
119 return left <= right && top <= bottom;
122 Rect normalized()
const 124 return Rect(std::min(left, right),
125 std::min(top, bottom),
126 std::max(left, right),
127 std::max(top, bottom));
130 Rect moved(
int x,
int y)
const 132 return Rect(left + x,
138 Rect grown(
int border)
const 140 return Rect(left - border,
146 Rectf to_rectf()
const;
147 SDL_Rect to_sdl()
const 149 return { left, top, get_width(), get_height() };
152 bool operator<(
const Rect& other)
const {
153 return std::tie(left, top, right, bottom) < std::tie(other.left, other.top, other.right, other.bottom);
157 std::ostream& operator<<(std::ostream& out,
const Rect& rect);