-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoord.h
56 lines (41 loc) · 1.05 KB
/
coord.h
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
#pragma once
#include <stdexcept>
#include <cmath>
// A simple 2D coordinate class used to handle tile positions.
class coord {
public:
int x;
int y;
coord() { x = 0; y = 0; }
coord(int x_in, int y_in) { x = x_in; y = y_in; }
// boilerplate
coord operator+(const coord & rhs) const {
return coord(x + rhs.x, y + rhs.y);
}
coord operator-(const coord & rhs) const {
return coord(x - rhs.x, y - rhs.y);
}
coord operator+=(const coord & rhs) {
x += rhs.x;
y += rhs.y;
return *this;
}
coord operator-=(const coord & rhs) {
x -= rhs.x;
y -= rhs.y;
return *this;
}
bool operator==(const coord & other) const {
return x == other.x && y == other.y;
}
bool operator!=(const coord & other) const {
return !(*this == other);
}
double manhattan_dist(const coord & other) const {
return std::fabs(x-other.x) + std::fabs(y-other.y);
}
};
// And some helper routines for translating from directions to
// relative coordinates.
enum direction {NORTH, SOUTH, EAST, WEST, IDLE};
coord get_delta(direction dir);