public QuadTreeItem <T>?QueryNearest(RectangleF range, PointF start)
        {
            var items = Query(range);

            if (items == null)
            {
                return(null);
            }

            bool             hasValue    = false;
            float            lastItemDst = default;
            QuadTreeItem <T> lastItem    = default;

            foreach (var item in items)
            {
                if (hasValue &&
                    item.Bounds.SquaredDistanceTo(start).CompareTo(lastItemDst) >= 0)
                {
                    continue;
                }

                hasValue    = true;
                lastItem    = item;
                lastItemDst = item.Bounds.SquaredDistanceTo(start);
            }

            QuadTreePool <T> .Return(items);

            if (!hasValue)
            {
                return(null);
            }
            return(lastItem);
        }
Exemple #2
0
        public bool Insert(QuadTreeItem <T> item)
        {
            if (UseFuzzyBoundaries)
            {
                if (!Bounds.Intersects(item.Bounds))
                {
                    return(false);
                }
            }
            else
            {
                if (!Bounds.Contains(item.Bounds))
                {
                    return(false);
                }
            }

            if (Items.Count >= Threshold)
            {
                if (!IsDivided)
                {
                    Subdivide();
                }

                if (TopLeft.Insert(item) ||
                    TopRight.Insert(item) ||
                    BottomLeft.Insert(item) ||
                    BottomRight.Insert(item))
                {
                    return(true);
                }
            }
            else
            {
                Items.Add(item);
                return(true);
            }

            if (AllowOverflow)
            {
                Items.Add(item);
                return(true);
            }

            return(false);
        }