Beispiel #1
0
        /// <summary>
        /// Look for the given item. Compare with .Equals(obj)
        /// </summary>
        /// <param name="searchedItem">What to search</param>
        /// <param name="index">Where it lies</param>
        /// <returns>True if any matching item was found</returns>
        public bool Contains(T searchedItem, out QuadTreeKey index)
        {
            unchecked {
                index = null;
                bool b = false;

                if (this._isLeaf)
                {
                    var iter = _items.GetEnumerator();
                    KeyValuePair <QuadTreeKey, T> kv = default(KeyValuePair <QuadTreeKey, T>);
                    while (!b && iter.MoveNext())
                    {
                        kv = iter.Current;
                        b |= kv.Value.Equals(searchedItem);
                    }

                    // Assign output key
                    index = kv.Key;
                }
                else
                {
                    // Will search in every children
                    var childrenIter      = _children.GetEnumerator();
                    QuadTreeNode <T> node = default(QuadTreeNode <T>);
                    while (!b && childrenIter.MoveNext())
                    {
                        node = childrenIter.Current as QuadTreeNode <T>;
                        b   |= node.Contains(searchedItem, out index);
                    }
                }

                return(b);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Try to insert an item
        /// </summary>
        /// <param name="key">Key of the item</param>
        /// <param name="item">Item to insert</param>
        /// <returns>True if item has been inserted</returns>
        private bool Add(T item)
        {
            // Generate a key for the new location
            var key = new QuadTreeKey(item.X, item.Y);

            // Try to insert item
            if (_items.ContainsKey(key))
            {
                return(false);
            }
            else
            {
                _items.Add(key, item);
                return(true);
            }
        }