22 #ifndef QDENGINE_MINIGAMES_ADV_RECT_H 23 #define QDENGINE_MINIGAMES_ADV_RECT_H 25 #include "qdengine/xmath.h" 26 #include "qdengine/minigames/adv/Range.h" 40 template<
typename scalar_type,
class vect_type>
42 typedef vect_type VectType;
43 typedef scalar_type ScalarType;
51 _width(ScalarType(0)),
52 _height(ScalarType(0)) {}
61 Rect(ScalarType left, ScalarType top, ScalarType width, ScalarType height) :
67 Rect(
const VectType& _topleft,
const VectType&
size) :
73 void set(ScalarType left, ScalarType top, ScalarType width, ScalarType height) {
80 inline ScalarType left()
const {
83 inline ScalarType top()
const {
86 inline ScalarType width()
const {
89 inline ScalarType height()
const {
93 VectType _lefttop()
const {
94 return VectType(_left, _top);
96 VectType right_top()
const {
97 return VectType(_left + _width, _top);
99 VectType _leftbottom()
const {
100 return VectType(_left, _top + _height);
102 VectType right_bottom()
const {
103 return VectType(_left + _width, _top + _height);
107 inline ScalarType right()
const {
108 return _left + _width;
110 inline ScalarType bottom()
const {
111 return _top + _height;
121 return VectType(_left + _width / ScalarType(2),
122 _top + _height / ScalarType(2));
126 return VectType(_width, _height);
130 inline void left(ScalarType left) {
133 inline void top(ScalarType top) {
136 inline void width(ScalarType width) {
139 inline void height(ScalarType height) {
144 inline void right(ScalarType right) {
145 _left = right - _width;
147 inline void bottom(ScalarType bottom) {
148 _top = bottom - _height;
153 _left = center.x - _width / ScalarType(2);
154 _top = center.y - _height / ScalarType(2);
164 inline void size(
const VectType& size) {
173 if (point.x >= left() && point.y >= top() &&
174 point.x <= right() && point.y <= bottom())
181 if (rect.left() >= left() && rect.top() >= top() &&
182 rect.bottom() <= bottom() && rect.right() <= right())
188 inline bool rect_overlap(
const RectType& rect)
const {
189 if (left() > rect.right() || right() < rect.left()
190 || top() > rect.bottom() || bottom() < rect.top())
201 inline RectType
scaled(
const VectType&
scale,
const VectType& origin)
const {
202 return (*
this - origin) * scale + origin;
207 if (width() < ScalarType(0)) {
208 left(left() + width());
211 if (height() < ScalarType(0)) {
212 top(top() + height());
217 inline RectType intersection(
const RectType& rect)
const {
218 RangeType xRange = RangeType(left(), right()).intersection(RangeType(rect.left(), rect.right()));
219 RangeType yRange = RangeType(top(), bottom()).intersection(RangeType(rect.top(), rect.bottom()));
220 return RectType(xRange.minimum(), yRange.minimum(), xRange.length(), yRange.length());
224 RectType operator+(
const VectType& point)
const {
225 return RectType(left() + point.x, top() + point.y,
229 RectType operator-(
const VectType& point)
const {
230 return RectType(left() - point.x, top() - point.y,
234 RectType operator*(
const VectType& point)
const {
235 return RectType(left() * point.x, top() * point.y,
236 width() * point.x, height() * point.y);
239 RectType operator*(
const RectType& rhs)
const {
240 VectType leftTop(left() + width() * rhs.left(), top() + height() * rhs.top());
242 return RectType(leftTop, size);
245 RectType operator/(
const RectType& rhs)
const {
246 VectType leftTop((left() - rhs.left()) / rhs.width(), (top() - rhs.top()) / rhs.height());
247 VectType
size(width() / rhs.width(), height() / rhs.height());
248 return RectType(leftTop, size);
251 RectType operator/(
const VectType& point)
const {
252 return RectType(left() / point.x, top() / point.y,
253 width() / point.x, height() / point.y);
256 bool operator==(
const RectType& rect)
const {
257 return (_left == rect._left && _top == rect._top &&
258 _width == rect._width && _height == rect._height);
261 bool eq(
const RectType& rect, ScalarType eps = FLT_COMPARE_TOLERANCE)
const {
262 return (abs(_left - rect._left) < eps && abs(_top - rect._top) < eps &&
263 abs(_width - rect._width) < eps && abs(_height - rect._height) < eps);
266 bool operator!=(
const RectType& rect)
const {
267 return (_left != rect._left || _top != rect._top ||
268 _width != rect._width || _height != rect._height);
280 template<
class ST,
class VT>
281 operator ::Rect<ST, VT>()
const {
282 return ::Rect<ST, VT>(
static_cast<ST
>(left()),
283 static_cast<ST>(top()),
284 static_cast<ST>(width()),
285 static_cast<ST>(height()));
289 bool clipLine(VectType& pos0, VectType& pos1)
const;
292 template<
typename ScalarType,
class VectType>
294 VectType p0(pos0), p1(pos1);
303 float t[4] = {-1.0f, -1.0f, -1.0f, -1.0f};
305 ScalarType dx = p1.x - p0.x;
306 ScalarType dy = p1.y - p0.y;
311 tc = (float)(top() - p0.y) / dy;
312 if (tc >= 0.0f && tc <= 1.0f) {
313 crd = p0.x + tc * dx;
314 if (crd >= left() && crd <= right())
318 tc = (float)(bottom() - p0.y) / dy;
319 if (tc >= 0.0f && tc <= 1.0f) {
320 crd = p0.x + tc * dx;
321 if (crd >= left() && crd <= right())
327 tc = (float)(left() - p0.x) / dx;
328 if (tc >= 0.0f && tc <= 1.0f) {
329 crd = p0.y + tc * dy;
330 if (crd >= top() && crd <= bottom())
334 tc = (float)(right() - p0.x) / dx;
335 if (tc >= 0.0f && tc <= 1.0f) {
336 crd = p0.y + tc * dy;
337 if (crd >= top() && crd <= bottom())
343 pos1.set(p0.x + t[0]*dx, p0.y + t[0]*dy);
344 pos0.set(p0.x, p0.y);
346 pos0.set(p0.x + t[0]*dx, p0.y + t[0]*dy);
347 pos1.set(p1.x, p1.y);
350 pos0.set(p0.x + t[0]*dx, p0.y + t[0]*dy);
351 pos1.set(p0.x + t[1]*dx, p0.y + t[1]*dy);
353 pos1.set(p0.x + t[0]*dx, p0.y + t[0]*dy);
354 pos0.set(p0.x + t[1]*dx, p0.y + t[1]*dy);
364 #endif // QDENGINE_MINIGAMES_ADV_RECT_H RectType scaled(const VectType &scale, const VectType &origin) const
Производит скэлинг.
Definition: Rect.h:201
bool rect_inside(const RectType &rect) const
Проверяет не находится ли прямоугольник _rect внутри прямоугольника
Definition: Rect.h:180
void center(const VectType ¢er)
Переносит центр прямоугольника в точку _center не изменяя его размер.
Definition: Rect.h:152
VectType size() const
Возвращает размер прямоугольника.
Definition: Rect.h:125
void size(const VectType &size)
Устанавливает новые размеры, сохраняя левый-верхний угол в преждней точке.
Definition: Rect.h:164
In find(In first, In last, const T &v)
Definition: algorithm.h:225
void validate()
Исправляет отрицательную ширину/высоту
Definition: Rect.h:206
Graphics::Surface * scale(const Graphics::Surface &srcImage, int xSize, int ySize)
Базовый класс для игровых ресурсов.
Definition: console.h:28
bool point_inside(const VectType &point) const
Проверяет не находится ли точка _point внутри прямоугольника
Definition: Rect.h:172
VectType center() const
Возвращает координаты цетра прямоугольника.
Definition: Rect.h:120
Rect(const VectType &size)
Создаёт Rect размера _size, левый-верхний угол остаётся в точке (0, 0).
Definition: Rect.h:55
Абстрактый прямоугольник.
Definition: Rect.h:41