// ------------------------------------------------ // This algo remove old objects in O(n) // ------------------------------------------------ void ClearObjects(List <QTObject <T> > objects, bool isEnable) { int removeIndex = -1; int removeCount = 0; for (int i = 0; i < _objects.Count; i++) { QTObject <T> t = _objects[i]; if (!t.obj.isEnable && isEnable) { removeCount++; if (removeIndex == -1) { removeIndex = i; } } else { if (removeIndex != -1) { // swap with the first remove item _objects[removeIndex] = t; removeIndex++; } } } if (removeIndex > -1) { _objects.RemoveRange(removeIndex, removeCount); } }
/* * \brief Insert the object into the quadtree. If the node * exceeds the capacity, it will split and add all * objects to their corresponding nodes. * \param a_Object The object to insert */ private void Insert(QTObject <T> obj) { if (_nodes[0] != null) { int index = GetIndex(obj.rect); if (index != -1) { _nodes[index].Insert(obj); return; } } _objects.Add(obj); if (_objects.Count > MAX_OBJECTS && _level < MAX_LEVELS) { if (_nodes[0] == null) { Split(); } int i = 0; while (i < _objects.Count) { int index = GetIndex(_objects[i].rect); if (index != -1) { QTObject <T> qtObject = _objects[i]; _objects.RemoveAt(i); _nodes[index].Insert(qtObject); } else { i++; } } } }