point.h

main 105 lines · 4.3 KB Raw
Alex Alex Commit Client Upload Part 2 19/10/2026 00:06
1/*
2 * Copyright (c) 2010-2017 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
30template<class T>
31class TSize;
32
33template<class T>
34class TPoint
35{
36public:
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 || (x==other.x && y<=other.y); }
68 bool operator>=(const TPoint<T>&other) const { return x>=other.x || (x==other.x && y>=other.y); }
69 bool operator<(const TPoint<T>&other) const { return x<other.x || (x==other.x && y<other.y); }
70 bool operator>(const TPoint<T>&other) const { return x>other.x || (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
86typedef TPoint<int> Point;
87typedef TPoint<float> PointF;
88
89template<class T>
90std::ostream& operator<<(std::ostream& out, const TPoint<T>& point)
91{
92 out << point.x << " " << point.y;
93 return out;
94}
95
96template<class T>
97std::istream& operator>>(std::istream& in, TPoint<T>& point)
98{
99 in >> point.x;
100 in >> point.y;
101 return in;
102}
103
104#endif
105
Top