public BUCoverNode <T> AddRectangle(SpatialObj <T> rect, bool reorganize = true) { BUCoverNode <T> deepest_field = null; if (rootNode == null) { Size nodeSize = DetermineBoundingSize(rect); rootNode = new BUCoverNode <T>(new Rectangle(rect.boundingBox.Center, nodeSize), Capacity, 0, pVal, null); deepest_field = rootNode; } else { while (deepest_field == null) { deepest_field = FindContainingField(rect, rootNode); if (deepest_field == null) { if (!rootNode.GetActualBounds().ContainsPoint(rect.boundingBox.Center)) { rootNode = rootNode.CreateParent(rect.boundingBox.Center); } else { foreach (var corner in rect.boundingBox.GetCorners()) { if (!rootNode.GetActualBounds().ContainsPoint(corner)) { rootNode = rootNode.CreateParent(corner); break; } } } } } } if (deepest_field.IsFull() && !deepest_field.HasChildren()) { PartitionField(deepest_field); BUCoverNode <T> node = AddRectangle(rect); if (reorganize) { ReorganizeOverflownNodes(); } if (node != null) { return(node); } } else { bool overflown = deepest_field.StoreRectangle(rect); } if (deepest_field != null) { Count += 1; } return(deepest_field); }