static void DivideNodes(IList <RectangleNode <T, P> > nodes, int seed0, int seed1, List <RectangleNode <T, P> > gr0, List <RectangleNode <T, P> > gr1, ref IRectangle <P> box0, ref IRectangle <P> box1, int groupSplitThreshold) { for (int i = 0; i < nodes.Count; i++) { if (i == seed0 || i == seed1) { continue; } // ReSharper disable InconsistentNaming var box0_ = box0.Unite(nodes[i].Rectangle); double delta0 = box0_.Area - box0.Area; var box1_ = box1.Unite(nodes[i].Rectangle); double delta1 = box1_.Area - box1.Area; // ReSharper restore InconsistentNaming //keep the tree roughly balanced if (gr0.Count * groupSplitThreshold < gr1.Count) { gr0.Add(nodes[i]); box0 = box0_; } else if (gr1.Count * groupSplitThreshold < gr0.Count) { gr1.Add(nodes[i]); box1 = box1_; } else if (delta0 < delta1) { gr0.Add(nodes[i]); box0 = box0_; } else if (delta1 < delta0) { gr1.Add(nodes[i]); box1 = box1_; } else if (box0.Area < box1.Area) { gr0.Add(nodes[i]); box0 = box0_; } else { gr1.Add(nodes[i]); box1 = box1_; } } }