def test_find_color(self): board = GameBoard('tests/test_puzzle.txt') board.read_board() rect = Rectangle(board, Point(0, 0), Point(0, 0), 3) rect.draw_rectangle() rect = Rectangle(board, Point(0, 0), Point(2, 2), 4) rect.find_color() self.assertEqual(rect.color, 3)
def _convert_ranges_into_coordinates(self, basis, ranges): for first_scaled, second_scaled in ranges: if self.projection.orientation == Projection.TYPE_VERTICAL: yield Rectangle(basis.top + first_scaled, second_scaled - first_scaled, basis.left, basis.width) else: yield Rectangle(basis.top, basis.height, basis.left + first_scaled, second_scaled - first_scaled)
def initialize_board_solution(self): from utilities.rectangle import Rectangle initial_solution_state = [[-1 for c in range(self.cols)] for r in range(self.rows)] for i in range(len(self.blocks)): initial_solution_state[self.blocks[i].row][self.blocks[i].col] = i self.solution = deepcopy(initial_solution_state) # cover the solution with all possible rectangles for all blocks for k in range(len(self.blocks)): curr_row = self.blocks[k].row curr_col = self.blocks[k].col curr_value = self.blocks[k].value for factor in self.blocks[k].factors: for i in range(curr_value // factor): for j in range(factor): top_left = Point( curr_col + i - curr_value // factor + 1, curr_row - j) bottom_right = Point(curr_col + i, curr_row + factor - 1 - j) try: rect = Rectangle(self, top_left, bottom_right, k) rect.draw_rectangle() except AssertionError: continue self._update_final_cells_values() self.solution = initial_solution_state
def test_draw_rect(self): board = GameBoard('tests/test_puzzle.txt') board.read_board() rect = Rectangle(board, Point(0, 0), Point(2, 1), 4) rect.draw_rectangle() for y in range(2): for x in range(3): self.assertEqual(board.solution[y][x], 4)
def test_rectangle_init(self): board = GameBoard('tests/test_puzzle.txt') board.read_board() rect = Rectangle(board, Point(0, 1), Point(2, 2), 0) self.assertEqual(rect.board, board) self.assertEqual(rect.color, 0) self.assertEqual(rect.top_left, Point(0, 1)) self.assertEqual(rect.bottom_right, Point(2, 2))
def update_solution(self, first_point: Point, second_point: Point): self.board.solution = deepcopy(self.solution_backup) for i in range(self.board.rows): for j in range(self.board.cols): self.squares[i][j].color = self.squares_backup[i][j] rect = Rectangle(self.board, first_point, second_point, 0) rect.find_color() color = (250, 190, 250) if rect.color == -1 \ else COLORS[rect.color % (len(COLORS) - 1)][0:3] rect.draw_rectangle() for x in range(rect.top_left.x, rect.bottom_right.x + 1): for y in range(rect.top_left.y, rect.bottom_right.y + 1): self.squares[self.board.cols - y - 1][x].color = color
def backtrack(self, block_pointer: int = 0): # if all blocks are visited the solution is found if block_pointer > len(self.board.blocks) - 1: return True curr_block = self.board.blocks[block_pointer] # go through all block's factors while curr_block.factor_pointer < len(curr_block.factors): curr_factor = curr_block.factors[curr_block.factor_pointer] # go through all rectangles # curr_factor * curr_block.value // curr_factor for i in range(curr_block.value // curr_factor): for j in range(curr_factor): top_left = Point( curr_block.col + i - curr_block.value // curr_factor+1, curr_block.row - j) bottom_right = Point( curr_block.col + i, curr_block.row + curr_factor - 1 - j) try: rect = Rectangle( self.board, top_left, bottom_right, block_pointer) if not rect.check_conflicts(): rect.draw_rectangle() if self._is_area_full_covered(block_pointer): # true means correct solution is found if self.backtrack(block_pointer + 1): return True # incorrect state -> reset changes rect.clear() # colorize cell self.board.solution[ curr_block.row][curr_block.col] = block_pointer except AssertionError: continue self.board.blocks[block_pointer].factor_pointer += 1 # if no rectangles are found, reset pointer if block_pointer > 0: self.board.blocks[block_pointer].factor_pointer = 0
def is_point_within_dist_of_rect(rect=Rectangle(), point=Point(), dist=0.0): if ((rect.bottom_left.x - dist) < point.x and point.x < (rect.bottom_left.x + rect.width + dist)): if (point.x < rect.bottom_left.x): a = rect.bottom_left.x - point.x y_max = rect.bottom_left.y + rect.height + math.sqrt(dist**2 - a**2) y_min = rect.bottom_left.y - math.sqrt(dist**2 - a**2) if ((y_min < point.y) and point.y < y_max): return True else: return False elif (point.x < (rect.bottom_left.x + rect.width)): y_max = rect.bottom_left.y + rect.height + dist y_min = rect.bottom_left.y - dist if ((y_min < point.y) and point.y < y_max): return True else: return False else: a = rect.bottom_left.x + rect.width - point.x y_max = rect.bottom_left.y + rect.height + math.sqrt(dist**2 - a**2) y_min = rect.bottom_left.y - math.sqrt(dist**2 - a**2) if ((y_min < point.y) and point.y < y_max): return True else: return False return False
def rectangle_contain(self, r1=Rectangle(), r2=Rectangle()): v = (r1.x <= r2.x) and (r1.y <= r2.y) and ( r1.x + r1.w >= r2.x + r2.w) and (r1.y + r1.h >= r2.y + r2.h) and ( r1.x + r1.w > r2.x) and (r1.y + r1.h > r2.y) return v