34 #include <Pt/Gfx/Point.h>
35 #include <Pt/Gfx/Size.h>
62 Rect(Float width, Float height)
70 , _s(p2.x() - p1.x(), p2.y() - p1.y())
84 return (_s.width() == 0 || _s.height() == 0);
89 static Rect fromLTRB(Float left, Float top, Float right, Float bottom)
91 return Rect(
Point(left, top),
Size(right - left, bottom - top));
107 void set(
const Point& p,
const Size& s)
113 void set(
const Point& p1,
const Point& p2)
116 this->setWidth(p2.x() - p1.x());
117 this->setHeight(p2.y() - p1.y());
120 void set(Float width, Float height)
123 _s.set(width, height);
126 const Point& origin()
const
131 void setOrigin(
const Point& p)
136 void setSize(
const Size& s)
141 void setWidth(Float w)
146 void setHeight(Float h)
161 const Size& size()
const
188 return _p.x() + _s.width();
193 return _p.y() + _s.height();
196 const Point& topLeft()
const
201 Point topRight()
const
203 return Point(this->x() + this->width(), this->y());
206 Point bottomLeft()
const
208 return Point(this->x(), this->y() + this->height());
211 Point bottomRight()
const
213 return Point(this->x() + this->width(),
214 this->y() + this->height());
217 bool isEqual(
const Rect& other, Float eps = FloatNearlyZero)
const
219 return _p.isEqual(other._p, eps) && _s.isEqual(other._s, eps);
222 void move(Float dx, Float dy)
228 void expand(Float dw, Float dh)
234 void shrink(Float dw, Float dh)
240 void unify(
const Rect& rect)
252 const Float l = std::min(this->left(), rect.left());
253 const Float t = std::min(this->top(), rect.top());
254 const Float r = std::max(this->right(), rect.right());
255 const Float b = std::max(this->bottom(), rect.bottom());
258 _s = Size(r - l, b - t);
265 const Float l = std::max(this->left(), rect.left());
266 const Float t = std::max(this->top(), rect.top());
267 const Float r = std::min(this->right(), rect.right());
268 const Float b = std::min(this->bottom(), rect.bottom());
278 return p.x() >= _p.x() &&
279 p.x() < _p.x() + _s.width() &&
281 p.y() < _p.y() + _s.height();
288 return r.left() >= this->left() &&
289 r.right() <= this->right() &&
290 r.top() >= this->top() &&
291 r.bottom() <= this->bottom();
298 return this->right() > r.left() &&
299 r.right() > this->left() &&
300 this->bottom() > r.top() &&
301 r.bottom() > this->top();
308 return Point(_p.x() + _s.width() / 2, _p.y() + _s.height() / 2);
318 _s.setWidth(-_s.width());
322 _p.addY(_s.height());
323 _s.setHeight(-_s.height());
389 RectI(Int width, Int height)
397 , _s(p2.x() - p1.x(), p2.y() - p1.y())
411 return (_s.width() == 0 || _s.height() == 0);
434 void set(
const PointI& p,
const SizeI& s)
440 void set(
const PointI& p1,
const PointI& p2)
443 this->setWidth(p2.x() - p1.x());
444 this->setHeight(p2.y() - p1.y());
447 void set(Int width, Int height)
450 _s.set(width, height);
453 const PointI& origin()
const
458 void setOrigin(
const PointI& p)
463 void setSize(
const SizeI& s)
473 void setHeight(Int h)
488 const SizeI& size()
const
515 return _p.x() + _s.width();
520 return _p.y() + _s.height();
523 const PointI& topLeft()
const
528 PointI topRight()
const
530 return PointI(this->x() + this->width(), this->y());
533 PointI bottomLeft()
const
535 return PointI(this->x(), this->y() + this->height());
538 PointI bottomRight()
const
540 return PointI(this->x() + this->width(),
541 this->y() + this->height());
544 bool operator==(
const RectI& other)
const
546 return _p == other._p && _s == other._s;
549 bool operator!=(
const RectI& other)
const
551 return _p != other._p || _s != other._s;
554 void move(Int dx, Int dy)
560 void expand(Int dw, Int dh)
566 void shrink(Int dw, Int dh)
572 void unify(
const RectI& rect)
584 const Int l = std::min(this->left(), rect.left());
585 const Int t = std::min(this->top(), rect.top());
586 const Int r = std::max(this->right(), rect.right());
587 const Int b = std::max(this->bottom(), rect.bottom());
590 _s = SizeI(r - l, b - t);
597 const Int l = std::max(this->left(), rect.left());
598 const Int t = std::max(this->top(), rect.top());
599 const Int r = std::min(this->right(), rect.right());
600 const Int b = std::min(this->bottom(), rect.bottom());
610 return p.x() >= _p.x() &&
611 p.x() < _p.x() + _s.width() &&
613 p.y() < _p.y() + _s.height();
620 return r.left() >= this->left() &&
621 r.right() <= this->right() &&
622 r.top() >= this->top() &&
623 r.bottom() <= this->bottom();
630 return this->right() > r.left() &&
631 r.right() > this->left() &&
632 this->bottom() > r.top() &&
633 r.bottom() > this->top();
640 return PointI(_p.x() + _s.width() / 2, _p.y() + _s.height() / 2);
650 _s.setWidth(-_s.width());
654 _p.addY(_s.height());
655 _s.setHeight(-_s.height());
702 const Int l =
static_cast<Int
>(std::floor(this->left()));
703 const Int t =
static_cast<Int
>(std::floor(this->top()));
704 const Int r =
static_cast<Int
>(std::ceil(this->right()));
705 const Int b =
static_cast<Int
>(std::ceil(this->bottom()));
711 const Int l =
static_cast<Int
>(std::ceil(this->left()));
712 const Int t =
static_cast<Int
>(std::ceil(this->top()));
713 const Int r =
static_cast<Int
>(std::floor(this->right()));
714 const Int b =
static_cast<Int
>(std::floor(this->bottom()));