-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepresentation.py
More file actions
305 lines (257 loc) · 11.8 KB
/
representation.py
File metadata and controls
305 lines (257 loc) · 11.8 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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
from enum import Enum
import math
class GridRelativeOrientation(Enum):
"""
Implementation for orthogonal and diagonal relative orientations in a rectangular grid.
"""
FRONT = 0
FRONT_RIGHT = 1
RIGHT = 2
BACK_RIGHT = 3
BACK = 4
BACK_LEFT = 5
LEFT = 6
FRONT_LEFT = 7
class GridOrientation(Enum):
NORTH = (0, 0, 1, "^")
EAST = (1, 1, 0, ">")
SOUTH = (2, 0, -1, "v")
WEST = (3, -1, 0, "<")
def __init__(self, ordinal, dx, dy, display_string):
self.ordinal = ordinal
self.dx = dx
self.dy = dy
self.display_string = display_string
def __str__(self):
return self.display_string
# NORTH = Orientation(1, 0, 1, "^")
# EAST = Orientation(2, 1, 0, ">")
# SOUTH = Orientation(3, 0, -1, "v")
# WEST = Orientation(4, -1, 0, "<")
def compute_relative_orientation(self, relative_orientation):
"""
Computes the absolute orientation that is at the specified orientation relative to `this'.
E.g. if the current orientation is SOUTH and the relative orientation is RIGHT, the result is WEST.
:param relative_orientation: the GridRelativeOrientation value
:return: the resulting GridOrientation instance
"""
angle = relative_orientation.value
is_half_angle = (angle % 2) != 0
if is_half_angle:
raise ValueError("the relative orientation must be a straight angle")
straight_angle = angle // 2
return list(GridOrientation)[(self.ordinal + straight_angle) % 4]
def get_relative_dx(self, relative):
"""
Returns the delta x for the position at the indicated orientation, relative to <code>this</code> orientation.
E.g. if the orientation is EAST, and the relative orientation is BACK-RIGHT, the resulting orientation is
SOUTH-WEST, with a delta x of -1 and a delta y of -1.
:param relative: the relative orientation
:return: the delta x of the position at that relative orientation.
"""
angle = relative.value
straight_angle = angle // 2
is_half_angle = angle % 2
straight_result = (self.ordinal + straight_angle) % 4
res = list(GridOrientation)[straight_result].dx
if is_half_angle:
res += list(GridOrientation)[(straight_result + 1) % 4].dx
return res
def get_relative_dy(self, relative):
"""
Returns the delta y for the position at the indicated orientation, relative to <code>this</code> orientation.
E.g. if the orientation is EAST, and the relative orientation is BACK-RIGHT, the resulting orientation is
SOUTH-WEST, with a delta x of -1 and a delta y of -1.
:param relative: the relative orientation
:return: the delta y of the position at that relative orientation.
"""
angle = relative.value
straight_angle = angle // 2
is_half_angle = angle % 2
straight_result = (self.ordinal + straight_angle) % 4
res = list(GridOrientation)[straight_result].dy
if is_half_angle:
res += list(GridOrientation)[(straight_result + 1) % 4].dy
return res
class GridPosition(object):
"""
Implementation for positions in a rectangular grid.
"""
def __init__(self, x, y):
self.__x = x
self.__y = y
def __eq__(self, other):
if isinstance(other, self.__class__):
return self.__x == other.__x and self.__y == other.__y
else:
return False
def __hash__(self):
return self.__x + self.__y
def __lt__(self, other):
if isinstance(other, self.__class__):
if self.__x < other.__x:
return True
elif self.__x == other.__x:
return self.__y < other.__y
else:
return False
def __gt__(self, other):
if isinstance(other, self.__class__):
if self.__x > other.__x:
return True
elif self.__x == other.__x:
return self.__y > other.__y
else:
return False
def __str__(self):
return "(%i, %i)" % (self.__x, self.__y)
# def get_neighbour_position(self, direction):
# """
# Returns the position that is one of the 4 orthogonal neighbors of <code>this</code> and is in a direction
# indicated by the orientation argument.
# E.g. if direction is NORTH, the returned position will be north of this.
# :param direction: direction of the neighbor position, as an `GridOrientation' instance.
# :return: the neighbour `GridPosition'
# """
# if direction == GridOrientation.NORTH:
# return GridPosition(self.__x, self.__y + 1)
# elif direction == GridOrientation.EAST:
# return GridPosition(self.__x + 1, self.__y)
# elif direction == GridOrientation.SOUTH:
# return GridPosition(self.__x, self.__y - 1)
# elif direction == GridOrientation.WEST:
# return GridPosition(self.__x - 1, self.__y)
#
# return None
def get_neighbour_position(self, direction, relative_orientation=GridRelativeOrientation.FRONT):
"""
Returns the position that is one of the 8 (orthogonal and diagonal) neighbors of <code>this</code> and is in a
direction that has the provided orientation relative to the provided direction.
:param direction: direction used to compute the neighbor position, as an `GridOrientation' instance.
:param relative_orientation: orientation of the returned position, relative to the direction
in the first argument. The default relative orientation is FRONT, which means the function will compute the
neighbour cell that is in the "direction" specified by :arg direction.
E.g. if direction is NORTH, the returned position will be north of this, relative to the grid.
If it is WEST, the returned position will be west of this, relative to the grid.
:return: the neighbor `GridPosition'
"""
return GridPosition(self.__x + direction.get_relative_dx(relative_orientation),
self.__y + direction.get_relative_dy(relative_orientation))
def get_neighbours(self, direction):
neighbours = []
for grid_relative_orientation in list(GridRelativeOrientation):
neighbours.append(self.get_neighbour_position(direction, grid_relative_orientation))
return neighbours
def is_neighbour(self, grid_position):
"""
Indicates whether the provided position is a neighbor (orthogonal or diagonal).
:param grid_position: the candidate `GridPosition' neighbour
:return: True if the candidate is a neighbour
"""
return abs(self.__x - grid_position.__x) <= 1 and abs(self.__y - grid_position.__y) <= 1
def is_neighbour_ortho(self, grid_position):
"""
Indicates whether the provided position is an orthogonal neighbor (north, south, east or west).
:param grid_position: the candidate neighbor.
:return: True if the candidate is a neighbour
"""
return (abs(self.__x - grid_position.__x) == 1 and self.__y == grid_position.__y) or \
(self.__x == grid_position.__x and abs(self.__y - grid_position.__y) == 1)
def get_simple_relative_orientation(self, other_position):
"""
Determines the relative orientation of another GridPosition with respect to `this'.
:param other_position: The position for which the relative orientation needs to be determined.
:return: The `GridRelativeOrientation' of `otherPosition' with respect to the current one.
"""
delta_x = other_position.__x - self.__x
if delta_x < 0:
delta_x = -1
elif delta_x > 0:
delta_x = 1
delta_y = other_position.__y - self.__y
if delta_y < 0:
delta_y = -1
elif delta_y > 0:
delta_y = 1
if delta_x == 0:
if delta_y >= 0:
return GridRelativeOrientation.FRONT
return GridRelativeOrientation.BACK
elif delta_x > 0:
if delta_y > 0:
return GridRelativeOrientation.FRONT_RIGHT
elif delta_y < 0:
return GridRelativeOrientation.BACK_RIGHT
else:
return GridRelativeOrientation.RIGHT
else:
if delta_y > 0:
return GridRelativeOrientation.FRONT_LEFT
elif delta_y < 0:
return GridRelativeOrientation.BACK_LEFT
else:
return GridRelativeOrientation.LEFT
def get_relative_orientation(self, reference_orientation, neighbour_position):
"""
Returns the relative orientation of a position relative to `this' position and a reference
orientation.
E.g. for a `GridOrientation' reference_orientation of EAST, an orthogonal `GridPosition' neighbor position to
the right of this position will be in FRONT.
:param reference_orientation: the absolute orientation which is considered as 'front', or 'forward'.
:param neighbour_position: the position for which to compute the relative orientation.
:return: the relative orientation.
"""
if not self.is_neighbour(neighbour_position):
raise ValueError("Given position is not a neighbor")
for orientation in list(GridRelativeOrientation):
if self.get_neighbour_position(reference_orientation, orientation) == neighbour_position:
return orientation
raise RuntimeError("Should not be here!")
def get_distance_to(self, grid_position):
"""
Computes the Manhatten distance from a specified position
:param grid_position: the other `GridPosition' position
:return: the distance
"""
return abs(self.__x - grid_position.__x) + abs(self.__y - grid_position.__y)
def get_chebyshev_distance_to(self, grid_position):
"""
Computes the Euclidean distance from a specified position
:param grid_position: the other `GridPosition' position
:return: the distance
"""
return max(abs(self.__x - grid_position.__x), abs(self.__y - grid_position.__y))
def get_euclidean_distance_to(self, grid_position):
"""
Computes the Euclidean distance from a specified position
:param grid_position: the other `GridPosition' position
:return: the distance
"""
return math.sqrt((self.__x - grid_position.__x) ** 2 + (self.__y - grid_position.__y) ** 2)
def is_x_even(self):
"""
:return: True if the x coordinate is even
"""
return abs(self.__x) % 2 == 0
def is_y_even(self):
"""
:return: True if the y coordinate is even
"""
return abs(self.__y) % 2 == 0
def get_x(self):
return self.__x
def get_y(self):
return self.__y
if __name__ == "__main__":
pos = GridPosition(1, 1)
print(pos.get_neighbour_position(GridOrientation.NORTH))
print(pos.get_neighbour_position(GridOrientation.EAST))
print(pos.get_neighbour_position(GridOrientation.WEST))
print(pos.get_neighbour_position(GridOrientation.SOUTH))
print("===========")
print(pos.get_neighbour_position(GridOrientation.SOUTH, GridRelativeOrientation.FRONT))
print(pos.get_neighbour_position(GridOrientation.SOUTH, GridRelativeOrientation.FRONT_LEFT))
print(pos.get_neighbour_position(GridOrientation.SOUTH, GridRelativeOrientation.FRONT_RIGHT))
print(pos.get_neighbour_position(GridOrientation.SOUTH, GridRelativeOrientation.BACK))
print(pos.get_neighbour_position(GridOrientation.SOUTH, GridRelativeOrientation.BACK_LEFT))
print(pos.get_neighbour_position(GridOrientation.SOUTH, GridRelativeOrientation.BACK_RIGHT))