Exemple #1
0
        public bool Insert(QuadTreeElement <T> item, float minSize)
        {
            if (!Methods.IsRectEnveloped(topRight, bottomLeft, item.TopRight, item.BottomLeft))
            {
                return(false);
            }

            if (nodes.Count == 0)
            {
                CreateSubNodes(minSize);
            }

            foreach (QuadTreeNode <T> node in nodes)
            {
                if (Methods.IsRectEnveloped(node.topRight, node.bottomLeft, item.TopRight, item.BottomLeft))
                {
                    if (node.Insert(item, minSize))
                    {
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
            }

            this.contents.Add(item);
            return(true);
        }
Exemple #2
0
        public void Add(QuadTreeElement <T> item)
        {
            boundedElements.Add(item.Data, item);

            if (!root.Insert(item, this.minNodeSize))
            {
                var halfSize = (topRight - bottomLeft) / 2;

                while (!Methods.IsRectEnveloped(this.topRight, this.bottomLeft, item.TopRight, item.BottomLeft))
                {
                    this.topRight   += halfSize;
                    this.bottomLeft -= halfSize;
                    halfSize        *= 2;
                }

                var oldRoot = this.root;
                this.root = new QuadTreeNode <T>(topRight, bottomLeft);
                this.root.Insert(item, this.minNodeSize);

                foreach (var oldItem in oldRoot.SubTreeContents)
                {
                    root.Insert(oldItem, this.minNodeSize);
                }
            }
        }
Exemple #3
0
        public bool Remove(QuadTreeElement <T> item)
        {
            if (!Methods.IsRectEnveloped(this.topRight, this.bottomLeft, item.TopRight, item.BottomLeft))
            {
                return(false);
            }

            if (this.contents.Remove(item))
            {
                return(true);
            }

            foreach (QuadTreeNode <T> node in nodes)
            {
                if (Methods.IsRectEnveloped(node.topRight, node.bottomLeft, item.TopRight, item.BottomLeft))
                {
                    if (node.Remove(item))
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
Exemple #4
0
        public void Add(T newItem, Vector2D center, Vector2D size)
        {
            var boundedItem = new QuadTreeElement <T>(newItem, center + size / 2, center - size / 2);

            boundedElements.Add(newItem, boundedItem);

            if (!root.Insert(boundedItem, this.minNodeSize))
            {
                var halfSize = (topRight - bottomLeft) / 2;

                while (!Methods.IsRectEnveloped(this.topRight, this.bottomLeft, boundedItem.TopRight, boundedItem.BottomLeft))
                {
                    this.topRight   += halfSize;
                    this.bottomLeft -= halfSize;
                    halfSize        *= 2;
                }

                var oldRoot = this.root;
                this.root = new QuadTreeNode <T>(topRight, bottomLeft);
                this.root.Insert(boundedItem, this.minNodeSize);

                foreach (var oldItem in oldRoot.SubTreeContents)
                {
                    root.Insert(oldItem, this.minNodeSize);
                }
            }
        }
Exemple #5
0
        public bool Remove(QuadTreeElement <T> item)
        {
            if (!boundedElements.ContainsKey(item.Data))
            {
                return(false);
            }

            boundedElements.Remove(item.Data);
            return(root.Remove(item));
        }
Exemple #6
0
        public bool Contains(QuadTreeElement <T> item)
        {
            if (item == null)
            {
                throw new ArgumentNullException(nameof(item));
            }

            return(this.boundedElements.ContainsKey(item.Data) &&
                   item.Equals(this.boundedElements[item.Data]));
        }
Exemple #7
0
        public bool Remove(QuadTreeElement <T> item)
        {
            if (item == null)
            {
                throw new ArgumentNullException(nameof(item));
            }

            if (!boundedElements.ContainsKey(item.Data))
            {
                return(false);
            }

            boundedElements.Remove(item.Data);
            return(root.Remove(item));
        }
Exemple #8
0
 public bool Contains(QuadTreeElement <T> item)
 {
     return(this.boundedElements.ContainsKey(item.Data) &&
            item.Equals(this.boundedElements[item.Data]));
 }