public void CreateSubRegion(int currentDepth)
{
// The four sub regions of the quad tree
// +--------------+
// | nw | ne |
// |------+pivot--|
// | sw | se |
// +--------------+
BoundingBox box;
// 1. region south west
box = new BoundingBox(bounds.Xmin, bounds.Ymin, pivot.X, pivot.Y);
regions[0] = new QuadNode(box, tree);
// 2. region south east
box = new BoundingBox(pivot.X, bounds.Ymin, bounds.Xmax, pivot.Y);
regions[1] = new QuadNode(box, tree);
// 3. region north west
box = new BoundingBox(bounds.Xmin, pivot.Y, pivot.X, bounds.Ymax);
regions[2] = new QuadNode(box, tree);
// 4. region north east
box = new BoundingBox(pivot.X, pivot.Y, bounds.Xmax, bounds.Ymax);
regions[3] = new QuadNode(box, tree);
Point[] triangle = new Point[3];
// Find region for every triangle vertex
foreach (var index in triangles)
{
ITriangle tri = tree.triangles[index];
triangle[0] = tri.GetVertex(0);
triangle[1] = tri.GetVertex(1);
triangle[2] = tri.GetVertex(2);
AddTriangleToRegion(triangle, tri.ID);
}
for (int i = 0; i < 4; i++)
{
if (regions[i].triangles.Count > tree.sizeBound && currentDepth < tree.maxDepth)
{
regions[i].CreateSubRegion(currentDepth + 1);
}
}
}