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 int get_width()
const {
return right - left; }
102 int get_height()
const {
return bottom - top; }
103 Size get_size()
const {
return Size(right - left, bottom - top); }
104 int get_area()
const {
return get_width() * get_height(); }
108 return (get_width() <= 0 ||
114 return left <= right && top <= bottom;
117 Rect normalized()
const 119 return Rect(std::min(left, right),
120 std::min(top, bottom),
121 std::max(left, right),
122 std::max(top, bottom));
125 Rect moved(
int x,
int y)
const 127 return Rect(left + x,
133 Rect grown(
int border)
const 135 return Rect(left - border,
141 SDL_Rect to_sdl()
const 143 return {left, top, get_width(), get_height()};
146 bool operator<(
const Rect& other)
const {
147 return std::tie(left, top, right, bottom) < std::tie(other.left, other.top, other.right, other.bottom);
151 std::ostream& operator<<(std::ostream& out,
const Rect& rect);