def testGraph1(self): r1 = Rectangle([3, 3], [4, 4]) r2 = Rectangle([12, 12], [4, 4]) g = Grid(18, 18) g.draw_rect(r1) g.draw_rect(r2) start = (3, 5) goal = (10, 9) path1 = g.search_path(start, goal, manhattan_distance, g.search_hallway(goal)) g.draw_path(path1) start = (5, 3) goal = (12, 9) path2 = g.search_path(start, goal, manhattan_distance, g.search_hallway(goal)) start = (1, 5) goal = (11, 14) path3 = g.search_path(start, goal, manhattan_distance, g.search_hallway(goal)) path = path1 path += path2 if path2 else [] path += path3 if path3 else [] print g.dump(path, [' ', 'x', '.'])
def testSearchPathNoPath(self): r1 = Rectangle([3, 3], [4, 4]) r2 = Rectangle([12, 12], [4, 4]) g = Grid(15, 15) g.draw_rect(r1) g.draw_rect(r2) path = g.search_path((3, 3), (12, 12), manhattan_distance, g.is_walkable) self.assertFalse(path)
def testGraph2(self): r1 = Rectangle([3, 3], [4, 4]) r2 = Rectangle([12, 12], [4, 4]) g = Grid(15, 15) g.draw_rect(r1) g.draw_rect(r2) g.my_connect(r1, r2) print g.dump() path = g.search_path((3, 3), (12, 12), manhattan_distance, g.search_hallway((12, 12))) expected = [(3, 3), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (3, 9), (3, 10), (3, 11), (3, 12), (4, 12), (5, 12), (6, 12), (7, 12), (8, 12), (9, 12), (10, 12), (11, 12), (12, 12)]
def testGraph(self): r1 = Rectangle([3, 3], [4, 4]) r2 = Rectangle([12, 12], [4, 4]) g = Grid(15, 15) g.draw_rect(r1) g.draw_rect(r2) g.connect(r1, r2) path = g.search_path((3, 3), (12, 12), manhattan_distance, g.is_walkable) expected = [(3, 3), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (3, 9), (3, 10), (3, 11), (3, 12), (4, 12), (5, 12), (6, 12), (7, 12), (8, 12), (9, 12), (10, 12), (11, 12), (12, 12)] self.assertEqual(expected, path)
def testCreation(self): r = Rectangle((20, 10), (10, 10)) self.assertEqual((20, 10), r.center) self.assertEqual((10, 10), r.size) self.assertEqual(15, r.x1) self.assertEqual(5, r.y1) self.assertEqual(25, r.x2) self.assertEqual(15, r.y2)
def create_Rectangles(): r = Map() r.graph.add_node(Rectangle([4, 4], [6, 6])) r.graph.add_node(Rectangle([20, 10], [15, 10])) r.graph.add_node(Rectangle([10, 30], [15, 15])) r.graph.add_node(Rectangle([40, 30], [15, 15])) r.graph.add_node(Rectangle([40, 5], [10, 8])) r.graph.add_node(Rectangle([55, 15], [8, 8])) return r
def testGetDirection(self): r = Rectangle((10, 10), (4, 4)) self.assertEqual(Directions.top_left, r.get_direction(Rectangle((5, 5), (4, 4)))) self.assertEqual(Directions.top, r.get_direction(Rectangle((10, 5), (4, 4)))) self.assertEqual(Directions.top_right, r.get_direction(Rectangle((25, 5), (4, 4)))) self.assertEqual(Directions.left, r.get_direction(Rectangle((5, 10), (4, 4)))) self.assertEqual(Directions.right, r.get_direction(Rectangle((25, 10), (4, 4)))) self.assertEqual(Directions.bottom_left, r.get_direction(Rectangle((5, 25), (4, 4)))) self.assertEqual(Directions.bottom, r.get_direction(Rectangle((10, 25), (4, 4)))) self.assertEqual(Directions.bottom_right, r.get_direction(Rectangle((25, 25), (4, 4))))
#!/usr/local/bin/python from __future__ import print_function from map import Map, Rectangle from map.Placement import PlaceFinder, DefaultGravityCenterStrategy from scipy.spatial import distance r = Map() r.graph.add_node(Rectangle([5, 5], [5, 5])) r.graph.add_node(Rectangle([20, 10], [15, 10])) r.graph.add_node(Rectangle([10, 30], [15, 15])) r.graph.add_node(Rectangle([40, 30], [15, 15])) r.graph.add_node(Rectangle([40, 5], [10, 10])) r.graph.add_node(Rectangle([55, 15], [8, 8])) r.graph.nodes = sorted(r.graph.nodes, key=lambda r: distance.euclidean([20, 25], r.center)) print('-->', [n.id for n in r.graph.nodes]) # r = Map.random([20, 25], 9, width = 40, height = 45, minSize = 5, maxSize = 8) # finder = PlaceFinder(r.graph.nodes, DefaultGravityCenterStrategy(), 3) # finder.find() # print('-' * 50) # print([str(n.xy) for n in r.graph.nodes]) r.graph.triangulate() print('-' * 50) for i, Rectangle in enumerate(r.graph.nodes):