Otclient  14/8/2020
point.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2020 OTClient <https://github.com/edubart/otclient>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 #ifndef POINT_H
24 #define POINT_H
25 
26 #include "../stdext/types.h"
27 #include <sstream>
28 #include <cmath>
29 
30 template<class T>
31 class TSize;
32 
33 template<class T>
34 class TPoint
35 {
36 public:
37  TPoint() : x(0), y(0) {}
38  TPoint(T x, T y) : x(x), y(y) { }
39  TPoint(const TPoint<T>& other) : x(other.x), y(other.y) { }
40 
41  bool isNull() const { return x==0 && y==0; }
42  TSize<T> toSize() const { return TSize<T>(x, y); }
43 
44  TPoint<T> operator-() const { return TPoint<T>(-x, -y); }
45 
46  TPoint<T> operator+(const TPoint<T>& other) const { return TPoint<T>(x + other.x, y + other.y); }
47  TPoint<T>& operator+=(const TPoint<T>& other) { x+=other.x; y+=other.y; return *this; }
48  TPoint<T> operator-(const TPoint<T>& other) const { return TPoint<T>(x - other.x, y - other.y); }
49  TPoint<T>& operator-=(const TPoint<T>& other) { x-=other.x; y-=other.y; return *this; }
50  TPoint<T> operator*(const TPoint<T>& other) const { return TPoint<T>(x * other.x, y * other.y); }
51  TPoint<T>& operator*=(const TPoint<T>& other) { x*=other.x; y*=other.y; return *this; }
52  TPoint<T> operator/(const TPoint<T>& other) const { return TPoint<T>(x/other.x, y/other.y); }
53  TPoint<T>& operator/=(const TPoint<T>& other) { x/=other.x; y/=other.y; return *this; }
54 
55  TPoint<T> operator+(T other) const { return TPoint<T>(x + other, y + other); }
56  TPoint<T>& operator+=(T other) { x+=other; y+=other; return *this; }
57  TPoint<T> operator-(T other) const { return TPoint<T>(x - other, y - other); }
58  TPoint<T>& operator-=(T other) { x-=other; y-=other; return *this; }
59  TPoint<T> operator*(float v) const { return TPoint<T>(x*v, y*v); }
60  TPoint<T>& operator*=(float v) { x*=v; y*=v; return *this; }
61  TPoint<T> operator/(float v) const { return TPoint<T>(x/v, y/v); }
62  TPoint<T>& operator/=(float v) { x/=v; y/=v; return *this; }
63 
64  TPoint<T> operator&(int a) { return TPoint<T>(x & a, y & a); }
65  TPoint<T>& operator&=(int a) { x &= a; y &= a; return *this; }
66 
67  bool operator<=(const TPoint<T>&other) const { return x<=other.x && y<=other.y; }
68  bool operator>=(const TPoint<T>&other) const { return x>=other.x && y>=other.y; }
69  bool operator<(const TPoint<T>&other) const { return x<other.x && y<other.y; }
70  bool operator>(const TPoint<T>&other) const { return x>other.x && y>other.y; }
71 
72  TPoint<T>& operator=(const TPoint<T>& other) { x = other.x; y = other.y; return *this; }
73  bool operator==(const TPoint<T>& other) const { return other.x==x && other.y==y; }
74  bool operator!=(const TPoint<T>& other) const { return other.x!=x || other.y!=y; }
75 
76  float length() const { return sqrt((float)(x*x + y*y)); }
77  T manhattanLength() const { return std::abs(x) + std::abs(y); }
78 
79  float distanceFrom(const TPoint<T>& other) const {
80  return TPoint<T>(x - other.x, y - other.y).getLength();
81  }
82 
83  T x, y;
84 };
85 
88 
89 template<class T>
90 std::ostream& operator<<(std::ostream& out, const TPoint<T>& point)
91 {
92  out << point.x << " " << point.y;
93  return out;
94 }
95 
96 template<class T>
97 std::istream& operator>>(std::istream& in, TPoint<T>& point)
98 {
99  in >> point.x;
100  in >> point.y;
101  return in;
102 }
103 
104 #endif
TPoint::operator+
TPoint< T > operator+(T other) const
Definition: point.h:55
TPoint::operator<=
bool operator<=(const TPoint< T > &other) const
Definition: point.h:67
operator<<
std::ostream & operator<<(std::ostream &out, const TPoint< T > &point)
Definition: point.h:90
PointF
TPoint< float > PointF
Definition: point.h:87
TPoint::y
T y
Definition: point.h:83
TPoint::operator/
TPoint< T > operator/(const TPoint< T > &other) const
Definition: point.h:52
TPoint::operator=
TPoint< T > & operator=(const TPoint< T > &other)
Definition: point.h:72
TPoint::operator/
TPoint< T > operator/(float v) const
Definition: point.h:61
TPoint::operator-
TPoint< T > operator-(const TPoint< T > &other) const
Definition: point.h:48
TPoint::operator*
TPoint< T > operator*(float v) const
Definition: point.h:59
TPoint::operator+=
TPoint< T > & operator+=(T other)
Definition: point.h:56
TPoint::operator-
TPoint< T > operator-(T other) const
Definition: point.h:57
TPoint::toSize
TSize< T > toSize() const
Definition: point.h:42
TPoint::manhattanLength
T manhattanLength() const
Definition: point.h:77
Point
TPoint< int > Point
Definition: point.h:86
TPoint::distanceFrom
float distanceFrom(const TPoint< T > &other) const
Definition: point.h:79
TPoint::operator*=
TPoint< T > & operator*=(const TPoint< T > &other)
Definition: point.h:51
TPoint::TPoint
TPoint(const TPoint< T > &other)
Definition: point.h:39
TPoint::operator+=
TPoint< T > & operator+=(const TPoint< T > &other)
Definition: point.h:47
TPoint::operator+
TPoint< T > operator+(const TPoint< T > &other) const
Definition: point.h:46
TPoint::operator*
TPoint< T > operator*(const TPoint< T > &other) const
Definition: point.h:50
TPoint::operator!=
bool operator!=(const TPoint< T > &other) const
Definition: point.h:74
TPoint::TPoint
TPoint()
Definition: point.h:37
TPoint::operator*=
TPoint< T > & operator*=(float v)
Definition: point.h:60
TPoint::x
T x
Definition: point.h:83
TPoint::TPoint
TPoint(T x, T y)
Definition: point.h:38
TPoint::isNull
bool isNull() const
Definition: point.h:41
TPoint::operator-=
TPoint< T > & operator-=(T other)
Definition: point.h:58
TPoint::operator>=
bool operator>=(const TPoint< T > &other) const
Definition: point.h:68
TPoint::length
float length() const
Definition: point.h:76
TPoint::operator-=
TPoint< T > & operator-=(const TPoint< T > &other)
Definition: point.h:49
TPoint::operator==
bool operator==(const TPoint< T > &other) const
Definition: point.h:73
TPoint::operator-
TPoint< T > operator-() const
Definition: point.h:44
TPoint::operator&=
TPoint< T > & operator&=(int a)
Definition: point.h:65
operator>>
std::istream & operator>>(std::istream &in, TPoint< T > &point)
Definition: point.h:97
TPoint::operator&
TPoint< T > operator&(int a)
Definition: point.h:64
TPoint
Definition: point.h:34
TPoint::operator/=
TPoint< T > & operator/=(const TPoint< T > &other)
Definition: point.h:53
TPoint::operator>
bool operator>(const TPoint< T > &other) const
Definition: point.h:70
TSize
Definition: point.h:31
TPoint::operator<
bool operator<(const TPoint< T > &other) const
Definition: point.h:69
TPoint::operator/=
TPoint< T > & operator/=(float v)
Definition: point.h:62