private void AvoidSubgraphOverlap()
{
bool done;
do
{
done = true;
foreach (var g1 in LayoutSubgraphs.Skip(1))
{
foreach (var g2 in LayoutSubgraphs.Skip(1))
{
// The first subgraph's center point must be higher than the second subgraph
if (!g1.Equals(g2) && (g1.GraphCenterY + g1.OffsetY <= g2.GraphCenterY + g2.OffsetY))
{
var g1nodes = g1.Nodes.OrderBy(n => n.Y + n.TotalHeight);
var g2nodes = g2.Nodes.OrderBy(n => n.Y);
foreach (var node1 in g1nodes)
{
foreach (var node2 in g2nodes)
{
// If any two nodes from these two different subgraphs overlap
if ((node1.Y + node1.TotalHeight + GraphLayout.Graph.VerticalNodeDistance + g1.OffsetY > node2.Y + g2.OffsetY) &&
(((node1.X <= node2.X) && (node1.X + node1.Width + GraphLayout.Graph.HorizontalNodeDistance > node2.X)) ||
((node2.X <= node1.X) && (node2.X + node2.Width + GraphLayout.Graph.HorizontalNodeDistance > node1.X))))
{
// Shift the first subgraph to the top and the second subgraph to the bottom
g1.OffsetY -= 5;
g2.OffsetY += 5;
done = false;
}
if (!done) break;
}
if (!done) break;
}
}
}
}
} while (!done);
}