2013-03-16 35 views
1

首先,我將展示大約我在此嘗試完成的內容。 The desired effect. 請注意,它不是確切的,因爲目前的系統無法正常工作。這就是我來這裏的原因。排序視覺節點陣列

我一直在試圖解決這個問題一段時間。到目前爲止,我得到了這個代碼,但它不能正常工作。

public double UpdateChildPosition(bool fromParent) 
    { 
     if (children.Count == 0) 
      return 0; 

     if (!fromParent && parentnode != null) 
     { 
      parentnode.UpdateChildPosition(false); 
      return 0; 
     } 

     double cwidth = 0; 
     if ((fromParent && children.Count > 0) || parentnode == null) 
      children.ForEach((n) => cwidth += n.UpdateChildPosition(true)); 

     double width = children.Sum((n) => n.rectangle1.Width + 10) + cwidth/2; 
     double x = 0 - width/2; 
     foreach (MindmapNode node in children) 
     { 
      node.x = x; 
      x += node.rectangle1.Width + 10 + cwidth/children.Count; 
      node.y = 50; 
     } 
     return width; 
    } 

相反,它創建這樣的:

Picture of the current effect http://img29.imageshack.us/img29/9660/effectg.png

我給也許有點太少的信息,但我不完全知道有多少是必要的。詢問更多信息。 謝謝!

沒有讓我發佈圖片,因爲有新帳戶。

編輯 我忘了說,在我的系統中,座標是相對於父項。

EDIT2 我已經取得了一些進展。目前的問題是:當節點的大小改變時(是的,它們是動態的)它們重疊。

List<MindmapNode> children; 
public double UpdateChildPosition(bool fromParent) 
    { 
     if (children.Count == 0) 
      return rectangle1.Width + 10; 

     if (!fromParent && parentnode != null) 
     { 
      parentnode.UpdateChildPosition(false); 
      return 0; 
     } 

     double[] childrenwidth = new double[children.Count]; 

     //if ((fromParent && children.Count > 0) || parentnode == null) 
     List<MindmapNode> rchildren = (from n in children 
             where n.moved == false 
             select n).ToList(); 
     (from n in children 
     where rchildren.Contains(n) == false 
     select n).ToList().ForEach((n) => n.UpdateChildPosition(true)); 

     int i = 0; 
     rchildren.ForEach((n) => childrenwidth[i++] = 
      Math.Max(n.UpdateChildPosition(true), (n.children.Count > 0) ? n.rectangle1.Width * 2 : 0)); 

     double width = childrenwidth.Sum(); 
     double x = -width/2; 
     i = 0; 
     foreach (MindmapNode node in rchildren) 
     { 
      x += childrenwidth[i]/2; 
      node.x = x; 
      x += childrenwidth[i]/2; 
      node.y = 50; 
      i++; 
      node.SetOrigin(); 
     } 
     return width; 
    } 
+0

你碰到什麼問題?我看不到當前和期望之間的任何區別。 – 2013-03-16 18:19:35

+0

@PieterGeerkens他希望每個子節點組位於其父節點下方,看起來像。我不知道他是否希望綠色節點或紫色節點均勻分佈;這兩者之間的選擇意味着解決問題的不同方法。 – Random832 2013-03-16 18:21:05

+0

@ Random832:現在明白了; TY。 – 2013-03-16 18:21:45

回答

0

這應該爲每個家庭工作:

X = ((children.Last.Right - children.First.Left)/2) - Width/2; 

在我看來,這是需要什麼,翻譯直入代碼的定義。

+0

我似乎無法理解這個應該如何實施。 – Rare 2013-03-16 18:27:06

+0

您是否希望將父母親放入孩子(最好是我認爲的)還是相對於父母的孩子? – 2013-03-16 18:58:17

+0

「子節點均勻分佈,遞歸遍佈各層節點。」可能應該添加相對於父項。請注意它是遞歸的,因爲在節點下的節點下可以有節點 - 大小是動態的。 – Rare 2013-03-16 19:03:50