libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
rect.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <algorithm>
12#include <promeki/core/point.h>
13#include <promeki/core/size2d.h>
14
16
34template <typename T> class Rect {
35 public:
37 Rect() = default;
38
41 : _pos(pos), _size(size) {}
42
45 : _pos(x, y), _size(width, height) {}
46
48 T x() const { return _pos.x(); }
49
51 T y() const { return _pos.y(); }
52
54 T width() const { return _size.width(); }
55
57 T height() const { return _size.height(); }
58
60 void setX(T val) { _pos.setX(val); }
61
63 void setY(T val) { _pos.setY(val); }
64
66 void setWidth(T val) { _size.setWidth(val); }
67
69 void setHeight(T val) { _size.setHeight(val); }
70
72 Point<T, 2> topLeft() const { return _pos; }
73
75 Point<T, 2> topRight() const { return Point<T, 2>(x() + width(), y()); }
76
78 Point<T, 2> bottomLeft() const { return Point<T, 2>(x(), y() + height()); }
79
81 Point<T, 2> bottomRight() const { return Point<T, 2>(x() + width(), y() + height()); }
82
84 Point<T, 2> center() const { return Point<T, 2>(x() + width() / 2, y() + height() / 2); }
85
87 const Point<T, 2> &pos() const { return _pos; }
88
90 const Size2DTemplate<T> &size() const { return _size; }
91
93 void setPos(const Point<T, 2> &p) { _pos = p; }
94
96 void setSize(const Size2DTemplate<T> &s) { _size = s; }
97
99 bool isValid() const { return _size.isValid(); }
100
102 bool isEmpty() const { return !isValid(); }
103
105 bool contains(const Point<T, 2> &p) const {
106 return p.x() >= x() && p.x() < x() + width() &&
107 p.y() >= y() && p.y() < y() + height();
108 }
109
111 bool contains(const Rect &r) const {
112 return r.x() >= x() && r.x() + r.width() <= x() + width() &&
113 r.y() >= y() && r.y() + r.height() <= y() + height();
114 }
115
117 bool intersects(const Rect &r) const {
118 return x() < r.x() + r.width() && x() + width() > r.x() &&
119 y() < r.y() + r.height() && y() + height() > r.y();
120 }
121
123 Rect intersected(const Rect &r) const {
124 T ix = std::max(x(), r.x());
125 T iy = std::max(y(), r.y());
126 T ix2 = std::min(x() + width(), r.x() + r.width());
127 T iy2 = std::min(y() + height(), r.y() + r.height());
128 if(ix2 <= ix || iy2 <= iy) return Rect();
129 return Rect(ix, iy, ix2 - ix, iy2 - iy);
130 }
131
133 Rect united(const Rect &r) const {
134 if(isEmpty()) return r;
135 if(r.isEmpty()) return *this;
136 T ux = std::min(x(), r.x());
137 T uy = std::min(y(), r.y());
138 T ux2 = std::max(x() + width(), r.x() + r.width());
139 T uy2 = std::max(y() + height(), r.y() + r.height());
140 return Rect(ux, uy, ux2 - ux, uy2 - uy);
141 }
142
144 Rect adjusted(T dx1, T dy1, T dx2, T dy2) const {
145 return Rect(x() + dx1, y() + dy1,
146 width() + dx2 - dx1, height() + dy2 - dy1);
147 }
148
150 Rect translated(T dx, T dy) const {
151 return Rect(x() + dx, y() + dy, width(), height());
152 }
153
155 bool operator==(const Rect &other) const {
156 return _pos == other._pos && _size.width() == other._size.width() &&
157 _size.height() == other._size.height();
158 }
159
161 bool operator!=(const Rect &other) const {
162 return !(*this == other);
163 }
164
165 private:
166 Point<T, 2> _pos;
167 Size2DTemplate<T> _size;
168};
169
176
Dynamic array container wrapping std::vector.
Definition list.h:40
2D rectangle defined by position and size.
Definition rect.h:34
void setWidth(T val)
Sets the width.
Definition rect.h:66
const Size2DTemplate< T > & size() const
Returns the size.
Definition rect.h:90
Rect adjusted(T dx1, T dy1, T dx2, T dy2) const
Returns a rectangle adjusted by the given deltas.
Definition rect.h:144
void setSize(const Size2DTemplate< T > &s)
Sets the size.
Definition rect.h:96
T height() const
Returns the height.
Definition rect.h:57
bool operator!=(const Rect &other) const
Inequality operator.
Definition rect.h:161
void setPos(const Point< T, 2 > &p)
Sets the position.
Definition rect.h:93
Rect(const Point< T, 2 > &pos, const Size2DTemplate< T > &size)
Constructs a rectangle from position and size.
Definition rect.h:40
void setHeight(T val)
Sets the height.
Definition rect.h:69
Point< T, 2 > bottomLeft() const
Returns the bottom-left position.
Definition rect.h:78
bool operator==(const Rect &other) const
Equality operator.
Definition rect.h:155
T width() const
Returns the width.
Definition rect.h:54
bool intersects(const Rect &r) const
Returns true if the given rectangle overlaps this rectangle.
Definition rect.h:117
bool contains(const Point< T, 2 > &p) const
Returns true if the given point is inside this rectangle.
Definition rect.h:105
void setY(T val)
Sets the Y coordinate.
Definition rect.h:63
bool contains(const Rect &r) const
Returns true if the given rectangle is entirely inside this rectangle.
Definition rect.h:111
Rect translated(T dx, T dy) const
Returns a rectangle translated by dx, dy.
Definition rect.h:150
T x() const
Returns the X coordinate of the left edge.
Definition rect.h:48
bool isValid() const
Returns true if both width and height are greater than zero.
Definition rect.h:99
Point< T, 2 > topLeft() const
Returns the top-left position.
Definition rect.h:72
Rect intersected(const Rect &r) const
Returns the intersection of this rectangle with another.
Definition rect.h:123
Point< T, 2 > center() const
Returns the center point.
Definition rect.h:84
Point< T, 2 > topRight() const
Returns the top-right position.
Definition rect.h:75
T y() const
Returns the Y coordinate of the top edge.
Definition rect.h:51
Rect(T x, T y, T width, T height)
Constructs a rectangle from individual components.
Definition rect.h:44
void setX(T val)
Sets the X coordinate.
Definition rect.h:60
bool isEmpty() const
Returns true if width or height is zero or negative.
Definition rect.h:102
Rect united(const Rect &r) const
Returns the smallest rectangle containing both this and another.
Definition rect.h:133
const Point< T, 2 > & pos() const
Returns the position (top-left).
Definition rect.h:87
Point< T, 2 > bottomRight() const
Returns the bottom-right position.
Definition rect.h:81
Rect()=default
Default constructor. Creates an empty rectangle at origin.
#define PROMEKI_NAMESPACE_BEGIN
Starts a promeki namespace block.
Definition namespace.h:14
#define PROMEKI_NAMESPACE_END
Ends a promeki namespace block.
Definition namespace.h:19