-
Notifications
You must be signed in to change notification settings - Fork 2
/
geometry.h
232 lines (195 loc) · 4.79 KB
/
geometry.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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#ifndef _GEOMETRY_H_
#define _GEOMETRY_H_
#include <vector>
#include <string>
#ifndef MIN
#define MIN(x, y) ( (x) < (y) ? x : y)
#endif
#ifndef MOD
#define MOD(a, n) ( (a) < 0 ? ((a) % (n) + (n)) : ((a) % (n)))
#endif
#ifndef PI
#define PI 3.142
#endif
enum Direction
{
DIR_NULL = 0,
DIR_NORTH,
DIR_EAST,
DIR_SOUTH,
DIR_WEST
};
enum Direction_full
{
DIRFULL_NULL = 0,
DIRFULL_NORTH,
DIRFULL_NORTHEAST,
DIRFULL_EAST,
DIRFULL_SOUTHEAST,
DIRFULL_SOUTH,
DIRFULL_SOUTHWEST,
DIRFULL_WEST,
DIRFULL_NORTHWEST
};
std::string Direction_name(Direction dir);
std::string Direction_name(Direction_full dir);
struct Point
{
int x;
int y;
Point(int X = 0, int Y = 0) : x (X), y (Y) {}
Point(const Point &p) : x (p.x), y (p.y) {}
~Point(){}
std::string str() const;
bool operator==(const Point &other) const
{
return (x == other.x && y == other.y);
}
bool operator!=(const Point &other) const
{
return !(*this == other);
}
Point& operator +=(const Point &rhs)
{
x += rhs.x;
y += rhs.y;
return *this;
}
Point& operator -=(const Point &rhs)
{
x -= rhs.x;
y -= rhs.y;
return *this;
}
};
inline Point operator+(Point lhs, const Point& rhs)
{
lhs += rhs;
return lhs;
}
struct Tripoint
{
int x;
int y;
int z;
Tripoint(int X = 0, int Y = 0, int Z = 0) : x (X), y (Y), z (Z) {}
Tripoint(const Tripoint &p) : x (p.x), y (p.y), z (p.z) {}
~Tripoint(){}
std::string str();
bool operator==(const Tripoint &other) const
{
return (x == other.x && y == other.y && z == other.z);
}
bool operator!=(const Tripoint &other) const
{
return !(*this == other);
}
Tripoint& operator +=(const Tripoint &rhs)
{
x += rhs.x;
y += rhs.y;
z += rhs.z;
return *this;
}
Tripoint& operator +=(const Point &rhs)
{
x += rhs.x;
y += rhs.y;
return *this;
}
Tripoint& operator -=(const Tripoint &rhs)
{
x -= rhs.x;
y -= rhs.y;
z -= rhs.z;
return *this;
}
Tripoint& operator -=(const Point &rhs)
{
x -= rhs.x;
y -= rhs.y;
return *this;
}
operator Point()
{
Point ret;
ret.x = x;
ret.y = y;
return ret;
}
};
inline Tripoint operator+(Tripoint lhs, const Tripoint& rhs)
{
lhs += rhs;
return lhs;
}
inline Tripoint operator+(Tripoint lhs, const Point& rhs)
{
lhs += rhs;
return lhs;
}
inline Tripoint operator-(Tripoint lhs, const Tripoint& rhs)
{
lhs -= rhs;
return lhs;
}
inline Tripoint operator-(Tripoint lhs, const Point& rhs)
{
lhs -= rhs;
return lhs;
}
struct Pointcomp
{
bool operator() (const Point &lhs, const Point &rhs) const
{
if (lhs.x < rhs.x) return true;
if (lhs.x > rhs.x) return false;
if (lhs.y < rhs.y) return true;
if (lhs.y > rhs.y) return false;
return false;
}
};
struct Tripointcomp
{
bool operator() (const Tripoint &lhs, const Tripoint &rhs) const
{
if (lhs.x < rhs.x) return true;
if (lhs.x > rhs.x) return false;
if (lhs.y < rhs.y) return true;
if (lhs.y > rhs.y) return false;
if (lhs.z < rhs.z) return true;
if (lhs.z > rhs.z) return false;
return false;
}
};
std::vector<Point> line_to(int x0, int y0, int x1, int y1);
std::vector<Point> line_to(Point origin, Point target);
std::vector<Tripoint> line_to(Tripoint origin, Tripoint target);
std::vector<Tripoint> line_to(int x0, int y0, int z0, int x1, int y1, int z1);
int rl_dist (int x0, int y0, int x1, int y1);
int rl_dist (Point origin, Point target);
int rl_dist (int x0, int y0, int z0, int x1, int y1, int z1);
int rl_dist (Tripoint origin, Tripoint target);
// sqrt(dX^2 + dY^2)
int trig_dist (int x0, int y0, int x1, int y1);
int trig_dist (Point origin, Point target);
// dX + dY
int manhattan_dist(int x0, int y0, int x1, int y1);
int manhattan_dist(Point origin, Point target);
int manhattan_dist(int x0, int y0, int z0, int x1, int y1, int z1);
int manhattan_dist(Tripoint origin, Tripoint target);
/* Direction origin moves to reach target
* This is GENERAL direction, which means that if target is 500 tiles to the
* north and 5 tiles to the west, we return north since any reasonable
* observe would call that the "north" and not the "northwest."
* Generally, if dY >= 2 * dX then treat dX as 0, etc.
* All four are functionally equivalent and ignore Z for now.
*/
Direction_full get_general_direction(Point origin, Point target);
Direction_full get_general_direction(Tripoint origin, Point target);
Direction_full get_general_direction(Point origin, Tripoint target);
Direction_full get_general_direction(Tripoint origin, Tripoint target);
// points_at_exact_distance() returns a list of all points exactly dist tiles
// (using rl_dist() calculation) from origin. Basically, a circle.
std::vector<Point> points_at_exact_distance(Point origin, int dist);
#endif