Exemplo n.º 1
0
    def generate_points(self):  #AUTO-GENERATES END POINTS
        maze = csv.open_integer_grid(self.maze_csv_name)
        self.end_points = [-1, -1, -1, -1]

        for y in [0, len(maze) - 1]:  #SEARCH FIRST AND LAST ROW FOR PATH CELL
            if -1 not in self.end_points[2:4]:
                break  #RETURN IF BOTH POINTS FOUND
            for x in range(len(maze[y])):
                update.check_quit()
                if maze[y][x] == 0 and -1 in self.end_points[
                        0:2]:  #IF NEITHER POINT FOUND YET
                    self.end_points[0:2] = [x, y]
                elif maze[y][x] == 0:
                    self.end_points[2:4] = [x, y]
                    break

        for x in [0,
                  len(maze[0]) - 1]:  #DO THE SAME WITH FIRST AND LAST COLUMN
            if -1 not in self.end_points[2:4]: break
            for y in range(len(maze)):
                update.check_quit()
                if maze[y][x] == 0 and -1 in self.end_points[0:2]:
                    self.end_points[0:2] = [x, y]
                elif maze[y][x] == 0:
                    self.end_points[2:4] = [x, y]
                    break
Exemplo n.º 2
0
    def display_path(self):  #RETURNS PIL IMAGE OF MAZE WITH PATH OVERLAYED
        if self.path == None: return None
        total = len(self.path)

        if self.rectilinear and not self.edited:  #ALLOWS PATH TO BE OVERLAYED ON ORIGINAL IMAGE
            display_image = self.rgb()
            walls = csv.open_integer_grid(self.wall_csv_name)
            draw = ImageDraw.Draw(
                display_image)  #ALLOWS SHAPES TO BE DRAWN ON IMAGE
            prev = self.path[0]
            for i, point in zip(range(total), self.path):
                if prev[0] % 2 == 0:
                    x1 = round(
                        walls[prev[0] // 2][0] + self.wall_size /
                        2)  #CONVERTS PATH INDEX TO PIXEL POS USING WALL POS
                else:
                    x1 = round(walls[prev[0] // 2][0] +
                               (self.wall_size + self.cell_size) / 2)
                if prev[1] % 2 == 0:
                    y1 = round(walls[prev[1] // 2][1] + self.wall_size / 2)
                else:
                    y1 = round(walls[prev[1] // 2][1] +
                               (self.wall_size + self.cell_size) / 2)

                if point[0] % 2 == 0:
                    x2 = round(walls[point[0] // 2][0] + self.wall_size / 2)
                else:
                    x2 = round(walls[point[0] // 2][0] +
                               (self.wall_size + self.cell_size) / 2)
                if point[1] % 2 == 0:
                    y2 = round(walls[point[1] // 2][1] + self.wall_size / 2)
                else:
                    y2 = round(walls[point[1] // 2][1] +
                               (self.wall_size + self.cell_size) / 2)
                    #USE COLOUR GRADIENT TO DRAW PATH
                draw.line((x1, y1, x2, y2), fill=self.path_colour(i, total))
                prev = point
            return display_image

        if self.edited:  #IF EDITED MAZE GRID MUST BE USED
            display_image = self.display_maze()
            draw = ImageDraw.Draw(display_image)
            prev = self.path[0]
            for i, point in zip(range(total), self.path):
                draw.line((prev[0], prev[1], point[0], point[1]),
                          fill=self.path_colour(i, total))
                prev = point
            return display_image

        else:  #IF UNEDITED PATH CAN BE OVERLAYED ON ORIGINAL
            display_image = self.rgb()
            draw = ImageDraw.Draw(display_image)
            prev = self.path[0]
            for i, point in zip(range(total), self.path):
                draw.line((prev[0], prev[1], point[0], point[1]),
                          fill=self.path_colour(i, total))
                prev = point
            return display_image
Exemplo n.º 3
0
 def display_maze(self):  #RETURNS PIL IMAGE OF MAZE GRID
     grid = csv.open_integer_grid(self.maze_csv_name)
     display_image = PIL.new('RGB', (len(grid[0]), len(grid)))
     pixels = display_image.load()
     for y in range(len(grid)):
         for x in range(len(grid[y])):
             if grid[y][
                     x] == 0:  #BINARY MAZE GRID REPRESENTED BY BLACK/WHITE PIXELS
                 pixels[x, y] = (255, 255, 255)
             elif grid[y][x] == 1:
                 pixels[x, y] = (0, 0, 0)
     return display_image
Exemplo n.º 4
0
 def set_end_points(self, value):
     if not isinstance(value, list):
         raise TypeError('Value must be of type list')
     if not all(isinstance(x, int) for x in value):
         raise TypeError('End points must be integers')
     if len(value) != 4:
         raise ValueError('There must be 4 end point values')
     grid = csv.open_integer_grid(self.maze_csv_name)
     try:
         test = grid[value[1]][value[0]] + grid[value[3]][value[2]]
     except:
         raise ValueError('End points must be within maze boundaries')
     self.end_points = value
Exemplo n.º 5
0
    def generate_nodes(self):  #FINDS NODES USING R MODULES
        directory = os.getcwd()
        cmd_path = '/usr/local/bin/Rscript'
        input_text = directory + ' ' + self.maze_csv_name + ' ' + update.get_quit_file(
        ) + ' ' + update.get_update_file()
        script_path = directory + '/rectangles.r'
        if not self.rectilinear:  #USE RSR TO REDUCE NODE AMOUNTS FOR NON-RECTILINEAR
            rect_csv_file = subprocess.check_output(
                [cmd_path, script_path, input_text], universal_newlines=True)
            if rect_csv_file == 'quit':
                raise update.Quit

        input_text += ' ' + str(self.rectilinear)
        script_path = directory + '/nodes.r'
        for point in self.end_points:
            input_text += ' ' + str(
                point + 1)  #INCREMENT INDEXES AS R STARTS AT 1 NOT 0
        if not self.rectilinear: input_text += ' ' + rect_csv_file
        #CALL R PROGRAM AND WAIT FOR OUTPUT
        data = subprocess.check_output([cmd_path, script_path, input_text],
                                       universal_newlines=True)
        if data == 'quit':  #QUIT IF R PROGRAM HAS BEEN QUIT
            raise update.Quit
        nodes_grid = csv.open_integer_grid(data)
        self.nodes = structures.Hash_Table(
            len(nodes_grid))  #HASH TABLE OF NODES AND NEIGHBOURS
        for node in nodes_grid:  #JOIN NODES TOGETHER IN BOTH DIRECTIONS
            node_id = str(node[0]) + '-' + str(node[1])
            self.nodes[node_id] = []
            if node[2] != -1:
                nbr_id = str(node[2]) + '-' + str(node[1])
                self.nodes[node_id].append(nbr_id)
                self.nodes[nbr_id].append(node_id)
            if node[3] != -1:
                nbr_id = str(node[0]) + '-' + str(node[3])
                self.nodes[node_id].append(nbr_id)
                self.nodes[nbr_id].append(node_id)