/*! \file piline.h * \ingroup Math * \brief * \~english Two-dimensional line class * \~russian Класс отрезка двумерной линии */ /* PIP - Platform Independent Primitives Two-dimensional line 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 PILINE_H #define PILINE_H #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"); public: PIPoint p0; PIPoint p1; //! \~english Default constructor. //! \~russian Пустой конструктор. //! \details //! \~english Creates an empty line where start and end points coincide. //! \~russian При выполнении пустого конструктора координаты не изменяются. Начало и конец совпадают. PILine() {} //! \~english Creates a line from two points. //! \~russian Создает линию по двум принятым точкам \a PIPoint начала и конца. PILine(const PIPoint & p0_, const PIPoint & p1_) { p0 = p0_; p1 = p1_; } //! \~english Creates a line from coordinates. //! \~russian Создает линию по принятым координатам начала и конца. PILine(Type x0, Type y0, Type x1, Type y1) { p0.set(x0, y0); p1.set(x1, y1); } //! \~english Set new coordinates from two points. //! \~russian Задать новые координаты начала и конца по двум принятым точкам \a PIPoint. PILine & set(const PIPoint & p0_, const PIPoint & p1_) { p0 = p0_; p1 = p1_; return *this; } //! \~english Set new coordinates. //! \~russian Задать новые координаты начала и конца. PILine & set(Type x0, Type y0, Type x1, Type y1) { p0.set(x0, y0); p1.set(x1, y1); return *this; } //! \~english Check if start and end points coincide. //! \~russian Проверить на совпадение координат начала и конца. bool isEmpty() const { return (p0 == p1); } //! \~english Calculate width of the rectangle whose diagonal is this line. //! \~russian Вычислить ширину прямоугольника, диагональю которого является данный отрезок. Type width() const { return piAbs(p1.x - p0.x); } //! \~english Calculate height of the rectangle whose diagonal is this line. //! \~russian Вычислить высоту прямоугольника, диагональю которого является данный отрезок. Type height() const { return piAbs(p1.y - p0.y); } //! \~english Translate line by x and y. //! \~russian Сдвинуть линию на \a x, \a y. PILine & translate(Type x, Type y) { p0.translate(x, y); p1.translate(x, y); return *this; } //! \~english Translate line by point coordinates. //! \~russian Сдвинуть линию на значение координат точки \a PIPoint. PILine & translate(const PIPoint & p) { p0.translate(p); p1.translate(p); return *this; } //! \~english Create a copy of the line and translate it by x and y. //! \~russian Создать копию отрезка и сдвинуть её на `x` и `y`. PILine translated(Type x, Type y) const { PILine l(*this); l.translate(x, y); return l; } //! \~english Create a copy of the line and translate it by point coordinates. //! \~russian Создать копию отрезка и сдвинуть её на значение координат точки \a PIPoint. PILine translated(const PIPoint & p) const { PILine l(*this); l.translate(p); return l; } //! \~english Translate line by x and y. Alias for \a translate(). //! \~russian Сдвинуть линию на \a x, \a y. //! \details Является копией метода \a translate(). PILine & move(Type x, Type y) { return translate(x, y); } //! \~english Translate line by point coordinates. Alias for \a translate(). //! \~russian Сдвинуть линию на значение координат точки \a PIPoint. //! \details Является копией метода \a translate(). PILine & move(const PIPoint & p) { return translate(p); } //! \~english Create a copy of the line and translate it by x and y. Alias for \a translated(). //! \~russian Создать копию отрезка и сдвинуть её на \a x, \a y. //! \details Является копией метода \a translated(). PILine moved(Type x, Type y) const { PILine l(*this); l.translate(x, y); return l; } //! \~english Create a copy of the line and translate it by point coordinates. Alias for \a translated(). //! \~russian Создать копию отрезка и сдвинуть её на значение координат точки \a PIPoint. //! \details Является копией метода \a translated(). PILine moved(const PIPoint & p) const { PILine l(*this); l.translate(p); return l; } //! \~english Translate line by x value on both coordinates. //! \~russian Сдвинуть линию по двум координатам на значение \a x. void operator+=(Type x) { translate(x, x); } //! \~english Translate line by point coordinates. //! \~russian Сдвинуть линию по двум координатам на величину координат точки \a PIPoint. void operator+=(const PIPoint & p) { translate(p); } //! \~english Translate line by negative x value on both coordinates. //! \~russian Сдвинуть линию по двум координатам на значение \a x. void operator-=(Type x) { translate(-x, -x); } //! \~english Translate line by negative point coordinates. //! \~russian Сдвинуть линию по двум координатам на величину координат точки \a PIPoint. void operator-=(const PIPoint & p) { translate(-p); } //! \~english Translate line by point coordinates. //! \~russian Сдвинуть линию по двум координатам на величину координат точки \a PIPoint. PILine operator+(const PIPoint & p) { return translated(p); } //! \~english Translate line by negative point coordinates. //! \~russian Сдвинуть линию по двум координатам на величину координат точки \a PIPoint. PILine operator-(const PIPoint & p) { return translated(-p); } //! \~english Check equality of two lines. //! \~russian Проверить равенство координат двух отрезков. bool operator==(const PILine & r) const { return (p0 == r.p0 && p1 == r.p1); } //! \~english Check inequality of two lines. //! \~russian Проверить неравенство координат двух отрезков. bool operator!=(const PILine & r) const { return (p1 != r.p1 || p1 != r.p1); } }; //! \~english Stream output operator for PILine. //! \~russian Перегруженный оператор для вывода координат в \a PICout. template PICout operator<<(PICout & s, const PILine & v) { s.space(); s.saveAndSetControls(0); s << "Line{" << v.p0 << ", " << v.p1 << "}"; s.restoreControls(); return s; } typedef PILine PILinei; typedef PILine PILineu; typedef PILine PILinef; typedef PILine PILined; #endif // PILINE_H