PIPoint: const fixes, add some methods

This commit is contained in:
2026-03-28 22:23:41 +03:00
parent 9076cc749a
commit 22b47799dc
2 changed files with 41 additions and 8 deletions

View File

@@ -7,7 +7,7 @@ project(PIP)
set(PIP_MAJOR 5) set(PIP_MAJOR 5)
set(PIP_MINOR 7) set(PIP_MINOR 7)
set(PIP_REVISION 0) set(PIP_REVISION 0)
set(PIP_SUFFIX _alpha) set(PIP_SUFFIX _beta)
set(PIP_COMPANY SHS) set(PIP_COMPANY SHS)
set(PIP_DOMAIN org.SHS) set(PIP_DOMAIN org.SHS)

View File

@@ -132,6 +132,10 @@ public:
//! \~russian Возвращает полярный угол в градусах. //! \~russian Возвращает полярный угол в градусах.
double angleDeg() const { return toDeg(atan2(y, x)); } double angleDeg() const { return toDeg(atan2(y, x)); }
//! \~english Returns |x| + |y|.
//! \~russian Возвращает |x| + |y|.
Type manhattanLength() const { return piAbs<Type>(x) + piAbs<Type>(y); }
//! \~english Returns polar form with radius in \a x and angle in \a y. //! \~english Returns polar form with radius in \a x and angle in \a y.
//! \~russian Возвращает полярную форму, где радиус хранится в \a x, а угол в \a y. //! \~russian Возвращает полярную форму, где радиус хранится в \a x, а угол в \a y.
PIPoint<Type> toPolar(bool isDeg = false) const { return PIPoint<Type>(sqrt(x * x + y * y), isDeg ? angleDeg() : angleRad()); } PIPoint<Type> toPolar(bool isDeg = false) const { return PIPoint<Type>(sqrt(x * x + y * y), isDeg ? angleDeg() : angleRad()); }
@@ -165,31 +169,31 @@ public:
//! \~english Returns sum of two points. //! \~english Returns sum of two points.
//! \~russian Возвращает сумму двух точек. //! \~russian Возвращает сумму двух точек.
PIPoint<Type> operator+(const PIPoint<Type> & p) { return PIPoint<Type>(x + p.x, y + p.y); } PIPoint<Type> operator+(const PIPoint<Type> & p) const { return PIPoint<Type>(x + p.x, y + p.y); }
//! \~english Returns point with `p` added to both coordinates. //! \~english Returns point with `p` added to both coordinates.
//! \~russian Возвращает точку с добавлением `p` к обеим координатам. //! \~russian Возвращает точку с добавлением `p` к обеим координатам.
PIPoint<Type> operator+(const Type & p) { return PIPoint<Type>(x + p, y + p); } PIPoint<Type> operator+(const Type & p) const { return PIPoint<Type>(x + p, y + p); }
//! \~english Returns difference between two points. //! \~english Returns difference between two points.
//! \~russian Возвращает разность двух точек. //! \~russian Возвращает разность двух точек.
PIPoint<Type> operator-(const PIPoint<Type> & p) { return PIPoint<Type>(x - p.x, y - p.y); } PIPoint<Type> operator-(const PIPoint<Type> & p) const { return PIPoint<Type>(x - p.x, y - p.y); }
//! \~english Returns point with `p` subtracted from both coordinates. //! \~english Returns point with `p` subtracted from both coordinates.
//! \~russian Возвращает точку с вычитанием `p` из обеих координат. //! \~russian Возвращает точку с вычитанием `p` из обеих координат.
PIPoint<Type> operator-(const Type & p) { return PIPoint<Type>(x - p, y - p); } PIPoint<Type> operator-(const Type & p) const { return PIPoint<Type>(x - p, y - p); }
//! \~english Returns point with inverted coordinates. //! \~english Returns point with inverted coordinates.
//! \~russian Возвращает точку с инвертированными координатами. //! \~russian Возвращает точку с инвертированными координатами.
PIPoint<Type> operator-() { return PIPoint<Type>(-x, -y); } PIPoint<Type> operator-() const { return PIPoint<Type>(-x, -y); }
//! \~english Returns point scaled by `v`. //! \~english Returns point scaled by `v`.
//! \~russian Возвращает точку, масштабированную на `v`. //! \~russian Возвращает точку, масштабированную на `v`.
PIPoint<Type> operator*(Type v) { return PIPoint<Type>(x * v, y * v); } PIPoint<Type> operator*(Type v) const { return PIPoint<Type>(x * v, y * v); }
//! \~english Returns point divided by `v`. //! \~english Returns point divided by `v`.
//! \~russian Возвращает точку, деленную на `v`. //! \~russian Возвращает точку, деленную на `v`.
PIPoint<Type> operator/(Type v) { return PIPoint<Type>(x / v, y / v); } PIPoint<Type> operator/(Type v) const { return PIPoint<Type>(x / v, y / v); }
//! \~english Checks whether point coordinates are equal. //! \~english Checks whether point coordinates are equal.
//! \~russian Проверяет равенство координат точек. //! \~russian Проверяет равенство координат точек.
@@ -200,6 +204,35 @@ public:
bool operator!=(const PIPoint<Type> & p) const { return (x != p.x || y != p.y); } bool operator!=(const PIPoint<Type> & p) const { return (x != p.x || y != p.y); }
}; };
template<typename Type>
inline PIPoint<Type> operator+(const Type & f, const PIPoint<Type> & s) {
return PIPoint<Type>(f + s.x, f + s.y);
}
template<typename Type>
inline PIPoint<Type> operator-(const Type & f, const PIPoint<Type> & s) {
return PIPoint<Type>(f - s.x, f - s.y);
}
template<typename Type>
inline PIPoint<Type> operator*(const Type & f, const PIPoint<Type> & s) {
return PIPoint<Type>(f * s.x, f * s.y);
}
template<typename Type>
inline PIPoint<Type> operator/(const Type & f, const PIPoint<Type> & s) {
return PIPoint<Type>(f / s.x, f / s.y);
}
//! \~english Returns {|x|, |y|}.
//! \~russian Возвращает {|x|, |y|}.
template<typename Type>
inline constexpr PIPoint<Type> piAbs(const PIPoint<Type> & v) {
return PIPoint<Type>(piAbs<Type>(v.x), piAbs<Type>(v.y));
}
//! \relatesalso PICout //! \relatesalso PICout
//! \~english Writes point coordinates to \a PICout. //! \~english Writes point coordinates to \a PICout.
//! \~russian Выводит координаты точки в \a PICout. //! \~russian Выводит координаты точки в \a PICout.