Beispiel #1
0
 /// <summary>
 /// Walk the given linked list and test each node against the given bounds/
 /// </summary>
 /// <param name="last">The last node in the circularly linked list.</param>
 /// <param name="bounds">Bounds to test</param>
 /// <returns>Return true if a node in the list intersects the bounds</returns>
 static Boolean HasIntersectingNodes(QuadNode <T> last, Rect bounds)
 {
     if (last != null)
     {
         QuadNode <T> n = last;
         do
         {
             n = n.Next; // first node.
             if (n.Bounds.IntersectsWith(bounds))
             {
                 return(true);
             }
         } while (n != last);
     }
     return(false);
 }
Beispiel #2
0
        /// <summary>
        /// Insert the given node
        /// </summary>
        /// <param name="node">The node </param>
        /// <param name="bounds">The bounds of that node</param>
        /// <returns></returns>
        internal Quadrant <T> Insert(T node, Rect bounds)
        {
            Debug.Assert(bounds.Width != 0 && bounds.Height != 0);
            if (bounds.Width == 0 || bounds.Height == 0)
            {
                // todo: localize
                throw new ArgumentException("Bounds of quadrant cannot be zero width or height");
            }

            Double w = Bounds.Width / 2;

            if (w == 0)
            {
                w = 1;
            }
            Double h = Bounds.Height / 2;

            if (h == 0)
            {
                h = 1;
            }

            // assumption that the Rect struct is almost as fast as doing the operations
            // manually since Rect is a value type.

            Rect topLeft     = new Rect(Bounds.Left, Bounds.Top, w, h);
            Rect topRight    = new Rect(Bounds.Left + w, Bounds.Top, w, h);
            Rect bottomLeft  = new Rect(Bounds.Left, Bounds.Top + h, w, h);
            Rect bottomRight = new Rect(Bounds.Left + w, Bounds.Top + h, w, h);

            Quadrant <T> child = null;

            // See if any child quadrants completely contain this node.
            if (topLeft.Contains(bounds))
            {
                if (TopLeft == null)
                {
                    TopLeft = new Quadrant <T>(this, topLeft);
                }
                child = TopLeft;
            }
            else if (topRight.Contains(bounds))
            {
                if (TopRight == null)
                {
                    TopRight = new Quadrant <T>(this, topRight);
                }
                child = TopRight;
            }
            else if (bottomLeft.Contains(bounds))
            {
                if (BottomLeft == null)
                {
                    BottomLeft = new Quadrant <T>(this, bottomLeft);
                }
                child = BottomLeft;
            }
            else if (bottomRight.Contains(bounds))
            {
                if (BottomRight == null)
                {
                    BottomRight = new Quadrant <T>(this, bottomRight);
                }
                child = BottomRight;
            }

            if (child != null)
            {
                return(child.Insert(node, bounds));
            }
            else
            {
                QuadNode <T> n = new QuadNode <T>(node, bounds);
                if (Nodes == null)
                {
                    n.Next = n;
                }
                else
                {
                    // link up in circular link list.
                    QuadNode <T> x = Nodes;
                    n.Next = x.Next;
                    x.Next = n;
                }
                Nodes = n;
                return(this);
            }
        }