158 lines
3.7 KiB
C++
158 lines
3.7 KiB
C++
//! \file pipoint.h
|
|
//! \ingroup Math
|
|
//! \brief
|
|
//! \~english Two-dimensional point class
|
|
//! \~russian Класс двумерной точки
|
|
/*
|
|
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 <http://www.gnu.org/licenses/>.
|
|
*/
|
|
#ifndef PIPOINT_H
|
|
#define PIPOINT_H
|
|
|
|
#include "pimathbase.h"
|
|
|
|
|
|
//! \brief
|
|
//! \~english Two-dimensional point class
|
|
//! \~russian Класс двумерной точки
|
|
template<typename Type>
|
|
class PIP_EXPORT PIPoint {
|
|
static_assert(std::is_arithmetic<Type>::value, "Type must be arithmetic");
|
|
public:
|
|
Type x;
|
|
Type y;
|
|
|
|
//!
|
|
PIPoint() {x = y = Type();}
|
|
|
|
//!
|
|
PIPoint(Type x_, Type y_) {set(x_, y_);}
|
|
|
|
//!
|
|
PIPoint<Type> & set(Type x_, Type y_) {
|
|
x = x_;
|
|
y = y_;
|
|
return *this;
|
|
}
|
|
|
|
//!
|
|
PIPoint<Type> & set(const PIPoint<Type> & p) {
|
|
x = p.x;
|
|
y = p.y;
|
|
return *this;
|
|
}
|
|
|
|
//!
|
|
PIPoint<Type> & translate(Type x_, Type y_) {
|
|
x += x_;
|
|
y += y_;
|
|
return *this;
|
|
}
|
|
|
|
//!
|
|
PIPoint<Type> & translate(const PIPoint<Type> & p) {
|
|
x += p.x;
|
|
y += p.y;
|
|
return *this;
|
|
}
|
|
|
|
//!
|
|
PIPoint<Type> translated(Type x_, Type y_) const {
|
|
PIPoint<Type> rp(*this);
|
|
rp.translate(x_, y_);
|
|
return rp;
|
|
}
|
|
|
|
//!
|
|
PIPoint<Type> translated(const PIPoint<Type> & p) const {
|
|
PIPoint<Type> rp(*this);
|
|
rp.translate(p);
|
|
return rp;
|
|
}
|
|
|
|
//!
|
|
PIPoint<Type> & move(Type x_, Type y_) {return translate(x_, y_);}
|
|
|
|
//!
|
|
PIPoint<Type> & move(const PIPoint<Type> & p) {return translate(p);}
|
|
|
|
//!
|
|
PIPoint<Type> moved(Type x_, Type y_) const {
|
|
PIPoint<Type> rp(*this);
|
|
rp.translate(x_, y_);
|
|
return rp;
|
|
}
|
|
|
|
//!
|
|
PIPoint<Type> moved(const PIPoint<Type> & p) const {
|
|
PIPoint<Type> rp(*this);
|
|
rp.translate(p);
|
|
return rp;
|
|
}
|
|
|
|
//!
|
|
double angleRad() const {return atan2(y, x);}
|
|
|
|
//!
|
|
double angleDeg() const {return toDeg(atan2(y, x));}
|
|
|
|
//!
|
|
PIPoint<Type> toPolar(bool isDeg = false) const {return PIPoint<Type>(sqrt(x*x + y*y), isDeg ? angleDeg() : angleRad());}
|
|
|
|
//!
|
|
static PIPoint<Type> fromPolar(const PIPoint<Type> & p) {return PIPoint<Type>(p.y * cos(p.x), p.y * sin(p.x));}
|
|
|
|
//!
|
|
void operator +=(const PIPoint<Type> & p) {translate(p);}
|
|
|
|
//!
|
|
PIPoint<Type> operator +(const PIPoint<Type> & p) {return PIPoint<Type>(x + p.x, y + p.y);}
|
|
|
|
//!
|
|
PIPoint<Type> operator +(const Type & p) {return PIPoint<Type>(x + p, y + p);}
|
|
|
|
//!
|
|
PIPoint<Type> operator -(const PIPoint<Type> & p) {return PIPoint<Type>(x - p.x, y - p.y);}
|
|
|
|
//!
|
|
PIPoint<Type> operator -(const Type & p) {return PIPoint<Type>(x - p, y - p);}
|
|
|
|
//!
|
|
PIPoint<Type> operator -() {return PIPoint<Type>(-x, -y);}
|
|
|
|
//!
|
|
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>
|
|
PICout operator <<(PICout & s, const PIPoint<Type> & v) {
|
|
s.setControl(0, true);
|
|
s << "Point{" << v.x << ", " << v.y << "}";
|
|
s.restoreControl();
|
|
return s;
|
|
}
|
|
|
|
|
|
typedef PIPoint<int> PIPointi;
|
|
typedef PIPoint<uint> PIPointu;
|
|
typedef PIPoint<float> PIPointf;
|
|
typedef PIPoint<double> PIPointd;
|
|
|
|
#endif // PIPOINT_H
|