Ejemplo n.º 1
0
        /// <summary>
        /// Returns the specified child octant tree instance, or instantiates a new octant tree if the specified child
        /// instance is <c>null</c>. The newly constructed instance will then be returned.
        /// </summary>
        /// <param name="trees">
        /// A reference to the <see cref="OctantTree"/> array from which to get the specified instance.
        /// </param>
        /// <param name="index">The index in the array whose held instance to get or set.</param>
        /// <param name="newOctant">
        /// The octant instance representing the space managed by the new child tree instance, if necessary.
        /// </param>
        /// <returns>The instance held at the specified index, or the newly created instance.</returns>
        public static OctantTree GetOrSetSubTree(ref OctantTree[] trees, int index, Octant newOctant)
        {
            if (trees != null && trees?[index] == null)
            {
                trees[index] = new OctantTree(newOctant);
            }

            return(trees?[index]);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Adds the given body to one of the child trees of this instance.
        /// </summary>
        /// <param name="newBody">The body to add.</param>
        private void AddToChildTree(Body newBody)
        {
            // don't create subtrees if it violates the minimum width limit, to prevent infinitely deep trees and thus
            // preventing excessive resource allocation
            if (octant.Length / 2 < Constants.MinimumTreeWidth)
            {
                return;
            }

            for (int childTreeIndex = 0; childTreeIndex < 8; childTreeIndex++)
            {
                OctantTree childTree = SubTree(childTreeIndex);

                // if the child tree under consideration contains the given body, then add it to that tree
                if (childTree.octant.ContainsPoint(newBody.Position))
                {
                    childTree.AddBody(newBody);
                    return;
                }
            }
        }