Example #1
0
        /// <summary>
        ///     Adds the neighboring quad nodes.
        /// </summary>
        private void AddNeighbors()
        {
            switch (Type)
            {
            case NodeType.TopLeft:

                #region The direct neighbors that DO NOT belong within the current node's parent:

                // In case this is not the topmost quad node of the whole tree
                if (Parent.NeighborTop != null)
                {
                    // The parent's top neighbor's next-level bottom-left quad node.
                    // This is effectively the adjacent quad node of the same size as this one.
                    NeighborTop = Parent.NeighborTop.ChildBottomLeft;
                }

                // In case this is not the leftmost quad node of the whole tree
                if (Parent.NeighborLeft != null)
                {
                    // The parent's left neighbor's next-level top-right quad node.
                    // This is effectively the adjacent quad node of the same size as this one.
                    NeighborLeft = Parent.NeighborLeft.ChildTopRight;
                }

                #endregion

                #region The direct neighbors within the same parent:

                NeighborRight  = Parent.ChildTopRight;
                NeighborBottom = Parent.ChildBottomLeft;

                #endregion

                break;

            case NodeType.TopRight:

                #region The direct neighbors that DO NOT belong within the current node's parent:

                if (Parent.NeighborTop != null)
                {
                    NeighborTop = Parent.NeighborTop.ChildBottomRight;
                }

                if (Parent.NeighborRight != null)
                {
                    NeighborRight = Parent.NeighborRight.ChildTopLeft;
                }

                #endregion

                #region The direct neighbors within the same parent:

                NeighborLeft   = Parent.ChildTopLeft;
                NeighborBottom = Parent.ChildBottomRight;

                #endregion

                break;

            case NodeType.BottomLeft:

                #region The direct neighbors that DO NOT belong within the current node's parent:

                if (Parent.NeighborBottom != null)
                {
                    NeighborBottom = Parent.NeighborBottom.ChildTopLeft;
                }

                if (Parent.NeighborLeft != null)
                {
                    NeighborLeft = Parent.NeighborLeft.ChildBottomRight;
                }

                #endregion

                #region The direct neighbors within the same parent:

                NeighborRight = Parent.ChildBottomRight;
                NeighborTop   = Parent.ChildTopLeft;

                #endregion

                break;

            case NodeType.BottomRight:

                #region The direct neighbors that DO NOT belong within the current node's parent:

                if (Parent.NeighborBottom != null)
                {
                    NeighborBottom = Parent.NeighborBottom.ChildTopRight;
                }

                if (Parent.NeighborRight != null)
                {
                    NeighborRight = Parent.NeighborRight.ChildBottomLeft;
                }

                #endregion

                #region The direct neighbors within the same parent:

                NeighborLeft = Parent.ChildBottomLeft;
                NeighborTop  = Parent.ChildTopRight;

                #endregion

                break;
            }

            if (hasChildren)
            {
                // recursively add neighbors for all children.
                ChildTopLeft.AddNeighbors();
                ChildTopRight.AddNeighbors();
                ChildBottomLeft.AddNeighbors();
                ChildBottomRight.AddNeighbors();
            }
        }