17 #ifndef HEADER_SUPERTUX_MATH_RECTF_HPP 18 #define HEADER_SUPERTUX_MATH_RECTF_HPP 23 #include "math/anchor_point.hpp" 24 #include "math/sizef.hpp" 25 #include "math/vector.hpp" 34 return Rectf(center.x - size.width / 2.0f,
35 center.y - size.height / 2.0f,
36 center.x + size.width / 2.0f,
37 center.y + size.height / 2.0f);
50 m_p1(np1), m_size(np2.x - np1.x, np2.y - np1.y)
54 Rectf(
float x1,
float y1,
float x2,
float y2) :
55 m_p1(x1, y1), m_size(x2 - x1, y2 - y1)
57 assert(m_size.width >= 0 &&
69 bool operator==(
const Rectf& other)
const 71 return (m_p1 == other.m_p1 &&
72 m_size == other.m_size);
76 float& get_left() {
return m_p1.x; }
77 float& get_top() {
return m_p1.y; }
79 float get_left()
const {
return m_p1.x; }
80 float get_right()
const {
return m_p1.x + m_size.width; }
81 float get_top()
const {
return m_p1.y; }
82 float get_bottom()
const {
return m_p1.y + m_size.height; }
84 float get_width()
const {
return m_size.width; }
85 float get_height()
const {
return m_size.height; }
87 void set_left(
float v) { m_size.width -= v - m_p1.x; m_p1.x = v; }
88 void set_right(
float v) { m_size.width += v - get_right(); }
90 void set_top(
float v) { m_size.height -= v - m_p1.y; m_p1.y = v; }
91 void set_bottom(
float v) { m_size.height += v - get_bottom(); }
93 Vector get_middle()
const {
return Vector(m_p1.x + m_size.width / 2.0f,
94 m_p1.y + m_size.height / 2.0f); }
96 void set_pos(
const Vector& v) { m_p1 = v; }
98 void set_width(
float width) { m_size.width = width; }
99 void set_height(
float height) { m_size.height = height; }
100 void set_size(
float width,
float height) { m_size =
Sizef(width, height); }
101 Sizef get_size()
const {
return m_size; }
103 void move(
const Vector& v) { m_p1 += v; }
106 bool contains(
const Vector& v)
const {
107 return v.x >= m_p1.x && v.y >= m_p1.y && v.x < get_right() && v.y < get_bottom();
110 bool contains(
const Rectf& other)
const 113 if (m_p1.x >= other.get_right() || other.get_left() >= get_right())
115 if (m_p1.y >= other.get_bottom() || other.get_top() >= get_bottom())
121 float distance (
const Vector& other, AnchorPoint ap = ANCHOR_MIDDLE)
const 123 Vector v = get_anchor_pos (*
this, ap);
124 return ((v - other).norm ());
127 float distance (
const Rectf& other, AnchorPoint ap = ANCHOR_MIDDLE)
const 129 Vector v1 = get_anchor_pos(*
this, ap);
130 Vector v2 = get_anchor_pos(other, ap);
132 return ((v1 - v2).norm ());
135 Rectf grown(
float border)
const 137 return Rectf(m_p1.x - border, m_p1.y - border,
138 get_right() + border, get_bottom() + border);
144 Vector p1()
const {
return m_p1; }
145 Vector p2()
const {
return Vector(m_p1.x + m_size.width, m_p1.y + m_size.height); }
147 void set_p1(
const Vector& p) {
148 m_size =
Sizef(m_size.width + (m_p1.x - p.x),
149 m_size.height + (m_p1.y - p.y));
152 void set_p2(
const Vector& p) {
153 m_size =
Sizef(p.x - m_p1.x,
163 std::ostream& operator<<(std::ostream& out,
const Rectf& rect);
Simple two dimensional vector.
Definition: vector.hpp:24