private void DrawBvhAABB <TValue>(IDebugCanvas debugCanvas, BvhTreeAABB <TValue> bvhRoot) { var q = new Queue <(BvhTreeAABB <TValue>, int)>(); q.Enqueue((bvhRoot, 0)); var maxDepth = -1; while (q.Count > 0) { var(node, depth) = q.Dequeue(); if (node.First == null) { maxDepth = Math.Max(depth, maxDepth); } else { q.Enqueue((node.First, depth + 1)); q.Enqueue((node.Second, depth + 1)); } } void Helper(BvhTreeAABB <TValue> bvh, int depth = 0) { if (bvh.First != null) { var r = maxDepth == 0 ? 0 : (int)(255 * depth / (float)maxDepth); var g = 255 - r; debugCanvas.DrawAxisAlignedBoundingBox(bvh.Bounds, new StrokeStyle(Color.FromArgb(r, g, 0), 3)); Helper(bvh.First, depth + 1); Helper(bvh.Second, depth + 1); } else { for (var i = bvh.StartIndexInclusive; i < bvh.EndIndexExclusive; i++) { debugCanvas.DrawAxisAlignedBoundingBox(bvh.BoundingBoxes[i], new StrokeStyle(Color.Black, 3)); } } } Helper(bvhRoot); }