public static Tree BuildFS(string root_dir, renderNodeFunct renderNode) { TNode c; TNode n; FileInfo fi; Type ty; var di = new DirectoryInfo(root_dir); var k = new TraversalListIDS<TNode>(TraversalType.DepthFirst); var t = new Tree(new TNode()); t.MaxDepth = t.Root.Level = 0; t.Root.Data = di; k.Add(t.Root); while (k.Count > 0) { c = k.Take(); renderNode(t,c); ty = c.Data.GetType(); if (ty == typeof(DirectoryInfo)) { di = (DirectoryInfo)c.Data; foreach (var dir in di.GetDirectories()) { n = new TNode(); n.Level = c.Level + 1; if (n.Level > t.MaxDepth) t.MaxDepth = n.Level; n.Parent = c; c.Children.Add(n); n.Data = dir; k.Add(n); } //foreach (var file in di.GetFiles()) //{ // n = new TNode(); // n.Level = c.Level + 1; // if (n.Level > t.MaxDepth) t.MaxDepth = n.Level; // n.Parent = c; // c.Children.Add(n); // n.Data = file; //} } else if (ty == typeof(FileInfo)) { fi = (FileInfo)c.Data; } } return t; }
public Tree CreateSpanningTree() { Tree t; TNode root; root=new TNode(); root.Children=this.Roots; t=new Tree(root); return t; }
public Tree(TNode root) { this.Root = root; if (root == null) { this.Count = 0; this.MaxDepth = 0; } else { this.Count = 1; this.MaxDepth = 1; } }
void fsTreeRenderNode(Tree owner, TNode c) { if (owner.MaxDepth == 0) return; t = owner; t.Root.Point.X = this.splitContainer1.Panel2.Width / 2; t.Root.Point.Y = this.splitContainer1.Panel2.Height / 2; t.UpdateAttributes(); depthMap = DepthMap.Generate(t.MaxDepth, rand); #if RADIAL t.LayoutRadial2D(radius, edge_length, layout_type, chkGrow.Checked); #else t.LayoutTopDown2D(radius, edge_length, layout_type, chkGrow.Checked); #endif Redraw(false); }
void Panel2_Click(object sender,EventArgs e) { if (t == null) return; TNode node; List<TNode> near; MouseEventArgs mouse = (MouseEventArgs)e; node = t.FindByPoint(mouse.Location, tolerance: node_diameter); if(node != null) { lblSelectedNode.Text = "node [" + node.Point.X + ", " + node.Point.Y + "]"; // TREEIFY if (mouse.Button == MouseButtons.Left) { TNode n; n = new TNode(); /* n = Tree.Generate(gen_type, depth: 1, max_breadth: 4).Root; // */ n.Level = node.Level + 1; node.Children.Add(n); t.UpdateAttributes(); depthMap = DepthMap.Generate(t.MaxDepth, rand); t.LayoutRadial2D(radius, edge_length, layout_type, chkGrow.Checked); Redraw(); } // FIND NEIBOURS else if (mouse.Button == MouseButtons.Right) { near = t.FindNearest(mouse.Location, radius: 100); foreach (TNode n in near) { draw_circle(n.Point, Color.Green, Color.Cyan); } } } }
private void draw_brach(TNode parent, TNode child) { if(parent == null) return; Graphics g; Pen p; BranchColor = NodeFillColor; g=this.GetContext(); p = new Pen(BranchColor, t.MaxDepth-(1.0f * child.Level)); g.DrawLine(p, (int)parent.Point.X, (int)parent.Point.Y, (int)child.Point.X, (int)child.Point.Y); //Point from; //Point to; //int resolution = parent.Children.Count; //from = new Point((int)parent.x, (int)parent.y); //to = new Point((int)child.x, (int)child.y); //g.DrawLines(p, curve(from, to, resolution, child.angle)); }
void selColors(TNode node) { if (node.Level == 1) { NodeLineColor = NodeFillColor = RootColor; NodeLineColor = RootLineColor; } else { NodeLineColor = NodeFillColor = depthMap.SelectColor(node.Level); } }
/// <summary> /// Generates a random tree /// </summary> /// <param name="depth">desired depth of tree</param> /// <returns> /// A newly generated Tree of specified degree "maxBreadth" /// </returns> public static Tree Generate(TraversalType type, int depth, int maxBreadth, int numFirstChildren = -1, int maxNodes = -1) { Tree t; TNode r; TNode c; Random rnd; int breadth; int d; bool finished; int i; TNode child; TraversalListIDS<TNode> ids=new TraversalListIDS<TNode>(type); int count; rnd=new Random(); r=new TNode(); t=new Tree(r); finished=false; r.Level=1; count=0; ids.Add(r); while(ids.Count > 0 && finished == false) { c=ids.Get(); // generate new level of arbirary children until desired depth is reached if(c.Level==1) { if(numFirstChildren<=0) { breadth=rnd.Next(maxBreadth)+1; } else { breadth=numFirstChildren; } } else { breadth=rnd.Next(maxBreadth)+1; } for(i = 0; i < breadth; i++) { child=new TNode(); child.Parent=c; child.Level=c.Level+1; child.ChildIndex = i; count++; c.Children.Add(child); ids.Add(child); } // finished when reached desired number of nodes or reached desired depth if(maxNodes > 0) { finished=count>=maxNodes; } else { finished=c.Level==depth; } } return t; }
public Tree(TNode root) { this.root=root; }
/// <summary> /// Generates a random tree /// </summary> /// <param name="depth">desired depth of tree</param> /// <returns> /// A newly generated Tree of specified degree "maxBreadth" /// </returns> public static Tree Generate(TraversalType gen_mechanism, int depth, int max_breadth, int num_first_children = -1, int max_nodes = -1) { Tree t = new Tree { Root = new TNode { Level = 1 } }; Random rnd = new Random(); int breadth; int i; TNode child; var k = new TraversalListIDS<TNode>(gen_mechanism); var visited = new Hashtable(); bool finished = false; TNode c; k.Add(t.Root); while (k.Count > 0 && finished == false) { // generate new level of arbirary children until desired depth is reached c = k.Take(); // finished when reached desired number of nodes or reached desired depth if (max_nodes > 0) { finished = t.Count >= max_nodes; } else { finished = c.Level == depth + 1; } if (!finished && (gen_mechanism == TraversalType.DepthFirst || !visited.ContainsKey(c))) { if (gen_mechanism == TraversalType.BreadthFirst) visited.Add(c, 0); if (c.Level == 1) { if (num_first_children == -1) { breadth = rnd.Next(1, max_breadth + 1); } else { breadth = num_first_children; } } else { breadth = rnd.Next(1, max_breadth + 1); //if(c.Level==4){ // breadth = 1000; //} //if (c.Level == depth) //{ // breadth = 255; //} } for (i = 0; i < breadth; i++) { child = new TNode(); child.Parent = c; child.Level = c.Level + 1; child.ChildIndex = i; if (child.Level > t.MaxDepth) t.MaxDepth = child.Level; t.Count++; c.Children.Add(child); k.Add(child); } } } return t; }
/// <summary> /// Connects current tree to parent /// </summary> public void AssignParent(TNode p) { //TODO: important pickup angles from parent //double prev_gap; //double arc; //double level_ratio; //double level_arc = 2.0; Root.Parent = p; p.Children.Add(Root); Root.Level = p.Level + 1; //// right bisector limit //prev_gap = c.angle - prev_angle; //c.rightBisector = c.angle - (prev_gap / 2.0); //// setup sector space for potential decendants that are positioned from current node //level_ratio = Level / (Level + 1.0); //arc = level_arc * Math.Asin(level_ratio); //c.leftTangent = c.angle + arc; //c.rightTangent = c.angle - arc; //prev_angle = c.angle; }
public void DetachNode(TNode n) { }
public Edge(TNode from, TNode to, double length) { this.From = from; this.To = to; this.Length = length; }
private void draw_brach(TNode parent, TNode child) { if(parent == null) return; Graphics g; Pen p; g=this.GetContext(); p=new Pen(BranchColor,1); g.DrawLine(p, (int)parent.x,(int)parent.y, (int)child.x,(int)child.y); }
private void radial_draw_node(TNode node) { if(node.Visible==false) return; // DRAW draw_circle(node.x,node.y, node.Level); if(branches) draw_brach(node.Parent, node); }