private static ItemWithBounds[] FillWithItems(Quadtree <object> tree, int amount, int seed = 0) { var random = new Random(seed); var items = new ItemWithBounds[amount]; for (var i = 0; i < items.Length; i++) { var item = new ItemWithBounds(GetRectAtPoint(tree, (float)random.NextDouble(), (float)random.NextDouble())); items[i] = item; tree.Insert(item, item.Bounds); } return(items); }
public void Retrieve_CorrectResults() { var treeBounds = Rect.MinMaxRect(-500, -400, 300, 200); var tree = Quadtree <object> .Create(treeBounds, 1, 5); // Looks somewhat like this: // // +----------------+----------------+ // | | | // | +------------------+ | // | | | | | // | | | | | // | | | | | // | | | +----------+ | // | +------------------+ | | // | | | | | // +---------------------------------+ // | | | | | // | | | | | // | | | | | // | | | | | // | | | | | // | | | | | // | | +----------+ | // | | | // +----------------+----------------+ var topItem = new ItemWithBounds(GetMinMaxRect(tree, 0.1f, 0.6f, 0.7f, 0.9f)); var bottomItem = new ItemWithBounds(GetMinMaxRect(tree, 0.6f, 0.1f, 0.9f, 0.7f)); tree.Insert(topItem, topItem.Bounds); tree.Insert(bottomItem, bottomItem.Bounds); // sorry for anybody who is reading this shitty code.. var topLeft = tree.Retrieve(GetMinMaxRect(tree, 0, 0.51f, 0.49f, 1f)); var topRight = tree.Retrieve(GetMinMaxRect(tree, 0.51f, 0.51f, 1, 1)); var bottomLeft = tree.Retrieve(GetMinMaxRect(tree, 0, 0, 0.49f, 0.49f)); var bottomRight = tree.Retrieve(GetMinMaxRect(tree, 0.51f, 0, 1, 0.49f)); // better names anybody? please? // ReSharper disable InconsistentNaming var topRight_topLeft = tree.Retrieve(GetMinMaxRect(tree, 0.51f, 0.76f, 0.74f, 1)); var topRight_topRight = tree.Retrieve(GetMinMaxRect(tree, 0.76f, 0.76f, 1, 1)); var topRight_bottomLeft = tree.Retrieve(GetMinMaxRect(tree, 0.51f, 0.51f, 0.74f, 0.74f)); var topRight_bottomRight = tree.Retrieve(GetMinMaxRect(tree, 0.76f, 0.51f, 1, 0.74f)); // ReSharper restore InconsistentNaming Assert.IsTrue(tree.IsSplit); Assert.AreEqual(1, topLeft.Count); Assert.AreEqual(2, topRight.Count); Assert.AreEqual(0, bottomLeft.Count); Assert.AreEqual(1, bottomRight.Count); Assert.AreEqual(1, topRight_topLeft.Count); Assert.AreEqual(0, topRight_topRight.Count); Assert.AreEqual(2, topRight_bottomLeft.Count); Assert.AreEqual(1, topRight_bottomRight.Count); Assert.IsTrue(topLeft.Contains(topItem)); Assert.IsTrue(bottomRight.Contains(bottomItem)); }