-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhexgrid.cpp
More file actions
103 lines (79 loc) · 1.93 KB
/
hexgrid.cpp
File metadata and controls
103 lines (79 loc) · 1.93 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "input.hpp"
#include "hexgrid.hpp"
namespace utils {
namespace hexgrid {
std::ostream&
operator<<(std::ostream& os, const Direction& direction) {
switch (direction) {
case Direction::kNorthWest: os << "northwest"; break;
case Direction::kNorth: os << "north"; break;
case Direction::kNorthEast: os << "northeast"; break;
case Direction::kSouthWest: os << "southwest"; break;
case Direction::kSouth: os << "south"; break;
case Direction::kSouthEast: os << "southeast"; break;
}
return os;
}
Point::Point(const int& x, const int& y)
: m_x(x), m_y(y) {
}
Point::Point(const Point& point)
: m_x(point.m_x), m_y(point.m_y) {
}
size_t
Point::distance(const Point& destination) const {
auto delta_x = destination.m_x - this->m_x;
auto delta_y = destination.m_y - this->m_y;
if ((delta_x > 0) == (delta_y > 0)) {
return std::max(std::abs(delta_x), std::abs(delta_y));
} else {
return std::abs(delta_x) + std::abs(delta_y);
}
}
void
Point::step(const Direction& direction) {
switch (direction) {
case Direction::kNorthWest:
this->m_x--;
break;
case Direction::kNorth:
this->m_y++;
break;
case Direction::kNorthEast:
this->m_x++;
this->m_y++;
break;
case Direction::kSouthEast:
this->m_x++;
break;
case Direction::kSouth:
this->m_y--;
break;
case Direction::kSouthWest:
this->m_x--;
this->m_y--;
break;
}
}
Point&
Point::operator+=(const Point& summand) {
this->m_x += summand.m_x;
this->m_y += summand.m_y;
return *this;
}
bool
operator==(const Point& lhs, const Point& rhs) {
return lhs.m_x == rhs.m_x && lhs.m_y == rhs.m_y;
}
Point
operator+(const Point& lhs, const Point& rhs) {
Point point(lhs.m_x + rhs.m_x, lhs.m_y + rhs.m_y);
return point;
}
std::ostream&
operator<<(std::ostream& os, const Point& point) {
os << "(" << point.m_x << ", " << point.m_y << ")";
return os;
}
} // namespace hexgrid
} // namespace utils