public void RTreeQuery_IncrementalRectangles() { const int RectsCount = 1000; const int RegionSize = 1000; const int RectSize = 10; for (int seed = 0; seed < 1; ++seed) { var rects = new Rectangle[RectsCount]; var rand = new Random(seed); for (int i = 0; i < RectsCount; ++i) { rects[i] = new Rectangle(new Point(rand.Next(RegionSize), rand.Next(RegionSize))); } // create rTree with just the first rectangle var l = new List<KeyValuePair<Rectangle, Rectangle>> { new KeyValuePair<Rectangle, Rectangle>(rects[0], rects[0]) }; var queryTree = new RTree<Rectangle>(l); // add remaining rectangles 10 at a time for (int a = 1, b = 10; b < RectsCount; a = b, b += 10) { for (int i = a; i < b; ++i) { queryTree.Add(rects[i], rects[i]); } Assert.AreEqual(queryTree.GetAllLeaves().Count(), b, "did we lose leaves?"); Assert.AreEqual(queryTree.GetAllIntersecting( new Rectangle(0, 0, RegionSize + RectSize, RegionSize + RectSize)).Count(), b, "are all leaves inside the max range?"); Assert.AreEqual(queryTree.GetAllIntersecting(new Rectangle(-2, -2, -1, -1)).Count(), 0, "should be no leaves inside this rectangle!"); var query = new Rectangle(rand.Next(RegionSize), rand.Next(RegionSize), rand.Next(RegionSize), rand.Next(RegionSize)); var checkList = (from r in rects.Take(b) where query.Intersects(r) select r).ToList(); var checkSet = new HashSet<Rectangle>(checkList); var result = queryTree.GetAllIntersecting(query).ToList(); Assert.AreEqual(result.Count, checkList.Count, "result and check are different sizes: seed={0}", seed); foreach (var r in result) { Assert.IsTrue(query.Intersects(r), "rect doesn't intersect query: seed={0}, rect={1}, query={2}", seed, r, query); Assert.IsTrue(checkSet.Contains(r), "check set does not contain rect: seed={0}", seed); } } } }