def _get_distance_between_points(self, p1, p2): if Validator.is_type([p1, p2], tuple) and Validator.is_number( [p1[0], p1[1], p2[0], p2[1]]): return math.sqrt((p1[0] - p2[0])**2 + (p1[1] - p2[1])**2) raise InappropriateArgsError( "calculating distance between points p1: " + str(p1) + " and p2: " + str(p2))
def _get_points_on_line(self, rho, theta): if Validator.is_number([rho, theta]): a, b = np.cos(theta), np.sin(theta) x0, y0 = a * rho, b * rho return int(x0 + 1000 * (-b)), int(y0 + 1000 * a), int(x0 - 1000 * (-b)), int(y0 - 1000 * a) raise InappropriateArgsError("getting points with rho: " + str(rho) + " and theta: " + str(theta))
def _get_longest_line(self, tl, tr, bl, br): if Validator.is_type([tl, tr, bl, br], tuple) and \ Validator.is_number([tl[0], tl[1], tr[0], tr[1], bl[0], bl[1], br[0], br[1]]): tltr = self._get_distance_between_points(tl, tr) tlbl = self._get_distance_between_points(tl, bl) trbr = self._get_distance_between_points(tr, br) blbr = self._get_distance_between_points(bl, br) max_value = max([tltr, tlbl, trbr, blbr]) # returns the max value of the list min_value = min([tltr, tlbl, trbr, blbr]) # returns the min value of the list if max_value - min_value > max_value * 0.1: # if the difference is grater than 10 % of max value raise SudokuFieldSizeError( ) # one corner wasn't detected so we should try it again return max_value raise InappropriateArgsError("calculating the longest line")
def test_incorrect_is_number(self): self.assertEqual(Validator.is_number("1"), False) self.assertEqual(Validator.is_number([1, 2.0, "3", 4.4]), False)
def test_correct_is_number(self): self.assertEqual(Validator.is_number(1), True) self.assertEqual(Validator.is_number(1.0), True) self.assertEqual(Validator.is_number([1, 2.0, 3, 4.4]), True)
def draw_point(self, img, pt): if Validator.is_type(img, np.ndarray) and Validator.is_type( pt, tuple) and Validator.is_number([pt[0], pt[1]]): cv2.circle(img, pt, 5, (0, 0, 255, 0), 2) raise InappropriateArgsError("drawing a point: " + str(pt) + " to image: " + str(img))
def test_correct_get_coords_of_angle(self): lines = self.field._get_hough_lines(self.field.changing_img) result = self.field._get_coords_of_angle(lines) self.assertEqual(type(result), np.ndarray) self.assertEqual(Validator.is_number(result[0].tolist()), True)
def test_correct_get_points_on_line(self): result = self.field._get_points_on_line(3, 3) self.assertEqual(type(result), tuple) self.assertEqual(Validator.is_number(list(result)), True) self.assertEqual(result, (-144, -989, 138, 990))