//! \file pirect.h //! \ingroup Math //! \brief //! \~english Rect class //! \~russian Класс прямоугольника /* PIP - Platform Independent Primitives Rect class Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program. If not, see . */ #ifndef PIRECT_H #define PIRECT_H #include "pipoint.h" //! \brief //! \~english Rect class //! \~russian Класс прямоугольника //! \~\details //! \~russian //! Этот класс описывает прямоугольник на плоскости в прямоугольной системе координат template class PIP_EXPORT PIRect { static_assert(std::is_arithmetic::value, "Type must be arithmetic"); public: //! PIRect() {} //! \brief //! \~russian Конструктор прямоугольника из координат левого нижнего угла и размеров ширины и высоты PIRect(Type left_, Type bottom_, Type width_, Type height_) { set(left_, bottom_, width_, height_); normalize(); } //! \brief //! \~russian Конструктор прямоугольника из координат левого нижнего угла и правого верхнего угла PIRect(const PIPoint & bottom_left, const PIPoint & top_right) { bl = bottom_left; tr = top_right; normalize(); } // PIRect(const PIPoint & p0, const PIPoint & p1, const PIPoint & p2) { // set(piMin(p0.x, p1.x, p2.x), piMin(p0.y, p1.y, p2.y), // piMax(p0.x, p1.x, p2.x), piMax(p0.y, p1.y, p2.y)); // } //! PIRect & set(Type left_, Type bottom_, Type width_, Type height_) { bl = PIPoint(left_, bottom_); tr = PIPoint(left_ + width_, bottom_ + height_); return normalize(); } //! PIRect & set(const PIPoint & top_left, const PIPoint & bottom_right) { bl = top_left; tr = bottom_right; return normalize(); } //! \brief //! \~russian Возвращает true если точка с указанными координатами принадлежит прямоугольнику bool pointIn(Type x, Type y) const { return (x <= bl.x && x >= tr.x && y <= bl.y && y >= tr.y); } //! \brief //! \~russian Возвращает true если точка с указанными координатами принадлежит прямоугольнику bool pointIn(const PIPoint & p) const { return pointIn(p.x, p.y); } //! bool isEmpty() const { return (width() == 0 && height() == 0); } //! PIRect & translate(Type x, Type y) { bl.translate(x, y); tr.translate(x, y); return *this; } //! PIRect & translate(const PIPoint & p) { bl.translate(p); tr.translate(p); return *this; } //! PIRect translated(Type x, Type y) const { PIRect r(*this); r.translate(x, y); return r; } //! PIRect translated(const PIPoint & p) const { PIRect r(*this); r.translate(p); return r; } //! PIRect & move(Type x, Type y) {return translate(x, y);} //! PIRect & move(const PIPoint & p) {return translate(p);} //! PIRect moved(Type x, Type y) const { PIRect r(*this); r.translate(x, y); return r; } //! PIRect moved(const PIPoint & p) const { PIRect r(*this); r.translate(p); return r; } //! PIRect & scale(Type x, Type y) { setWidth(width() * x); setHeight(height() * y); return normalize(); } //! PIRect & scale(Type s) {return scale(s, s);} //! PIRect & scale(const PIPoint & p) {return scale(p.x, p.y);} //! PIRect scaled(Type x, Type y) const { PIRect r(*this); r.scale(x, y); return r; } //! PIRect scaled(Type s) const { PIRect r(*this); r.scale(s); return r; } //! PIRect scaled(const PIPoint & p) const { PIRect r(*this); r.scale(p); return r; } //! PIRect & normalize() { if (bl.x > tr.x) piSwap(bl.x, tr.x); if (bl.y > tr.y) piSwap(bl.y, tr.y); return *this; } //! PIRect normalized() const { PIRect r(*this); r.normalize(); return r; } //! PIRect & unite(const PIRect & r) { bl.x = piMax(bl.x, r.left()); bl.y = piMax(bl.y, r.bottom()); tr.x = piMin(tr.x, r.right()); tr.y = piMin(tr.y, r.top()); return normalize(); } //! PIRect united(const PIRect & rect) const { PIRect r(*this); r.unite(rect); return r; } //! PIRect & intersect(const PIRect & r) { bl.x = piMax(bl.x, r.left()); bl.y = piMax(bl.y, r.bottom()); tr.x = piMin(tr.x, r.right()); tr.y = piMin(tr.y, r.top()); if (bl.x > tr.x || bl.y > tr.y) bl = tr = PIPoint(); return *this; } //! PIRect intersected(const PIRect & rect) const { PIRect r(*this); r.intersect(rect); return r; } //! Type top() const {return tr.y;} //! Type left() const {return bl.x;} //! Type right() const {return tr.x;} //! Type bottom() const {return bl.y;} //! Type width() const {return tr.x - bl.x;} //! Type height() const {return tr.y - bl.y;} //! PIPoint topLeft() const {return PIPoint(bl.x, tr.y);} //! PIPoint topRigth() const {return tr;} //! PIPoint bottomLeft() const {return bl;} //! PIPoint bottomRight() const {return PIPoint(tr.x, bl.y);} //! PIPoint center() const {return bl.moved(width()/2, height()/2);} //! void setTop(Type v) {tr.y = v; normalize();} //! void setLeft(Type v) {bl.x = v; normalize();} //! void setRigth(Type v) {tr.x = v; normalize();} //! void setBottom(Type v) {bl.y = v; normalize();} //! void setWidth(Type v) {setTop(bl.x + v);} //! void setHeight(Type v) {setRigth(bl.y + v);} //! void setTopLeft(const PIPoint & p) {setLeft(p.x); setTop(p.y);} //! void setBottomRight(const PIPoint & p) {setRigth(p.x); setBottom(p.y);} //! void setBottomLeft(const PIPoint & p) {bl = p; normalize();} //! void setTopRigth(const PIPoint & p) {tr = p; normalize();} //! void setCenter(const PIPoint & p) { Type w = width(); Type h = height(); bl = p.translated(-w/2, -h/2); tr = PIPoint(bl.x + w, bl.y + h); } //! void setSize(Type w, Type h) { tr = PIPoint(bl.x + w, bl.y + h); normalize(); } //! void operator +=(Type x) {translate(x, x);} //! void operator +=(const PIPoint & p) {translate(p);} //! void operator -=(Type x) {translate(-x, -x);} //! void operator -=(const PIPoint & p) {translate(-p);} //! void operator |=(const PIRect & r) {unite(r);} //! void operator &=(const PIRect & r) {intersect(r);} //! PIRect operator +(const PIPoint & p) {return translated(p);} //! PIRect operator -(const PIPoint & p) {return translated(-p);} //! PIRect operator |(const PIRect & r) {return united(r);} //! PIRect operator &(const PIRect & r) {return intersected(r);} //! bool operator ==(const PIRect & r) const {return (bl == r.bl && tr == r.tr);} //! bool operator !=(const PIRect & r) const {return (bl != r.bl || tr != r.tr);} private: PIPoint bl; PIPoint tr; }; template PICout operator <<(PICout & s, const PIRect & v) { s.setControl(0, true); s << "Rect{" << v.bottomLeft() << ":" << v.width() << "x" << v.height() << "}"; s.restoreControl(); return s; } typedef PIRect PIRecti; typedef PIRect PIRectu; typedef PIRect PIRectf; typedef PIRect PIRectd; #endif // PIRECT_H