//! \~\file pipoint.h
//! \~\ingroup Math
//! \~\brief
//! \~english The PIPoint class provides a two-dimensional point on the plane.
//! \~russian Класс PIPoint представляет точку на плоскости с двумя координатами.
/*
PIP - Platform Independent Primitives
Two-dimensional point 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 PIPOINT_H
#define PIPOINT_H
#include "pimathbase.h"
//! \~\ingroup Math
//! \~\brief
//! \~english Two-dimensional point.
//! \~russian Двумерная точка.
//! \~\details
//! \~english Stores point coordinates and provides basic translation and conversion helpers.
//! \~russian Хранит координаты точки и предоставляет базовые методы смещения и преобразования.
template
class PIP_EXPORT PIPoint {
static_assert(std::is_arithmetic::value, "Type must be arithmetic");
public:
//! \~english Horizontal coordinate.
//! \~russian Горизонтальная координата.
Type x;
//! \~english Vertical coordinate.
//! \~russian Вертикальная координата.
Type y;
//! \~english Constructs point at the origin.
//! \~russian Создает точку в начале координат.
PIPoint() { x = y = Type(); }
//! \~english Constructs point from coordinates.
//! \~russian Создает точку с заданными координатами.
PIPoint(Type x_, Type y_) { set(x_, y_); }
//! \~english Sets point coordinates.
//! \~russian Задает координаты точки.
PIPoint & set(Type x_, Type y_) {
x = x_;
y = y_;
return *this;
}
//! \~english Copies coordinates from another point.
//! \~russian Копирует координаты из другой точки.
PIPoint & set(const PIPoint & p) {
x = p.x;
y = p.y;
return *this;
}
//! \~english Shifts the point by `x_` and `y_`.
//! \~russian Сдвигает точку на `x_` и `y_`.
PIPoint & translate(Type x_, Type y_) {
x += x_;
y += y_;
return *this;
}
//! \~english Shifts the point by another point.
//! \~russian Сдвигает точку на координаты другой точки.
PIPoint & translate(const PIPoint & p) {
x += p.x;
y += p.y;
return *this;
}
//! \~english Returns translated copy of the point.
//! \~russian Возвращает смещенную копию точки.
PIPoint translated(Type x_, Type y_) const {
PIPoint rp(*this);
rp.translate(x_, y_);
return rp;
}
//! \~english Returns copy shifted by another point.
//! \~russian Возвращает копию, смещенную на другую точку.
PIPoint translated(const PIPoint & p) const {
PIPoint rp(*this);
rp.translate(p);
return rp;
}
//! \~english Same as \a translate().
//! \~russian Синоним \a translate().
PIPoint & move(Type x_, Type y_) { return translate(x_, y_); }
//! \~english Same as \a translate().
//! \~russian Синоним \a translate().
PIPoint & move(const PIPoint & p) { return translate(p); }
//! \~english Same as \a translated().
//! \~russian Синоним \a translated().
PIPoint moved(Type x_, Type y_) const {
PIPoint rp(*this);
rp.translate(x_, y_);
return rp;
}
//! \~english Same as \a translated().
//! \~russian Синоним \a translated().
PIPoint moved(const PIPoint & p) const {
PIPoint rp(*this);
rp.translate(p);
return rp;
}
//! \~english Returns polar angle in radians.
//! \~russian Возвращает полярный угол в радианах.
double angleRad() const { return atan2(y, x); }
//! \~english Returns polar angle in degrees.
//! \~russian Возвращает полярный угол в градусах.
double angleDeg() const { return toDeg(atan2(y, x)); }
//! \~english Returns polar form with radius in \a x and angle in \a y.
//! \~russian Возвращает полярную форму, где радиус хранится в \a x, а угол в \a y.
PIPoint toPolar(bool isDeg = false) const { return PIPoint(sqrt(x * x + y * y), isDeg ? angleDeg() : angleRad()); }
//! \~english Builds Cartesian point from polar pair with angle in \a x and radius in \a y.
//! \~russian Строит декартову точку из полярной пары, где угол хранится в \a x, а радиус в \a y.
static PIPoint fromPolar(const PIPoint & p) { return PIPoint(p.y * cos(p.x), p.y * sin(p.x)); }
//! \~english Same as \a translate().
//! \~russian Синоним \a translate().
PIPoint & operator+=(const PIPoint & p) {
translate(p);
return *this;
}
//! \~english Multiplies both coordinates by `v`.
//! \~russian Умножает обе координаты на `v`.
PIPoint & operator*=(Type v) {
x *= v;
y *= v;
return *this;
}
//! \~english Divides both coordinates by `v`.
//! \~russian Делит обе координаты на `v`.
PIPoint & operator/=(Type v) {
x /= v;
y /= v;
return *this;
}
//! \~english Returns sum of two points.
//! \~russian Возвращает сумму двух точек.
PIPoint operator+(const PIPoint & p) { return PIPoint(x + p.x, y + p.y); }
//! \~english Returns point with `p` added to both coordinates.
//! \~russian Возвращает точку с добавлением `p` к обеим координатам.
PIPoint operator+(const Type & p) { return PIPoint(x + p, y + p); }
//! \~english Returns difference between two points.
//! \~russian Возвращает разность двух точек.
PIPoint operator-(const PIPoint & p) { return PIPoint(x - p.x, y - p.y); }
//! \~english Returns point with `p` subtracted from both coordinates.
//! \~russian Возвращает точку с вычитанием `p` из обеих координат.
PIPoint operator-(const Type & p) { return PIPoint(x - p, y - p); }
//! \~english Returns point with inverted coordinates.
//! \~russian Возвращает точку с инвертированными координатами.
PIPoint operator-() { return PIPoint(-x, -y); }
//! \~english Returns point scaled by `v`.
//! \~russian Возвращает точку, масштабированную на `v`.
PIPoint operator*(Type v) { return PIPoint(x * v, y * v); }
//! \~english Returns point divided by `v`.
//! \~russian Возвращает точку, деленную на `v`.
PIPoint operator/(Type v) { return PIPoint(x / v, y / v); }
//! \~english Checks whether point coordinates are equal.
//! \~russian Проверяет равенство координат точек.
bool operator==(const PIPoint & p) const { return (x == p.x && y == p.y); }
//! \~english Checks whether point coordinates differ.
//! \~russian Проверяет неравенство координат точек.
bool operator!=(const PIPoint & p) const { return (x != p.x || y != p.y); }
};
//! \relatesalso PICout
//! \~english Writes point coordinates to \a PICout.
//! \~russian Выводит координаты точки в \a PICout.
template
PICout operator<<(PICout & s, const PIPoint & v) {
s.space();
s.saveAndSetControls(0);
s << "Point{" << v.x << ", " << v.y << "}";
s.restoreControls();
return s;
}
//! \~english Alias for point with `int` coordinates.
//! \~russian Псевдоним точки с координатами типа `int`.
typedef PIPoint PIPointi;
//! \~english Alias for point with `uint` coordinates.
//! \~russian Псевдоним точки с координатами типа `uint`.
typedef PIPoint PIPointu;
//! \~english Alias for point with `float` coordinates.
//! \~russian Псевдоним точки с координатами типа `float`.
typedef PIPoint PIPointf;
//! \~english Alias for point with `double` coordinates.
//! \~russian Псевдоним точки с координатами типа `double`.
typedef PIPoint PIPointd;
#endif // PIPOINT_H