def test_line_intersect(self): horizontal = HorizontalLine(Point(0, 5), Point(13, 5)) vertical = VerticalLine(Point(7, 0), Point(7, 25)) p = get_intersection(vertical, horizontal) self.assertEqual(p, Point(7, 5)) # reverse order horizontal = HorizontalLine(Point(13, 5), Point(0, 5)) vertical = VerticalLine(Point(7, 25), Point(7, 0)) p = get_intersection(vertical, horizontal) self.assertEqual(p, Point(7, 5))
def test_does_intersect(self): horizontal = HorizontalLine(Point(0, 5), Point(13, 5)) vertical = VerticalLine(Point(7, 0), Point(7, 25)) self.assertTrue(does_intersect(vertical, horizontal)) horizontal = HorizontalLine(Point(0, 5), Point(13, 5)) vertical = VerticalLine(Point(42, 0), Point(42, 25)) self.assertFalse(does_intersect(vertical, horizontal))
def generate_random_inner_rects(num_rects, frame): x_iter = frame.bottom_left.x x_end = frame.top_right.x y_iter = frame.bottom_left.y y_end = frame.top_right.y rects = [] while x_iter < x_end and y_iter < y_end and num_rects > 0: arr = np.random.random_sample(2) bottom_left = Point(x_iter, y_iter) x = arr[0] * (x_end - x_iter) + x_iter y = arr[1] * (y_end - y_iter) + y_iter top_right = Point(x, y) r = Rect(bottom_left, top_right) rects.append(r) num_rects -= 1 x_iter += x y_iter += y return rects
def test_single_rect(self): frame = Rect(Point(0, 0), Point(42, 42)) rects = [Rect(Point(0, 0), Point(21, 42))] c_rects = get_complement(frame, rects) self.assertEqual(len(c_rects), 1) self.assertEqual(c_rects[0], Rect(Point(21, 0), Point(42, 42)))
def test_already_full_multiple_rect_horizontal(self): frame = Rect(Point(0, 0), Point(42, 42)) rects = [ Rect(Point(0, 0), Point(42, 21)), Rect(Point(0, 21), Point(42, 42)) ] c_rects = get_complement(frame, rects) self.assertEqual(len(c_rects), 0)
def test_overlap(self): r1 = Rect(Point(0, 0), Point(13, 13)) r2 = Rect(Point(13, 13), Point(20, 25)) self.assertFalse(r1.does_overlap(r2)) r3 = Rect(Point(13.00, 17.00), Point(20.00, 25.00)) self.assertTrue(r2.does_overlap(r3)) self.assertTrue(r3.does_overlap(r2))
def test_equality(self): p = Point(0, 0) self.assertEqual(p, p) # test with round-off errors p = Point(1. / 3, 1. / 3) self.assertEqual(p, p) p1 = Point(1, 2) p2 = Point(52, 2) self.assertNotEqual(p1, p2) p1 = Point(1, 1. / 9) p2 = Point(math.pi, 1. / 9) self.assertNotEqual(p1, p2)
#!/usr/bin/env python3 from rect_comp import Point, Rect, get_intersection, get_complement, get_intersect_points # a demonstation of the API frame = Rect(Point(0, 0), Point(100, 100)) r1 = Rect(Point(13, 13), Point(20, 25)) r2 = Rect(Point(27, 17), Point(45, 42)) r3 = Rect(Point(32, 50), Point(63, 70)) crects = get_complement(frame, [r1, r2, r3]) print( 'The complementary rectangle set (each represented by its botton left and top right corners:' ) for r in crects: print(r)
def gen_random_frame(): max_value = 2**16 arr = np.random.random_sample(4) * max_value arr = sorted(arr) p = Point(arr[0], arr[1]) return Rect(p, Point(p.x + arr[2], p.y + arr[3]))
def test_empty_case(self): frame = Rect(Point(0, 0), Point(42, 42)) rects = [] c_rects = get_complement(frame, rects) self.assertEqual(len(c_rects), 1) self.assertEqual(c_rects[0], frame)
def test_already_full_single_rect(self): frame = Rect(Point(0, 0), Point(42, 42)) rects = [Rect(Point(0, 0), Point(42, 42))] c_rects = get_complement(frame, rects) self.assertEqual(len(c_rects), 0)
def create_horizontal_line(): HorizontalLine(Point(0, 1), Point(42, 42))
def create_vertical_line(): VerticalLine(Point(0, 1), Point(42, 42))