//вычислить координаты для каждого узла и записать в 2 разных списка либо точку самого узла (V), либо пару точек, задающую ребро (E) public void GetPoints(TNode t, int canvasWidth, int canvasHeight, EList E, EList V) { if (t.level > 0) { int step = canvasWidth / (SizeOfLevel(root, t.level) + 1); TNode prev = null; bool f1 = false; GetPrev(root, t, ref prev, ref f1); Point current; if (prev == null) { current = new Point(step, t.level * canvasHeight / MainForm.MaxDEPTH - 15); } else { current = new Point(prev.tPoint.X + step, t.level * canvasHeight / MainForm.MaxDEPTH - 15); } t.tPoint = current; V.Add(current, t.index); E.Add(current, t.Parent.tPoint); if (t.Branches != null && t.Branches.Length != 0) { for (int i = 0; i < t.Branches.Length; i++) { GetPoints(t.Branches[i], canvasWidth, canvasHeight, E, V); } } } else { V.Add(t.tPoint, t.index); if (t.Branches != null && t.Branches.Length != 0) { for (int i = 0; i < t.Branches.Length; i++) { GetPoints(t.Branches[i], canvasWidth, canvasHeight, E, V); } } } }
public static void GenerateBranches (TNode curRoot, int maxDepth, int maxBranches, int canvasWidth, int canvasHeight, ref EList E, ref EList V, int ind) { Random r = new Random(); if (maxDepth == MainForm.MaxDEPTH) { curRoot.tPoint = new Point(canvasWidth / 2, 15); V.Add(curRoot.tPoint, new Point(0, 0)); curRoot.Branches = new TNode[r.Next(maxBranches)]; for (int p = 0; p < curRoot.Branches.Length; p++) { curRoot.Branches[p] = new TNode() { index = p + 1, level = MainForm.MaxDEPTH - maxDepth + 1, notVisited = true, Parent = curRoot }; //if (ind < V.indicator.Length) // V.indicator[ind]++; //ind++; GenerateBranches(curRoot.Branches[p], maxDepth - 1, maxBranches, canvasWidth, canvasHeight, ref E, ref V, ind); } } else if (maxDepth != 0) { curRoot.Branches = new TNode[r.Next(maxBranches)]; //запись координат точки текущего узла и запись пары точек для последующего рисования ребра int x = (curRoot.index + ind) * canvasWidth / (curRoot.Parent.Branches.Length + 1), y = curRoot.level * canvasHeight / MainForm.MaxDEPTH + 15; curRoot.tPoint = new Point(x, y); V.Add(new Point(x, y), new Point(0, 0)); E.Add(new Point(x, y), curRoot.Parent.tPoint); for (int p = 0; p < curRoot.Branches.Length; p++) { curRoot.Branches[p] = new TNode() { index = p + 1, level = MainForm.MaxDEPTH - maxDepth + 1, notVisited = true, Parent = curRoot }; //if (ind < V.indicator.Length) // V.indicator[ind]++; //ind++; GenerateBranches(curRoot.Branches[p], maxDepth - 1, maxBranches, canvasWidth, canvasHeight, ref E, ref V, ind); } } else { int x = (curRoot.index + ind) * canvasWidth / (curRoot.Parent.Branches.Length + 1), y = curRoot.level * canvasHeight / MainForm.MaxDEPTH + 15; curRoot.tPoint = new Point(x, y); V.Add(new Point(x, y), new Point(0, 0)); E.Add(new Point(x, y), curRoot.Parent.tPoint); } }