def test_write_read_rectangles(self): rectangles = [ Rectangle(Point(0, 0), 10, 10), Rectangle(Point(2, 2), 20, 20) ] utils.write_rectangles(rectangles) csv_rectangles = utils.read_rectangles() self.assertEqual(len(rectangles), len(csv_rectangles))
def test_is_inside(self): rectangle = Rectangle(Point(0, 0), 10, 10) point_in = Point(1, 1) point_out = Point(20, 20) point_in_border = Point(10, 10) self.assertTrue(rectangle.is_inside(point_in)) self.assertFalse(rectangle.is_inside(point_out)) self.assertFalse(rectangle.is_inside(point_in_border))
def closest_border_rectangle_point(point, rectangle): x_points = range(rectangle.leftPoint.x, rectangle.rightPoint.x + 1, 1) y_points = range(rectangle.leftPoint.y, rectangle.rightPoint.y + 1, 1) closest_x = min(x_points, key=lambda x: abs(x - point.x)) closest_y = min(y_points, key=lambda y: abs(y - point.y)) if rectangle.is_inside(point): A = Point(rectangle.leftPoint.x, closest_y) B = Point(closest_x, rectangle.rightPoint.y) C = Point(rectangle.rightPoint.x, closest_y) D = Point(closest_x, rectangle.leftPoint.y) return find_nearest_point([A, B, C, D], target_point=point) else: return Point(closest_x, closest_y)
def read_rectangles(path='rectangles_file.csv'): rectangles = [] with open(path) as csv_file: csv_reader = csv.reader(csv_file, delimiter=',') line_count = 0 for row in csv_reader: if line_count == 0: print(f'Column names are {", ".join(row)}') line_count += 1 else: rectangles.append( Rectangle(Point(int(row[0]), int(row[1])), int(row[2]), int(row[3]))) line_count += 1 print(f'Processed {line_count} lines.') return rectangles
def test_closest_border_rectangle_point(self): rectangle = Rectangle(Point(0, 0), 10, 10) point_in = Point(1, 1) point_out = Point(20, 20) point_in_border = Point(10, 10) self.assertEqual( Point(10, 10), utils.closest_border_rectangle_point(point_out, rectangle)) self.assertEqual( Point(10, 10), utils.closest_border_rectangle_point(point_in_border, rectangle)) self.assertEqual( Point(0, 1), utils.closest_border_rectangle_point(point_in, rectangle))
def insert_figure_random(self, width, height): for x in range(self.max_iterations): start_point = Point( np.random.randint(0, self.space_max_x - width), np.random.randint(0, self.space_max_y - height)) target_rectangle = Rectangle(start_point, width, height) # target_points = get_points(target_rectangle) can = True for rectangle in self.figures: # source_points = get_points(rectangle) if doOverlap(rectangle.leftPoint, rectangle.rightPoint, target_rectangle.leftPoint, target_rectangle.rightPoint): can = False break if can: self.figures.append(target_rectangle) return
def test_equal(self): self.assertEqual(Point(10, 10), Point(10, 10)) self.assertNotEqual(Point(10, 10), Point(5, 10))
def test_doOverlap(self): source = Point(0, 0), Point(5, 5) same = Point(0, 0), Point(5, 5) border_intersect = Point(0, 5), Point(1, 6) no_intersect = Point(6, 6), Point(10, 10) full_intersect = Point(0, 0), Point(3, 3) intersect = Point(4, 4), Point(6, 6) problem_source_point = Point(0, 2), Point(6, 8) problem_point = Point(0, 2), Point(7, 4) self.assertTrue( initializer.doOverlap(problem_source_point[0], problem_source_point[1], problem_point[0], problem_point[1])) self.assertTrue( initializer.doOverlap(source[0], source[1], intersect[0], intersect[1])) self.assertFalse( initializer.doOverlap(source[0], source[1], no_intersect[0], no_intersect[1])) self.assertTrue( initializer.doOverlap(source[0], source[1], same[0], same[1])) self.assertFalse( initializer.doOverlap(source[0], source[1], border_intersect[0], border_intersect[1])) self.assertFalse( initializer.doOverlap(source[0], source[1], full_intersect[0], full_intersect[1]))