def addEdge(self, event): self.graph.addEdge(self.startVertex.id, self.endVertex.id) newEdge = Edge(-1, canvas, self.startVertex, self.endVertex, self.directed) self.edges.append(newEdge) newEdge.draw() #reset start vertex for next run self.startVertex = None self.endVertex = None
def __init__(self, x_shift: int=0, y_shift: int=0): super().__init__() self._cfg: Config = Config() self._x_shift: int = x_shift self._y_shift: int = y_shift self._screen: pg.Surface = pg.display.get_surface() self._highlighted_edge = None # Create the dots (a 2D list of Dot objects) self._dots: list[list[Dot]] = [] for r in range(0, self._cfg.CELL_ROWS + 1): row = [] for c in range(0, self._cfg.CELL_COLS + 1): dot = Dot( ((self._cfg.DOT_DIA + self._cfg.CELL_WIDTH) * c + self._cfg.GUTTER_WIDTH + self._x_shift, (self._cfg.DOT_DIA + self._cfg.CELL_HEIGHT) * r + self._cfg.GUTTER_WIDTH + self._y_shift), (self._cfg.DOT_DIA, self._cfg.DOT_DIA), BLACK ) row.append(dot) dot.draw() self._dots.append(row) # Create the cells (a 2D list of Cell objects) self._cells: list[list[Cell]] = [] for r in range(0, self._cfg.CELL_ROWS): row = [] for c in range(0, self._cfg.CELL_COLS): cell = Cell( ((c + 1) * self._cfg.DOT_DIA + c * self._cfg.CELL_WIDTH + self._cfg.GUTTER_WIDTH + self._x_shift, (r + 1) * self._cfg.DOT_DIA + r * self._cfg.CELL_HEIGHT + self._cfg.GUTTER_WIDTH + self._y_shift), (self._cfg.CELL_WIDTH, self._cfg.CELL_HEIGHT), BACKGROUND_COLOR ) row.append(cell) cell.draw() self._cells.append(row) # Create the edges (a 2D list of Edge objects) # Superrows and supercolumns are rows and columns of a collection # of one dot, two edges, and one cell. This allows for easy grouping # and lookup of edges based on mouse location. self._edges: list[list[Edge]] = [] for r in range(0, self._cfg.CELL_ROWS + 1): row = [] for c in range(0, self._cfg.CELL_COLS + 1): h_edge, v_edge = None, None if c < self._cfg.CELL_COLS: # horizontal edge h_edge = Edge( ((c + 1) * self._cfg.DOT_DIA + c * self._cfg.CELL_WIDTH + self._cfg.GUTTER_WIDTH + self._x_shift, r * self._cfg.DOT_DIA + r * self._cfg.CELL_HEIGHT + self._cfg.GUTTER_WIDTH + self._y_shift), (self._cfg.CELL_WIDTH, self._cfg.DOT_DIA), EDGE_COLOR_DEFAULT ) h_edge.draw() # Establish cell to edge relationships - for checking whether a # cell is captured by a user, we need to know which cells the # edge touches. An edge always touches one or two cells. Edges # that touch one cell are the edges that make up the exterior # of the board. # top superrow if h_edge and r == 0: h_edge.cell2 = self._cells[r][c] self._cells[r][c].edge_top = h_edge # bottom superrow elif h_edge and r == self._cfg.CELL_ROWS: h_edge.cell1 = self._cells[r-1][c] self._cells[r-1][c].edge_bottom = h_edge # all other superrows elif h_edge: h_edge.cell1 = self._cells[r-1][c] h_edge.cell2 = self._cells[r][c] self._cells[r][c].edge_top = h_edge self._cells[r-1][c].edge_bottom = h_edge if r < self._cfg.CELL_ROWS: # vertical edge v_edge = Edge( (c * self._cfg.DOT_DIA + c * self._cfg.CELL_WIDTH + self._cfg.GUTTER_WIDTH + self._x_shift, (r + 1) * self._cfg.DOT_DIA + r * self._cfg.CELL_HEIGHT + self._cfg.GUTTER_WIDTH + self._y_shift), (self._cfg.DOT_DIA, self._cfg.CELL_HEIGHT), EDGE_COLOR_DEFAULT ) v_edge.draw() # Establish cell to edge relationships - for checking whether a # cell is captured by a user, we need to know which edges the # cell touches. A cell always touches four edges: top, bottom, # left and right. # left supercolumn if v_edge and c == 0: v_edge.cell2 = self._cells[r][c] self._cells[r][c].edge_left = v_edge # right supercolumn elif v_edge and c == self._cfg.CELL_COLS: v_edge.cell1 = self._cells[r][c-1] self._cells[r][c-1].edge_right = v_edge # all other supercolumns elif v_edge: v_edge.cell1 = self._cells[r][c-1] v_edge.cell2 = self._cells[r][c] self._cells[r][c].edge_left = v_edge self._cells[r][c-1].edge_right = v_edge # Group the edges into tuples with (usually) two edges per # tuple. Each tuple is stored by row,col in the edges 2-D list. # The last column will only have the vertical edge in the tuple. # The last row will only have the horizontal edge in the tuple. # The last row and column will be empty. # Each member of the tuple is a dict that stores a reference to # the pg.Rect that represents the edge, its capture status, and # a tuple of cells it touches. if h_edge and v_edge: row.append((h_edge, v_edge)) elif h_edge: # bottom row row.append((h_edge,)) elif v_edge: # right col row.append((v_edge,)) else: # bottom, right corner row.append(()) self._edges.append(row) self.draw()