Exemplo n.º 1
0
        private BarnesHutTree ComputeBarnesHutTree()
        {
            float minX = float.MaxValue;
            float maxX = float.MinValue;
            float minY = float.MaxValue;
            float maxY = float.MinValue;

            foreach (Body body in _bodies)
            {
                if (body.Position.X < minX)
                {
                    minX = body.Position.X;
                }
                if (body.Position.X > maxX)
                {
                    maxX = body.Position.X;
                }
                if (body.Position.Y < minY)
                {
                    minY = body.Position.Y;
                }
                if (body.Position.Y > maxY)
                {
                    maxY = body.Position.Y;
                }
            }

            float width  = Math.Abs(maxX - minX);
            float height = Math.Abs(maxY - minY);
            float size   = (float)Math.Ceiling(Math.Max(width, height) + 0.5);
            var   middle = new Vector2((minX + maxX) / 2, (minY + maxY) / 2);
            var   origin = new Vector2(middle.X - size / 2, middle.Y - size / 2);

            var tree = new BarnesHutTree(new Rectangle(origin, size, size), 4);

            foreach (Body body in _bodies)
            {
                tree.Insert(body);
            }

            return(tree);
        }