diff --git a/libs/main/math/piline.h b/libs/main/math/piline.h index f11e0701..e681f40b 100644 --- a/libs/main/math/piline.h +++ b/libs/main/math/piline.h @@ -34,6 +34,14 @@ #include "pipoint.h" + +/*! \brief + * \~english Two-dimensional line class + * \~russian Класс отрезка двумерной линии + * \~\details + * \~russian + * Этот класс описывает линию на плоскости в прямоугольной системе координат + */ template class PIP_EXPORT PILine { static_assert(std::is_arithmetic::value, "Type must be arithmetic"); @@ -41,82 +49,120 @@ public: PIPoint p0; PIPoint p1; + //! PILine() {} + //! PILine(const PIPoint & p0_, const PIPoint & p1_) { p0 = p0_; p1 = p1_; } + //! PILine(Type x0, Type y0, Type x1, Type y1) { p0.set(x0, y0); p1.set(x1, y1); } + //! PILine & set(const PIPoint & p0_, const PIPoint & p1_) { p0 = p0_; p1 = p1_; return *this; } + //! PILine & set(Type x0, Type y0, Type x1, Type y1) { p0.set(x0, y0); p1.set(x1, y1); return *this; } + //! bool isEmpty() const { return (p0 == p1); } + //! Type width() const {return piAbs(p1.x - p0.x);} + + //! Type height() const {return piAbs(p1.y - p0.y);} + //! PILine & translate(Type x, Type y) { p0.translate(x, y); p1.translate(x, y); return *this; } + + //! PILine & translate(const PIPoint & p) { p0.translate(p); p1.translate(p); return *this; } + + //! PILine translated(Type x, Type y) const { PILine l(*this); l.translate(x, y); return l; } + + //! PILine translated(const PIPoint & p) const { PILine l(*this); l.translate(p); return l; } + + //! PILine & move(Type x, Type y) {return translate(x, y);} + + //! PILine & move(const PIPoint & p) {return translate(p);} + + //! PILine moved(Type x, Type y) const { PILine l(*this); l.translate(x, y); return l; } + + //! PILine moved(const PIPoint & p) const { PILine l(*this); l.translate(p); return l; } + //! 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);} + + //! PILine operator +(const PIPoint & p) {return translated(p);} + + //! PILine operator -(const PIPoint & p) {return translated(-p);} + + //! bool operator ==(const PILine & r) const {return (p0 == r.p0 && p1 == r.p1);} + + //! bool operator !=(const PILine & r) const {return (p1 != r.p1 || p1 != r.p1);} }; - template PICout operator <<(PICout & s, const PILine & v) { s.setControl(0, true); diff --git a/libs/main/math/pipoint.h b/libs/main/math/pipoint.h index 401518a4..5b9634b6 100644 --- a/libs/main/math/pipoint.h +++ b/libs/main/math/pipoint.h @@ -1,16 +1,15 @@ -/*! \file pipoint.h - * \ingroup Math - * \brief - * \~english Two-dimensional point class - * \~russian Класс двумерной точки - * \~\authors - * \~english - * Ivan Pelipenko peri4ko@yandex.ru; - * Andrey Bychkov work.a.b@yandex.ru; - * \~russian - * Иван Пелипенко peri4ko@yandex.ru; - * Андрей Бычков work.a.b@yandex.ru; -*/ +//! \file pipoint.h +//! \ingroup Math +//! \brief +//! \~english Two-dimensional point class +//! \~russian Класс двумерной точки +//! \~\authors +//! \~english +//! Ivan Pelipenko peri4ko@yandex.ru; +//! Andrey Bychkov work.a.b@yandex.ru; +//! \~russian +//! Иван Пелипенко peri4ko@yandex.ru; +//! Андрей Бычков work.a.b@yandex.ru; /* PIP - Platform Independent Primitives Two-dimensional point class @@ -31,10 +30,10 @@ #include "pimathbase.h" -/*! \brief - * \~english Two-dimensional point class - * \~russian Класс двумерной точки - */ + +//! \brief +//! \~english Two-dimensional point class +//! \~russian Класс двумерной точки template class PIP_EXPORT PIPoint { static_assert(std::is_arithmetic::value, "Type must be arithmetic"); @@ -42,65 +41,109 @@ public: Type x; Type y; + //! PIPoint() {x = y = Type();} + + //! PIPoint(Type x_, Type y_) {set(x_, y_);} + //! PIPoint & set(Type x_, Type y_) { x = x_; y = y_; return *this; } + + //! PIPoint & set(const PIPoint & p) { x = p.x; y = p.y; return *this; } + + //! PIPoint & translate(Type x_, Type y_) { x += x_; y += y_; return *this; } + + //! PIPoint & translate(const PIPoint & p) { x += p.x; y += p.y; return *this; } + + //! PIPoint translated(Type x_, Type y_) const { PIPoint rp(*this); rp.translate(x_, y_); return rp; } + + //! PIPoint translated(const PIPoint & p) const { PIPoint rp(*this); rp.translate(p); return rp; } + + //! PIPoint & move(Type x_, Type y_) {return translate(x_, y_);} + + //! PIPoint & move(const PIPoint & p) {return translate(p);} + + //! PIPoint moved(Type x_, Type y_) const { PIPoint rp(*this); rp.translate(x_, y_); return rp; } + + //! PIPoint moved(const PIPoint & p) const { PIPoint rp(*this); rp.translate(p); return rp; } + + //! double angleRad() const {return atan2(y, x);} + + //! double angleDeg() const {return toDeg(atan2(y, x));} + + //! PIPoint toPolar(bool isDeg = false) const {return PIPoint(sqrt(x*x + y*y), isDeg ? angleDeg() : angleRad());} + + //! static PIPoint fromPolar(const PIPoint & p) {return PIPoint(p.y * cos(p.x), p.y * sin(p.x));} + //! void operator +=(const PIPoint & p) {translate(p);} - PIPoint operator +(const PIPoint & p) {return PIPoint(x + p.x, y + p.y);} - PIPoint operator +(const Type & p) {return PIPoint(x + p, y + p);} - PIPoint operator -(const PIPoint & p) {return PIPoint(x - p.x, y - p.y);} - PIPoint operator -(const Type & p) {return PIPoint(x - p, y - p);} - PIPoint operator -() {return PIPoint(-x, -y);} - bool operator ==(const PIPoint & p) const {return (x == p.x && y == p.y);} - bool operator !=(const PIPoint & p) const {return (x != p.x || y != p.y);} + //! + PIPoint operator +(const PIPoint & p) {return PIPoint(x + p.x, y + p.y);} + + //! + PIPoint operator +(const Type & p) {return PIPoint(x + p, y + p);} + + //! + PIPoint operator -(const PIPoint & p) {return PIPoint(x - p.x, y - p.y);} + + //! + PIPoint operator -(const Type & p) {return PIPoint(x - p, y - p);} + + //! + PIPoint operator -() {return PIPoint(-x, -y);} + + //! + bool operator ==(const PIPoint & p) const {return (x == p.x && y == p.y);} + + //! + bool operator !=(const PIPoint & p) const {return (x != p.x || y != p.y);} }; diff --git a/libs/main/math/pirect.h b/libs/main/math/pirect.h index 6f82cdad..91b78b75 100644 --- a/libs/main/math/pirect.h +++ b/libs/main/math/pirect.h @@ -1,16 +1,15 @@ -/*! \file pirect.h - * \ingroup Math - * \brief - * \~english Rect class - * \~russian Класс прямоугольника - * \~\authors - * \~english - * Ivan Pelipenko peri4ko@yandex.ru; - * Andrey Bychkov work.a.b@yandex.ru; - * \~russian - * Иван Пелипенко peri4ko@yandex.ru; - * Андрей Бычков work.a.b@yandex.ru; -*/ +//! \file pirect.h +//! \ingroup Math +//! \brief +//! \~english Rect class +//! \~russian Класс прямоугольника +//! \~\authors +//! \~english +//! Ivan Pelipenko peri4ko@yandex.ru; +//! Andrey Bychkov work.a.b@yandex.ru; +//! \~russian +//! Иван Пелипенко peri4ko@yandex.ru; +//! Андрей Бычков work.a.b@yandex.ru; /* PIP - Platform Independent Primitives Rect class @@ -34,121 +33,168 @@ #include "pipoint.h" -/*! \brief - * \~english Rect class - * \~russian Класс прямоугольника - * \~\details - * \~russian - * Этот класс описывает прямоугольник на плоскости в прямоугольной системе координат - */ + +//! \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 Конструктор прямоугольника из координат левого нижнего угла и размеров ширины и высоты - */ + //! \brief + //! \~russian Конструктор прямоугольника из координат левого нижнего угла и размеров ширины и высоты PIRect(Type left_, Type bottom_, Type width_, Type height_) { set(left_, bottom_, width_, height_); normalize(); } - /*! \brief - * \~russian Конструктор прямоугольника из координат левого нижнего угла и правого верхнего угла - */ + + //! \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()); @@ -156,11 +202,15 @@ public: 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()); @@ -169,54 +219,125 @@ public: 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: @@ -224,6 +345,7 @@ private: PIPoint tr; }; + template PICout operator <<(PICout & s, const PIRect & v) { s.setControl(0, true);