//! \~\file piline.h //! \~\ingroup Math //! \~\brief //! \~english Two-dimensional line segment type //! \~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" //! \~\ingroup Math //! \~\brief //! \~english Two-dimensional line segment. //! \~russian Двумерный отрезок. //! \~\details //! \~english Stores start and end points of a segment on a plane. //! \~russian Хранит начальную и конечную точки отрезка на плоскости. template class PIP_EXPORT PILine { static_assert(std::is_arithmetic::value, "Type must be arithmetic"); public: //! \~english Segment start point. //! \~russian Начальная точка отрезка. PIPoint p0; //! \~english Segment end point. //! \~russian Конечная точка отрезка. PIPoint p1; //! \~english Constructs empty segment. //! \~russian Создает пустой отрезок. PILine() {} //! \~english Constructs segment from two points. //! \~russian Создает отрезок по двум точкам. PILine(const PIPoint & p0_, const PIPoint & p1_) { p0 = p0_; p1 = p1_; } //! \~english Constructs segment from endpoint coordinates. //! \~russian Создает отрезок по координатам концов. PILine(Type x0, Type y0, Type x1, Type y1) { p0.set(x0, y0); p1.set(x1, y1); } //! \~english Sets segment endpoints from two points. //! \~russian Задает концы отрезка по двум точкам. PILine & set(const PIPoint & p0_, const PIPoint & p1_) { p0 = p0_; p1 = p1_; return *this; } //! \~english Sets segment endpoints from coordinates. //! \~russian Задает концы отрезка по координатам. PILine & set(Type x0, Type y0, Type x1, Type y1) { p0.set(x0, y0); p1.set(x1, y1); return *this; } //! \~english Returns `true` if segment endpoints are equal. //! \~russian Возвращает `true`, если концы отрезка совпадают. bool isEmpty() const { return (p0 == p1); } //! \~english Returns horizontal span between endpoints. //! \~russian Возвращает горизонтальный размах между концами. Type width() const { return piAbs(p1.x - p0.x); } //! \~english Returns vertical span between endpoints. //! \~russian Возвращает вертикальный размах между концами. Type height() const { return piAbs(p1.y - p0.y); } //! \~english Shifts the segment by `x` and `y`. //! \~russian Сдвигает отрезок на `x` и `y`. PILine & translate(Type x, Type y) { p0.translate(x, y); p1.translate(x, y); return *this; } //! \~english Shifts the segment by a point offset. //! \~russian Сдвигает отрезок на смещение, заданное точкой. PILine & translate(const PIPoint & p) { p0.translate(p); p1.translate(p); return *this; } //! \~english Returns translated copy of the segment. //! \~russian Возвращает смещенную копию отрезка. PILine translated(Type x, Type y) const { PILine l(*this); l.translate(x, y); return l; } //! \~english Returns copy shifted by a point offset. //! \~russian Возвращает копию, смещенную на точку. PILine translated(const PIPoint & p) const { PILine l(*this); l.translate(p); return l; } //! \~english Same as \a translate(). //! \~russian Синоним \a translate(). PILine & move(Type x, Type y) { return translate(x, y); } //! \~english Same as \a translate(). //! \~russian Синоним \a translate(). PILine & move(const PIPoint & p) { return translate(p); } //! \~english Same as \a translated(). //! \~russian Синоним \a translated(). PILine moved(Type x, Type y) const { PILine l(*this); l.translate(x, y); return l; } //! \~english Same as \a translated(). //! \~russian Синоним \a translated(). PILine moved(const PIPoint & p) const { PILine l(*this); l.translate(p); return l; } //! \~english Shifts both coordinates by `x`. //! \~russian Сдвигает обе координаты на `x`. void operator+=(Type x) { translate(x, x); } //! \~english Shifts the segment by a point offset. //! \~russian Сдвигает отрезок на смещение, заданное точкой. void operator+=(const PIPoint & p) { translate(p); } //! \~english Shifts both coordinates by `-x`. //! \~russian Сдвигает обе координаты на `-x`. void operator-=(Type x) { translate(-x, -x); } //! \~english Shifts the segment by the negated point offset. //! \~russian Сдвигает отрезок на отрицательное смещение точки. void operator-=(const PIPoint & p) { translate(-p); } //! \~english Returns segment shifted by a point offset. //! \~russian Возвращает отрезок, смещенный на точку. PILine operator+(const PIPoint & p) { return translated(p); } //! \~english Returns segment shifted by the negated point offset. //! \~russian Возвращает отрезок, смещенный на отрицательное смещение точки. PILine operator-(const PIPoint & p) { return translated(-p); } //! \~english Checks whether segment endpoints are equal. //! \~russian Проверяет равенство концов отрезков. bool operator==(const PILine & r) const { return (p0 == r.p0 && p1 == r.p1); } //! \~english Checks whether segments differ. //! \~russian Проверяет различие отрезков. bool operator!=(const PILine & r) const { return (p1 != r.p1 || p1 != r.p1); } }; //! \relatesalso PICout //! \~english Writes segment coordinates to \a PICout. //! \~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; } //! \~english Alias for segment with `int` coordinates. //! \~russian Псевдоним отрезка с координатами типа `int`. typedef PILine PILinei; //! \~english Alias for segment with `uint` coordinates. //! \~russian Псевдоним отрезка с координатами типа `uint`. typedef PILine PILineu; //! \~english Alias for segment with `float` coordinates. //! \~russian Псевдоним отрезка с координатами типа `float`. typedef PILine PILinef; //! \~english Alias for segment with `double` coordinates. //! \~russian Псевдоним отрезка с координатами типа `double`. typedef PILine PILined; #endif // PILINE_H