Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 46 additions & 57 deletions src/framework/util/point.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,18 @@ template<class T>
class TPoint
{
public:
TPoint() : x(0), y(0) {}
TPoint(T xy) : x(xy), y(xy) {}
TPoint(T x, T y) : x(x), y(y) {}
TPoint(const TPoint& other) : x(other.x), y(other.y) {}

bool isNull() const { return x == 0 && y == 0; }
TSize<T> toSize() const { return TSize<T>(x, y); }
TPoint translated(T dx, T dy) const { TPoint point = *this; point.x += dx; point.y += dy; return point; }

TPoint scale(const float v) {
T x{}, y{};
constexpr TPoint() = default;
constexpr TPoint(T x, T y) : x{ x }, y{ y } {}
constexpr TPoint(T xy) : x{ xy }, y{ xy } {}

constexpr bool isNull() const noexcept { return x == 0 && y == 0; }
constexpr T manhattanLength() const noexcept { return std::abs(x) + std::abs(y); }
constexpr float length() const noexcept { return std::sqrt(static_cast<float>(x * x + y * y)); }
constexpr float distanceFrom(const TPoint& other) const noexcept { return (*this - other).length(); }
constexpr TPoint translated(T dx, T dy) const noexcept { return { x + dx, y + dy }; }
constexpr TSize<T> toSize() const noexcept { return { x, y }; }
constexpr TPoint scale(float v) noexcept {
if (v != 1.f) {
float factor = (1.f - (1.f / v));
x -= x * factor;
Expand All @@ -50,62 +52,49 @@ class TPoint
return *this;
}

TPoint operator-() const { return TPoint(-x, -y); }
constexpr TPoint operator-() const noexcept { return { -x, -y }; }

TPoint operator+(const TPoint& other) const { return TPoint(x + other.x, y + other.y); }
TPoint& operator+=(const TPoint& other) { x += other.x; y += other.y; return *this; }
TPoint operator-(const TPoint& other) const { return TPoint(x - other.x, y - other.y); }
TPoint& operator-=(const TPoint& other) { x -= other.x; y -= other.y; return *this; }
TPoint operator*(const TPoint& other) const { return TPoint(x * other.x, y * other.y); }
TPoint& operator*=(const TPoint& other) { x *= other.x; y *= other.y; return *this; }
TPoint operator/(const TPoint& other) const { return TPoint(x / other.x, y / other.y); }
TPoint& operator/=(const TPoint& other) { x /= other.x; y /= other.y; return *this; }
constexpr TPoint operator+(const TPoint& other) const { return { x + other.x, y + other.y }; }
constexpr TPoint& operator+=(const TPoint& other) { x += other.x; y += other.y; return *this; }
constexpr TPoint operator-(const TPoint& other) const { return { x - other.x, y - other.y }; }
constexpr TPoint& operator-=(const TPoint& other) { x -= other.x; y -= other.y; return *this; }
constexpr TPoint operator*(const TPoint& other) const { return { x * other.x, y * other.y }; }
constexpr TPoint& operator*=(const TPoint& other) { x *= other.x; y *= other.y; return *this; }
constexpr TPoint operator/(const TPoint& other) const { return { x / other.x, y / other.y }; }
constexpr TPoint& operator/=(const TPoint& other) { x /= other.x; y /= other.y; return *this; }

TPoint operator+(T other) const { return TPoint(x + other, y + other); }
TPoint& operator+=(T other) { x += other; y += other; return *this; }
TPoint operator-(T other) const { return TPoint(x - other, y - other); }
TPoint& operator-=(T other) { x -= other; y -= other; return *this; }
TPoint operator*(float v) const { return TPoint(x * v, y * v); }
TPoint& operator*=(float v) { x *= v; y *= v; return *this; }
TPoint operator/(float v) const { return TPoint(x / v, y / v); }
TPoint& operator/=(float v) { x /= v; y /= v; return *this; }
constexpr TPoint operator+(T other) const { return { x + other, y + other }; }
constexpr TPoint& operator+=(T other) { x += other; y += other; return *this; }
constexpr TPoint operator-(T other) const { return { x - other, y - other }; }
constexpr TPoint& operator-=(T other) { x -= other; y -= other; return *this; }
constexpr TPoint operator*(T v) const { return { x * v, y * v }; }
constexpr TPoint& operator*=(T v) { x *= v; y *= v; return *this; }
constexpr TPoint operator/(T v) const { return { x / v, y / v }; }
constexpr TPoint& operator/=(T v) { x /= v; y /= v; return *this; }

TPoint operator&(int a) { return TPoint(x & a, y & a); }
TPoint& operator&=(int a) { x &= a; y &= a; return *this; }
constexpr TPoint operator&(int a) const { return { x & a, y & a }; }
constexpr TPoint& operator&=(int a) { x &= a; y &= a; return *this; }

bool operator<=(const TPoint& other) const { return x <= other.x && y <= other.y; }
bool operator>=(const TPoint& other) const { return x >= other.x && y >= other.y; }
bool operator<(const TPoint& other) const { return x < other.x && y < other.y; }
bool operator>(const TPoint& other) const { return x > other.x && y > other.y; }
constexpr bool operator<=(const TPoint& other) const { return x <= other.x && y <= other.y; }
constexpr bool operator>=(const TPoint& other) const { return x >= other.x && y >= other.y; }
constexpr bool operator<(const TPoint& other) const { return x < other.x && y < other.y; }
constexpr bool operator>(const TPoint& other) const { return x > other.x && y > other.y; }

TPoint& operator=(const TPoint& other) = default;
bool operator==(const TPoint& other) const { return other.x == x && other.y == y; }
bool operator!=(const TPoint& other) const { return other.x != x || other.y != y; }
constexpr TPoint& operator=(const TPoint& other) = default;

float length() const { return sqrt(static_cast<float>(x * x + y * y)); }
T manhattanLength() const { return std::abs(x) + std::abs(y); }
constexpr bool operator==(const TPoint& other) const { return other.x == x && other.y == y; }
constexpr bool operator!=(const TPoint& other) const { return other.x != x || other.y != y; }

float distanceFrom(const TPoint& other) const { return TPoint(x - other.x, y - other.y).length(); }
constexpr std::size_t hash() const noexcept { return (7 * 15 + x) * 15 + y; }

std::size_t hash() const { return (7 * 15 + x) * 15 + y; }
friend std::ostream& operator<<(std::ostream& out, const TPoint& point) {
return out << point.x << " " << point.y;
}

T x, y;
friend std::istream& operator>>(std::istream& in, TPoint& point) {
return in >> point.x >> point.y;
}
};

using Point = TPoint<int>;
using PointF = TPoint<float>;

template<class T>
std::ostream& operator<<(std::ostream& out, const TPoint<T>& point)
{
out << point.x << " " << point.y;
return out;
}

template<class T>
std::istream& operator>>(std::istream& in, TPoint<T>& point)
{
in >> point.x;
in >> point.y;
return in;
}
using PointF = TPoint<float>;
Loading
Loading