def main(): # grid = Grid(coordinate_system=OFFSET, hexagon_type=FLAT) # grid[-1, -1] = None # grid[-1, 0] = None # grid[0, 0] = None # grid[0, 1] = None # grid[1, 0] = None # grid[1, 1] = None # grid[1, 2] = None # path = grid.shortest_path_coordinates((-1, -1), (1, 2)) # print(path) grid = Grid(hexagon_type=HexagonType.FLAT, coordinate_system=CoordinateSystem.CUBIC) for i in range(-3, 4): for j in range(-3, 4): c = Grid.convert((i, j), CoordinateSystem.AXIAL, CoordinateSystem.CUBIC) grid[c] = None path = grid.shortest_path_coordinates((-3, 2, 1), (3, -2, -1)) draw = DrawGrid(grid) draw.draw_hexagons(grid.keys(), labels=True, fill='#7070ff') draw.draw_hexagons(path, labels=True, fill='#ff7070') draw.draw()
def test_offset_conversion(self): coords = [(0, 0), (0, 1), (0, 2), (-1, 2), (3, -4)] expected = [(0, 0), (1, 1), (0, 2), (-1, 2), (3, -4)] for c, e in zip(coords, expected): out = Grid.convert(c, OFFSET_ODD_ROWS, OFFSET_EVEN_ROWS) self.assertSequenceEqual(out, e) coords = [(0, 0), (0, 1), (0, 2), (-1, 2), (3, -4)] expected = [(0, 0), (0, 1), (-1, 1), (-2, 1), (5, -2)] for c, e in zip(coords, expected): out = Grid.convert(c, OFFSET_ODD_ROWS, OFFSET_ODD_COLUMNS) self.assertSequenceEqual(out, e) coords = [(0, 0), (0, 1), (0, 2), (-1, 2), (3, -4)] expected = [(0, 0), (0, 1), (-1, 2), (-2, 1), (5, -1)] for c, e in zip(coords, expected): out = Grid.convert(c, OFFSET_ODD_ROWS, OFFSET_EVEN_COLUMNS) self.assertSequenceEqual(out, e)
def main(): g = Grid(hexagon_type=HexagonType.FLAT, coordinate_system=CoordinateSystem.CUBIC) for i in range(-3, 4): for j in range(-3, 4): c = Grid.convert((i, j), CoordinateSystem.AXIAL, CoordinateSystem.CUBIC) g[c] = None # g = Grid(HexagonType.FLAT, CoordinateSystem.AXIAL) # for i in range(5): # for j in range(5): # g[i, j] = None draw_obj = DrawGrid(g) draw_obj.draw_hexagons(g.keys(), labels=True, fill='#7070ff') draw_obj.draw()
def test_within(self): g = Grid(hexagon_type=HexagonType.FLAT, coordinate_system=CoordinateSystem.OFFSET) for i in range(-3, 4): for j in range(-3, 4): g[i, j] = None expected = [None] * 7 self.assertCountEqual(g.within((0, 0), 1), expected) g = Grid(hexagon_type=HexagonType.FLAT, coordinate_system=CoordinateSystem.CUBIC) for i in range(-3, 4): for j in range(-3, 4): c = Grid.convert((i, j), CoordinateSystem.AXIAL, CoordinateSystem.CUBIC) g[c] = None expected = [None] * 7 self.assertCountEqual(g.within((2, 0, -2), 1), expected)
def test_ring(self): g = Grid(HexagonType.FLAT, CoordinateSystem.AXIAL) for i in range(5): for j in range(5): g[i, j] = None expected = [None] * 3 self.assertCountEqual(g.ring((0, 0), 2), expected) expected = g.neighbors((1, 2)) self.assertCountEqual(g.ring((1, 2), 1), expected) g = Grid(hexagon_type=HexagonType.FLAT, coordinate_system=CoordinateSystem.CUBIC) for i in range(-3, 4): for j in range(-3, 4): c = Grid.convert((i, j), CoordinateSystem.AXIAL, CoordinateSystem.CUBIC) g[c] = None expected = [None] * 4 self.assertCountEqual(g.ring((3, -6, 3), 3), expected)
def test_within_coordinates(self): g = Grid(hexagon_type=HexagonType.FLAT, coordinate_system=CoordinateSystem.OFFSET) for i in range(-3, 4): for j in range(-3, 4): g[i, j] = None expected = [(0, 0), (0, -1), (0, 1), (1, -1), (1, 0), (-1, -1), (-1, 0)] self.assertCountEqual(g.within_coordinates((0, 0), 1, validate=True), expected) expected = [(0, 0), (0, -1), (0, 1), (1, -1), (1, 0), (-1, -1), (-1, 0), (0, -2), (1, -2), (2, -1), (2, 0), (2, 1), (1, 1), (0, 2), (-1, 1), (-2, 1), (-2, 0), (-2, -1), (-1, -2)] self.assertCountEqual(g.within_coordinates((0, 0), 2, validate=True), expected) g = Grid(hexagon_type=HexagonType.FLAT, coordinate_system=CoordinateSystem.CUBIC) for i in range(-3, 4): for j in range(-3, 4): c = Grid.convert((i, j), CoordinateSystem.AXIAL, CoordinateSystem.CUBIC) g[c] = None expected = [(2, 0, -2), (2, 1, -3), (3, 0, -3), (3, -1, -2), (2, -1, -1), (1, 0, -1), (1, 1, -2)] self.assertCountEqual( g.within_coordinates((2, 0, -2), 1, validate=True), expected) expected = [(2, 0, -2), (2, 1, -3), (3, 0, -3), (3, -1, -2), (2, -1, -1), (1, 0, -1), (1, 1, -2), (3, -2, -1), (2, -2, 0), (1, -1, 0), (0, 0, 0), (0, 1, -1), (0, 2, -2), (1, 2, -3)] self.assertCountEqual( g.within_coordinates((2, 0, -2), 2, validate=True), expected)
def test_ring_coordinates(self): g = Grid(HexagonType.FLAT, CoordinateSystem.AXIAL) for i in range(5): for j in range(5): g[i, j] = None expected = [(0, 2), (1, 1), (2, 0)] self.assertCountEqual(g.ring_coordinates((0, 0), 2, validate=True), expected) expected = g.neighbor_coordinates((1, 2), validate=True) self.assertCountEqual(g.ring_coordinates((1, 2), 1, validate=True), expected) g = Grid(hexagon_type=HexagonType.FLAT, coordinate_system=CoordinateSystem.CUBIC) for i in range(-3, 4): for j in range(-3, 4): c = Grid.convert((i, j), CoordinateSystem.AXIAL, CoordinateSystem.CUBIC) g[c] = None expected = [(0, -3, 3), (1, -3, 2), (2, -3, 1), (3, -3, 0)] self.assertCountEqual(g.ring_coordinates((3, -6, 3), 3), expected)
def test_conversion_invertible(self): c = (2, -6, 4) out = Grid.convert(c, CUBIC, AXIAL) self.assertSequenceEqual(out, (2, 4)) self.assertSequenceEqual(c, Grid.convert(out, AXIAL, CUBIC)) # Make sure conversions are invertible. coords = [(0, 0), (1, 1), (2, 3), (-2, 2), (2, 15), (20, 2), (2, -2), (-13.5, 2372.2)] for c in coords: for from_sys in [ AXIAL, OFFSET_EVEN_COLUMNS, OFFSET_ODD_COLUMNS, OFFSET_EVEN_ROWS, OFFSET_ODD_ROWS ]: for to_sys in [ AXIAL, OFFSET_EVEN_COLUMNS, OFFSET_ODD_COLUMNS, OFFSET_EVEN_ROWS, OFFSET_ODD_ROWS ]: out = Grid.convert(c, from_sys, to_sys) self.assertSequenceEqual( c, Grid.convert(out, to_sys, from_sys)) # Recall that cubic coordinates must sum to 0... coords = [(0, 0, 0), (1, -2, 1), (1, 2, -3), (10.5, -10.5, 0)] for c in coords: for to_sys in [ CUBIC, AXIAL, OFFSET_EVEN_COLUMNS, OFFSET_ODD_COLUMNS, OFFSET_EVEN_ROWS, OFFSET_ODD_ROWS ]: out = Grid.convert(c, CUBIC, to_sys) self.assertSequenceEqual(c, Grid.convert(out, to_sys, CUBIC)) # Make sure cubic coordinates pass through completely unchanged. for c in coords: out = Grid.convert(c, CUBIC, CUBIC) self.assertSequenceEqual(c, out)