17 #ifndef HEADER_SUPERTUX_MATH_RECTF_HPP 18 #define HEADER_SUPERTUX_MATH_RECTF_HPP 25 #include "math/anchor_point.hpp" 26 #include "math/sizef.hpp" 27 #include "math/vector.hpp" 34 static Rectf from_center(
const Vector& center,
const Sizef& size)
36 return Rectf(center.x - size.width / 2.0f,
37 center.y - size.height / 2.0f,
38 center.x + size.width / 2.0f,
39 center.y + size.height / 2.0f);
51 Rectf(
const Vector& np1,
const Vector& np2) :
52 m_p1(np1), m_size(np2.x - np1.x, np2.y - np1.y)
54 assert(m_size.width >= 0 &&
58 Rectf(
float x1,
float y1,
float x2,
float y2) :
59 m_p1(x1, y1), m_size(x2 - x1, y2 - y1)
61 assert(m_size.width >= 0 &&
71 Rectf(
const SDL_FRect& rect) :
72 m_p1(rect.x, rect.y), m_size(rect.w, rect.h)
78 bool operator==(
const Rectf& other)
const 80 return (m_p1 == other.m_p1 &&
81 m_size == other.m_size);
84 float& get_left() {
return m_p1.x; }
85 float& get_top() {
return m_p1.y; }
87 float get_left()
const {
return m_p1.x; }
88 float get_right()
const {
return m_p1.x + m_size.width; }
89 float get_top()
const {
return m_p1.y; }
90 float get_bottom()
const {
return m_p1.y + m_size.height; }
92 float& get_width() {
return m_size.width; }
93 float& get_height() {
return m_size.height; }
95 float get_width()
const {
return m_size.width; }
96 float get_height()
const {
return m_size.height; }
98 void set_left(
float v) { m_size.width -= v - m_p1.x; m_p1.x = v; }
99 void set_right(
float v) { m_size.width += v - get_right(); }
101 void set_top(
float v) { m_size.height -= v - m_p1.y; m_p1.y = v; }
102 void set_bottom(
float v) { m_size.height += v - get_bottom(); }
104 Vector get_middle()
const {
return Vector(m_p1.x + m_size.width / 2.0f,
105 m_p1.y + m_size.height / 2.0f); }
107 void set_pos(
const Vector& v) { m_p1 = v; }
109 void set_width(
float width) { m_size.width = width; }
110 void set_height(
float height) { m_size.height = height; }
111 void set_size(
float width,
float height) { m_size =
Sizef(width, height); }
112 Sizef get_size()
const {
return m_size; }
116 return get_width() <= 0 ||
120 void move(
const Vector& v) { m_p1 += v; }
121 Rectf moved(
const Vector& v)
const {
return Rectf(m_p1 + v, m_size); }
123 bool contains(
const Vector& v)
const {
124 return v.x >= m_p1.x && v.y >= m_p1.y && v.x < get_right() && v.y < get_bottom();
127 bool overlaps(
const Rectf& other)
const 129 if (get_right() < other.get_left() || get_left() > other.get_right())
131 if (get_bottom() < other.get_top() || get_top() > other.get_bottom())
137 float distance (
const Vector& other, AnchorPoint ap = ANCHOR_MIDDLE)
const 139 Vector v = get_anchor_pos (*
this, ap);
140 return glm::distance(v, other);
143 float distance (
const Rectf& other, AnchorPoint ap = ANCHOR_MIDDLE)
const 145 Vector v1 = get_anchor_pos(*
this, ap);
146 Vector v2 = get_anchor_pos(other, ap);
148 return glm::distance(v1, v2);
151 Rectf grown(
float border)
const 154 if (m_size.width + border * 2 < 0.f || m_size.height + border * 2 < 0.f)
157 return Rectf(m_p1.x - border, m_p1.y - border,
158 get_right() + border, get_bottom() + border);
164 Vector p1()
const {
return m_p1; }
165 Vector p2()
const {
return Vector(m_p1.x + m_size.width, m_p1.y + m_size.height); }
167 void set_p1(
const Vector& p) {
168 m_size =
Sizef(m_size.width + (m_p1.x - p.x),
169 m_size.height + (m_p1.y - p.y));
172 void set_p2(
const Vector& p) {
173 m_size =
Sizef(p.x - m_p1.x,
177 Rect to_rect()
const;
178 SDL_FRect to_sdl()
const 180 return { m_p1.x, m_p1.y, m_size.width, m_size.height };
189 std::ostream& operator<<(std::ostream& out,
const Rectf& rect);