Sto cercando di creare un diagramma ad albero come il nodo, come lo example image here. Ho il seguente codice:Creazione di un diagramma del nodo dell'albero
private void DrawNode(Graphics g, Node<T> node, float xOffset, float yOffset)
{
if (node == null)
{
return;
}
Bitmap bmp = (from b in _nodeBitmaps where b.Node.Value.Equals(node.Value) select b.Bitmap).FirstOrDefault();
if (bmp != null)
{
g.DrawImage(bmp, xOffset, yOffset);
DrawNode(g, node.LeftNode, xOffset - 30 , yOffset + 20);
DrawNode(g, node.RightNode, xOffset + 30, yOffset + 20);
}
}
Il mio codice è quasi funzionante. Il problema che sto avendo è che alcuni nodi si sovrappongono. Nell'immagine sopra, i nodi 25 e 66 si sovrappongono. Il motivo, ne sono sicuro, è perché il suo posizionamento matematico dei nodi di sinistra e dei nodi di destra equivale allo spazio, quindi il nodo destro del genitore si sovrappone al nodo sinistro del genitore adiacente. Come posso risolvere questo problema?
UPDATE:
Questo è l'aggiornamento del codice che ho fatto dopo il suggerimento del DTB:
int nodeWidth = 0;
int rightChildWidth = 0;
if (node.IsLeafNode)
{
nodeWidth = bmp.Width + 50;
}
else
{
int leftChildWidth = 0;
Bitmap bmpLeft = null;
Bitmap bmpRight = null;
if (node.LeftNode != null)
{
bmpLeft =
(from b in _nodeBitmaps where b.Node.Value.Equals(node.LeftNode.Value) select b.Bitmap).
FirstOrDefault();
if (bmpLeft != null)
leftChildWidth = bmpLeft.Width;
}
if (node.RightNode != null)
{
bmpRight =
(from b in _nodeBitmaps where b.Node.Value.Equals(node.RightNode.Value) select b.Bitmap).
FirstOrDefault();
if (bmpRight != null)
rightChildWidth = bmpRight.Width;
}
nodeWidth = leftChildWidth + 50 + rightChildWidth;
}
g.DrawImage(bmp, xOffset + (nodeWidth - bmp.Width)/2, yOffset);
if (node.LeftNode != null)
{
DrawNode(g, node.LeftNode, xOffset, yOffset + 20);
}
if (node.RightNode != null)
{
DrawNode(g, node.RightNode, xOffset + nodeWidth - rightChildWidth, yOffset + 20);
}
Ecco uno screenshot da questo codice:
Non so come vorremmo sapere come risolvere il problema, dal momento che è possibile gestire qualsiasi modo vuoi: Riduci i nodi! Sposta i genitori! Consenti la sovrapposizione! Basta decidere una strategia, calcolare quando sarà necessaria tale strategia e implementarla. – dlev
@dlev - non è così facile. Se sposto i genitori, lo stesso problema succede ai bambini (che sono anche i genitori) sotto di esso. Anche la riduzione dei nodi non aiuta ... che semplicemente li riduce, ma li sovrappone ancora. Deve essere una sorta di soluzione matematica – Icemanind
Non intendo essere un flip: ho sorvolato il "calcolare quando quella strategia sarà necessaria", ma questo è davvero il punto cruciale di questo problema. Difficile dire di più senza sapere esattamente come vuoi sistemare tutto. – dlev