def test_circle_in_bbox_border(self): # contains have no need for `border` attributes. It always includes # border points. c = Circle(Vec2(0, 0), 4) r = BoundingBox.from_center(Vec2(0, 0), 8, 8) self.assertTrue(intersects(c, r)) self.assertTrue(contains(r, c)) self.assertFalse(contains(c, r))
def test_query(self): x = Circle(Vec2(30, 30), 10) y = Circle(Vec2(-10, -10), 10) z = Circle(Vec2(0, 0), 10) self.tree.insert(x, x) self.tree.insert(y, y) self.tree.insert(z, z) # Helper to ignore results order o = lambda x: list(sorted(x, key=lambda x: x.center.x)) # Contains circle results = self.tree.query( BoundingBox.from_center(Vec2(30, 30), 22, 22)) self.assertEqual(results, [x]) # Intersect circle results = self.tree.query( BoundingBox.from_center(Vec2(20, 40), 10, 10)) self.assertEqual(results, [x]) # Is contained in circle results = self.tree.query( BoundingBox.from_center(Vec2(30, 30), 10, 10)) self.assertEqual(results, [x]) # Contains 2 circles results = self.tree.query( BoundingBox.from_center(Vec2(-5, -5), 32, 32)) self.assertEqual(o(results), o([y, z])) # Intersect 2 circles results = self.tree.query( BoundingBox.from_center(Vec2(-10, 0), 2, 2)) self.assertEqual(o(results), o([y, z])) # Contained in 2 circles results = self.tree.query( BoundingBox.from_center(Vec2(-5, -5), 2, 2)) self.assertEqual(o(results), o([y, z])) # Contained in 1 of circles results = self.tree.query( BoundingBox.from_center(Vec2(-10, -10), 2, 2)) self.assertEqual(results, [y]) # Contain all circles results = self.tree.query( BoundingBox.from_center(Vec2(0, 0), 80, 80)) self.assertEqual(o(results), o([x, y, z]))
def test_no_intersection(self): c = Circle(Vec2(0, 0), 2) r = BoundingBox.from_center(Vec2(10, 10), 2, 2) self.assertFalse(intersects(c, r)) self.assertFalse(contains(c, r)) self.assertFalse(contains(r, c))
def test_contains(self): b1 = BoundingBox.from_center(Vec2(0, 0), 4, 4) b2 = BoundingBox.from_center(Vec2(0, 0), 2, 2) self.assertTrue(intersects(b1, b2)) self.assertTrue(contains(b1, b2)) self.assertFalse(contains(b2, b1))
def test_border_intersection(self): c = Circle(Vec2(0, 0), 2) r = BoundingBox.from_center(Vec2(0, 4), 4, 4) self.assertFalse(intersects(c, r)) self.assertTrue(intersects(c, r, border=True))
def test_intersection(self): c = Circle(Vec2(0, 0), 5) r = BoundingBox.from_center(Vec2(0, 6), 4, 4) self.assertTrue(intersects(c, r)) self.assertFalse(contains(c, r)) self.assertFalse(contains(r, c))
def test_bbox_in_circle(self): c = Circle(Vec2(0, 0), 10) r = BoundingBox.from_center(Vec2(0, 0), 2, 2) self.assertTrue(intersects(c, r)) self.assertTrue(contains(c, r)) self.assertFalse(contains(r, c))