-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoint.hpp
More file actions
77 lines (62 loc) · 1.59 KB
/
point.hpp
File metadata and controls
77 lines (62 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#ifndef point_hpp
#define point_hpp
#include <string>
#include <sstream>
#include <ostream>
#include <cmath>
namespace csce {
template<typename T>
class point {
public:
T x = {};
T y = {};
point() {}
point(T _x, T _y) : x(_x), y(_y) {}
long double distance_to(const csce::point<T>& other) const {
T dx = this->x - other.x;
T dy = this->y - other.y;
return std::sqrt(dx * dx + dy * dy);
}
std::string str() const {
std::stringstream output;
output << "(" << this->x << ", " << this->y << ")";
return output.str();
}
bool operator==(const csce::point<T>& other) const {
return this->x == other.x && this->y == other.y;
}
bool operator!=(const csce::point<T>& other) const {
return !(*this == other);
}
bool operator<(const csce::point<T>& other) const {
if(this->y != other.y){
return this->y < other.y;
}
return this->x < other.x;
}
friend std::ostream& operator<<(std::ostream& stream, const csce::point<T>& p){
stream << p.str();
return stream;
}
csce::point<T> operator-(const csce::point<T>& other) const {
T dx = other.x - this->x;
T dy = other.y - this->y;
return csce::point<T>(dx, dy);
}
csce::point<T> operator+(const csce::point<T>& other) const {
T nx = other.x + this->x;
T ny = other.y + this->y;
return csce::point<T>(nx, ny);
}
private:
};
}
namespace std {
template <typename T>
struct hash< csce::point<T> >{
std::size_t operator()(const csce::point<T>& p) const{
return (std::hash<T>()(p.x) ^ (std::hash<T>()(p.y) << 1)) >> 1;
}
};
}
#endif /* point_hpp */