2010-11-25 00:36:15 +01:00
|
|
|
#ifndef POINT_H
|
|
|
|
#define POINT_H
|
|
|
|
|
2011-07-13 23:12:36 +02:00
|
|
|
#include "types.h"
|
|
|
|
#include <ostream>
|
|
|
|
#include <cmath>
|
2010-11-25 00:36:15 +01:00
|
|
|
|
|
|
|
template <class T>
|
|
|
|
class TSize;
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
class TPoint
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
inline TPoint() : x(0), y(0) {};
|
|
|
|
inline TPoint(T x, T y) : x(x), y(y) { };
|
|
|
|
inline TPoint(const TPoint<T>& other) : x(other.x), y(other.y) { };
|
|
|
|
|
|
|
|
inline bool isNull() const { return x==0 && y==0; }
|
|
|
|
inline TSize<T> toSize() const { return TSize<T>(x, y); }
|
|
|
|
|
|
|
|
inline TPoint<T> operator-() const { return TPoint<T>(-x, -y); }
|
|
|
|
inline TPoint<T> operator+(const TPoint<T>& other) const { return TPoint<T>(x + other.x, y + other.y); }
|
|
|
|
inline TPoint<T>& operator+=(const TPoint<T>& other) { x+=other.x; y+=other.y; return *this; }
|
|
|
|
inline TPoint<T> operator-(const TPoint<T>& other) const { return TPoint<T>(x - other.x, y - other.y); }
|
|
|
|
inline TPoint<T>& operator-=(const TPoint<T>& other) { x-=other.x; y-=other.y; return *this; }
|
|
|
|
inline TPoint<T> operator*(const TPoint<T>& other) const { return TPoint<T>(x * other.x, y * other.y); }
|
|
|
|
inline TPoint<T>& operator*=(const TPoint<T>& other) { x*=other.x; y*=other.y; return *this; }
|
|
|
|
inline TPoint<T> operator*(const T v) const { return TPoint<T>(x * v, y * v); }
|
|
|
|
inline TPoint<T>& operator*=(const T v) { x*=v; y*=v; return *this; }
|
|
|
|
inline TPoint<T> operator/(const TPoint<T>& other) const { return TPoint<T>(x/other.x, y/other.y); }
|
|
|
|
inline TPoint<T>& operator/=(const TPoint<T>& other) { x/=other.x; y/=other.y; return *this; }
|
|
|
|
inline TPoint<T> operator/(const T v) const { return TPoint<T>(x/v, y/v); }
|
|
|
|
inline TPoint<T>& operator/=(const T v) { x/=v; y/=v; return *this; }
|
|
|
|
|
|
|
|
inline bool operator<=(const TPoint<T>&other) const { return x<=other.x && y<=other.y; }
|
|
|
|
inline bool operator>=(const TPoint<T>&other) const { return x>=other.x && y>=other.y; }
|
|
|
|
inline bool operator<(const TPoint<T>&other) const { return x<other.x && y<other.y; }
|
|
|
|
inline bool operator>(const TPoint<T>&other) const { return x>other.x && y>other.y; }
|
|
|
|
|
|
|
|
inline TPoint<T>& operator=(const TPoint<T>& other) { x = other.x; y = other.y; return *this; }
|
|
|
|
inline bool operator==(const TPoint<T>& other) const { return other.x==x && other.y==y; }
|
|
|
|
inline bool operator!=(const TPoint<T>& other) const { return other.x!=x || other.y!=y; }
|
|
|
|
|
|
|
|
inline float length() const { return sqrtf((float)(x*x + y*y)); }
|
2011-02-08 23:48:26 +01:00
|
|
|
inline T manhattanLength() const { return std::abs(x) + std::abs(y); }
|
2010-11-25 00:36:15 +01:00
|
|
|
|
|
|
|
inline float distanceFrom(const TPoint<T>& other) const {
|
|
|
|
return TPoint<T>(x - other.x, y - other.y).getLength();
|
|
|
|
}
|
|
|
|
|
|
|
|
T x, y;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef TPoint<int> Point;
|
|
|
|
typedef TPoint<float> PointF;
|
|
|
|
|
2011-04-10 22:40:44 +02:00
|
|
|
template <class T>
|
|
|
|
inline std::ostream& operator<<(std::ostream& out, const TPoint<T>& point)
|
|
|
|
{
|
|
|
|
out << "Point(" << point.x << ","
|
|
|
|
<< point.y << ")";
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2010-11-25 00:36:15 +01:00
|
|
|
#endif
|