public QuadTreeNode(Rectangle a_BoundingRectangle, List<Tuple<Rectangle, int>> a_Rectangles, int a_Depth) { hasChilds = false; boundingRectangle = a_BoundingRectangle; rectangles = a_Rectangles; //ITems per node == 4 //Max depth == 10 if ( a_Rectangles.Count > 3 && a_Depth < 5) { hasChilds = true; Rectangle topLeft, topRight, bottomLeft, bottomRight; int halfWidth = boundingRectangle.Width / 2; int halfHeight = boundingRectangle.Height / 2; topLeft = new Rectangle(a_BoundingRectangle.X, a_BoundingRectangle.Y, halfWidth, halfHeight); topRight = new Rectangle( a_BoundingRectangle.X + halfWidth, a_BoundingRectangle.Y, halfWidth, halfHeight ); bottomLeft = new Rectangle(a_BoundingRectangle.X, a_BoundingRectangle.Y + halfHeight, halfWidth, halfHeight ); bottomRight = new Rectangle( a_BoundingRectangle.X + halfWidth, a_BoundingRectangle.Y + halfHeight, halfWidth, halfHeight ); List<Tuple<Rectangle, int>> tlRectangles = new List<Tuple<Rectangle, int>>(); List<Tuple<Rectangle, int>> trRectangles = new List<Tuple<Rectangle, int>>(); List<Tuple<Rectangle, int>> blRectangles = new List<Tuple<Rectangle, int>>(); List<Tuple<Rectangle, int>> brRectangles = new List<Tuple<Rectangle, int>>(); foreach (Tuple<Rectangle, int> pair in a_Rectangles) { if (pair.Item1.Intersects(topLeft) == true) tlRectangles.Add(pair); if (pair.Item1.Intersects(topRight) == true) trRectangles.Add(pair); if (pair.Item1.Intersects(bottomLeft) == true) blRectangles.Add(pair); if (pair.Item1.Intersects(bottomRight) == true) brRectangles.Add(pair); } childNodes = new QuadTreeNode[4]; //Left top childNodes[0] = new QuadTreeNode(topLeft, tlRectangles, a_Depth + 1); //Right top childNodes[1] = new QuadTreeNode(topRight, trRectangles, a_Depth + 1); //Left bottom childNodes[2] = new QuadTreeNode(bottomLeft, blRectangles, a_Depth + 1); //Right bottom childNodes[3] = new QuadTreeNode(bottomRight, brRectangles, a_Depth + 1); } }
public AssemblyQuadTree(Rectangle a_ScreenRectangle, List<Tuple<Rectangle, int>> a_Rectangles) { rootNode = new QuadTreeNode(a_ScreenRectangle, a_Rectangles, 0 ); }