def divide(self, cell): # create 4 new cells cell_nw = VariableSquareCell.create_initialized(self.rule, self.neighbourhood, self.cell_state_class) cell_ne = VariableSquareCell.create_initialized(self.rule, self.neighbourhood, self.cell_state_class) cell_sw = VariableSquareCell.create_initialized(self.rule, self.neighbourhood, self.cell_state_class) cell_se = VariableSquareCell.create_initialized(self.rule, self.neighbourhood, self.cell_state_class) cell_nw.size = cell.size / 4 cell_ne.size = cell.size / 4 cell_sw.size = cell.size / 4 cell_se.size = cell.size / 4 # position half_radius = cell.radius / 2 cell_nw.position = (cell.x - half_radius, cell.y - half_radius) cell_nw.radius = half_radius cell_ne.position = (cell.x + half_radius, cell.y - half_radius) cell_ne.radius = half_radius cell_sw.position = (cell.x - half_radius, cell.y + half_radius) cell_sw.radius = half_radius cell_se.position = (cell.x + half_radius, cell.y + half_radius) cell_se.radius = half_radius # create neighbor connections cell_nw.neighs["south"] = set([cell_sw]) cell_nw.neighs["east"] = set([cell_ne]) cell_ne.neighs["south"] = set([cell_se]) cell_ne.neighs["west"] = set([cell_nw]) cell_sw.neighs["north"] = set([cell_nw]) cell_sw.neighs["east"] = set([cell_se]) cell_se.neighs["north"] = set([cell_ne]) cell_se.neighs["west"] = set([cell_sw]) new_cells = {} new_cells["north"] = [cell_nw, cell_ne] new_cells["east"] = [cell_ne, cell_se] new_cells["south"] = [cell_se, cell_sw] new_cells["west"] = [cell_nw, cell_sw] for direction, direction_neighs in cell.neighs.items(): for direction_neigh in direction_neighs: for new_cell in new_cells[direction]: if self.is_neighbor_with(direction_neigh, new_cell): new_cell.neighs[direction].add(direction_neigh) # now we have new cells with correct neighs. # this time we have to update rest of the neighborhood for new_cell in [cell_nw, cell_ne, cell_sw, cell_se]: self.update_surrounding_neighbourhood(new_cell, [self]) return [cell], [cell_nw, cell_ne, cell_sw, cell_se]
def create_new_cell(self, cells_to_merge): new_cell = VariableSquareCell.create_initialized( self.rule, self.neighbourhood, self.cell_state_class) new_cell.position = self.interpolate_center(cells_to_merge) new_cell.size = len(cells_to_merge) * cells_to_merge[0].size new_cell.radius = cells_to_merge[0].radius * 2 return new_cell
def create_cells(self, rule): cells = {} for x in range(0, self.width, self.resolution): for y in range(0, self.height, self.resolution): coordinates = (x + self.resolution / 2, y + self.resolution / 2) cells[coordinates] = VariableSquareCell.create_initialized( rule, self.neighbourhood, self.cell_state_class) cells[coordinates].position = coordinates cells[coordinates].radius = self.resolution / 2 return cells