Exemple #1
0
        public Game1()
        {
            this.graphics = new GraphicsDeviceManager(this);
            this.graphics.IsFullScreen = false;
            this.graphics.PreferredBackBufferHeight = windowHeight;
            this.graphics.PreferredBackBufferWidth = windowWidth;
            this.Content.RootDirectory = "Content";

            this.quadTree = new QuadTree(new Vector2(0, 0), new Vector2(windowWidth, windowHeight), 10);
        }
Exemple #2
0
        /// <summary>
        /// Insert element into quad tree
        /// </summary>
        /// <param name="element">Element to insert into quad tree</param>
        public void InsertElement(IQuadTreeElement element)
        {
            // Max depth reached?
            if (this.currentDepth >= this.maxDepth)
            {
                elements.Add(element);
                return;
            }

            // Which quads is element in?
            Vector2 center = (dimensionsFrom + dimensionsTo) / 2.0f;

            int quadCount = 0;
            bool inQuadOne = false;
            bool inQuadTwo = false;
            bool inQuadThree = false;
            bool inQuadFour = false;

            if (element.boundingBox.X < center.X && element.boundingBox.Y < center.Y &&
                element.boundingBox.X + element.boundingBox.Width >= this.dimensionsFrom.X && element.boundingBox.Y + element.boundingBox.Height >= this.dimensionsFrom.Y)
            {
                inQuadOne = true;
                quadCount++;
            }

            if (element.boundingBox.X + element.boundingBox.Width >= center.X && element.boundingBox.Y < center.Y &&
                element.boundingBox.X < this.dimensionsTo.X && element.boundingBox.Y + element.boundingBox.Height >= this.dimensionsFrom.Y)
            {
                inQuadTwo = true;
                quadCount++;
            }

            if (element.boundingBox.X < center.X && element.boundingBox.Y + element.boundingBox.Height >= center.Y &&
                element.boundingBox.X + element.boundingBox.Width >= this.dimensionsFrom.X && element.boundingBox.Y < this.dimensionsTo.Y)
            {
                inQuadThree = true;
                quadCount++;
            }

            if (element.boundingBox.X + element.boundingBox.Width >= center.X && element.boundingBox.Y + element.boundingBox.Height >= center.Y &&
                element.boundingBox.X < this.dimensionsTo.X && element.boundingBox.Y < this.dimensionsTo.Y)
            {
                inQuadFour = true;
                quadCount++;
            }

            // Element is in no quad
            if (quadCount == 0)
                throw new ArgumentException("The element lies outside the tree", "element");

            // Element is in multiple quads
            if (quadCount > 1)
            {
                elements.Add(element);
                return;
            }

            // Element is in a specific quad
            if (inQuadOne)
            {
                if (this.quadOne == null)
                    this.quadOne = new QuadTree(this.dimensionsFrom, center, this.currentDepth + 1, this.maxDepth);

                this.quadOne.InsertElement(element);
            }

            if (inQuadTwo)
            {
                if (this.quadTwo == null)
                    this.quadTwo = new QuadTree(center, new Vector2(this.dimensionsTo.X, this.dimensionsFrom.Y), this.currentDepth + 1, this.maxDepth);

                this.quadTwo.InsertElement(element);
            }

            if (inQuadThree)
            {
                if (this.quadThree == null)
                    this.quadThree = new QuadTree(new Vector2(this.dimensionsFrom.X, this.dimensionsTo.Y), center, this.currentDepth + 1, this.maxDepth);

                this.quadThree.InsertElement(element);
            }

            if (inQuadFour)
            {
                if (this.quadFour == null)
                    this.quadFour = new QuadTree(center, new Vector2(this.dimensionsTo.X, this.dimensionsTo.Y), this.currentDepth + 1, this.maxDepth);

                this.quadFour.InsertElement(element);
            }
        }
Exemple #3
0
        /// <summary>
        /// Expand quad tree by inserting it into a new quad tree four times in size
        /// </summary>
        /// <param name="negativeX">Expand into negative X space?</param>
        /// <param name="negativeY">Expand into negative Y space?</param>
        /// <returns>Returns new expanded quad tree</returns>
        public QuadTree ExpandQuadTree(bool negativeX, bool negativeY)
        {
            this.RaiseDepth();

            Vector2 size = this.dimensionsTo - this.dimensionsFrom;
            Vector2 dimensionsFrom;
            Vector2 dimensionsTo;

            // Calculate new dimensions
            if (negativeX)
            {
                dimensionsFrom.X = this.dimensionsFrom.X - size.X;
                dimensionsTo.X = this.dimensionsTo.X;
            }
            else
            {
                dimensionsFrom.X = this.dimensionsFrom.X;
                dimensionsTo.X = this.dimensionsTo.X + size.X;
            }

            if (negativeY)
            {
                dimensionsFrom.Y = this.dimensionsFrom.Y - size.Y;
                dimensionsTo.Y = this.dimensionsTo.Y;
            }
            else
            {
                dimensionsFrom.Y = this.dimensionsFrom.Y;
                dimensionsTo.Y = this.dimensionsTo.Y + size.Y;
            }

            // Create new tree and insert current tree into it
            QuadTree newTree = new QuadTree(dimensionsFrom, dimensionsTo, this.maxDepth);

            if (!negativeX && !negativeY)
                newTree.quadOne = this;

            if (negativeX && !negativeY)
                newTree.quadTwo = this;

            if (!negativeX && negativeY)
                newTree.quadThree = this;

            if (negativeX && negativeY)
                newTree.quadFour = this;

            return newTree;
        }