1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-20 20:23:03 +02:00
vcmi/lib/Point.h

133 lines
2.3 KiB
C++
Raw Normal View History

2023-01-17 21:43:44 +02:00
/*
2023-01-18 02:32:08 +02:00
* Point.h, part of VCMI engine
2023-01-17 21:43:44 +02:00
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#pragma once
VCMI_LIB_NAMESPACE_BEGIN
class int3;
// A point with x/y coordinate, used mostly for graphic rendering
class Point
{
public:
int x, y;
//constructors
constexpr Point() : x(0), y(0)
2023-01-17 21:43:44 +02:00
{
}
2023-01-17 21:43:44 +02:00
constexpr Point(int X, int Y)
: x(X)
, y(Y)
{
}
2023-01-17 21:43:44 +02:00
constexpr static Point makeInvalid()
{
return Point(std::numeric_limits<int>::min(), std::numeric_limits<int>::min());
}
explicit DLL_LINKAGE Point(const int3 &a);
2023-01-17 21:43:44 +02:00
template<typename T>
constexpr Point operator+(const T &b) const
2023-01-17 21:43:44 +02:00
{
return Point(x+b.x,y+b.y);
}
template<typename T>
constexpr Point operator/(const T &div) const
2023-01-17 21:43:44 +02:00
{
return Point(x/div, y/div);
}
template<typename T>
constexpr Point operator*(const T &mul) const
2023-01-17 21:43:44 +02:00
{
return Point(x*mul, y*mul);
}
constexpr Point operator*(const Point &b) const
{
return Point(x*b.x,y*b.y);
}
2023-01-17 21:43:44 +02:00
template<typename T>
constexpr Point& operator+=(const T &b)
2023-01-17 21:43:44 +02:00
{
x += b.x;
y += b.y;
return *this;
}
constexpr Point operator-() const
{
return Point(-x, -y);
}
2023-01-17 21:43:44 +02:00
template<typename T>
constexpr Point operator-(const T &b) const
2023-01-17 21:43:44 +02:00
{
return Point(x - b.x, y - b.y);
}
template<typename T>
constexpr Point& operator-=(const T &b)
2023-01-17 21:43:44 +02:00
{
x -= b.x;
y -= b.y;
return *this;
}
template<typename T> constexpr Point& operator=(const T &t)
2023-01-17 21:43:44 +02:00
{
x = t.x;
y = t.y;
return *this;
}
template<typename T> constexpr bool operator==(const T &t) const
2023-01-17 21:43:44 +02:00
{
return x == t.x && y == t.y;
}
template<typename T> constexpr bool operator!=(const T &t) const
2023-01-17 21:43:44 +02:00
{
return !(*this == t);
}
constexpr bool isValid() const
{
return x > std::numeric_limits<int>::min() && y > std::numeric_limits<int>::min();
}
constexpr int lengthSquared() const
{
return x * x + y * y;
}
int length() const
{
return std::sqrt(lengthSquared());
}
2023-08-26 20:19:25 +02:00
double angle() const
{
2023-08-26 21:13:33 +02:00
return std::atan2(y, x); // rad
2023-08-26 20:19:25 +02:00
}
2023-01-17 21:43:44 +02:00
template <typename Handler>
void serialize(Handler &h, const int version)
{
h & x;
h & y;
}
};
VCMI_LIB_NAMESPACE_END