Esempio n. 1
0
        // Creates a collection of randomly colored nodes.
        // Respects the maximum and minimum number of nodes.
        private ObservableCollection <SimpleData> GenerateNodes()
        {
            var nodeSource = new ObservableCollection <SimpleData>();
            int minNodes, maxNodes;

            if (!int.TryParse(txtMinNodes.Text, out minNodes))
            {
                minNodes = 0;
            }
            if (!int.TryParse(txtMaxNodes.Text, out maxNodes) || minNodes > maxNodes)
            {
                maxNodes = minNodes;
            }
            int numberOfNodes = rand.Next(minNodes, maxNodes + 1);

            for (int i = 0; i < numberOfNodes; i++)
            {
                nodeSource.Add(new SimpleData()
                {
                    Key   = "Node" + i.ToString(),
                    Color = String.Format("#{0:X}{1:X}{2:X}", 120 + rand.Next(100), 120 + rand.Next(100), 120 + rand.Next(100))
                });
            }
            // Randomize the nodes a little:
            for (int i = 0; i < nodeSource.Count; i++)
            {
                int        swap = rand.Next(0, nodeSource.Count);
                SimpleData temp = nodeSource[swap];
                nodeSource[swap] = nodeSource[i];
                nodeSource[i]    = temp;
            }
            return(nodeSource);
        }
Esempio n. 2
0
        public int Compare(TreeVertex x, TreeVertex y)
        {
            SimpleData a    = x.Node.Data as SimpleData;
            SimpleData b    = y.Node.Data as SimpleData;
            int        num1 = int.Parse(a.Key.Replace("Node", ""));
            int        num2 = int.Parse(b.Key.Replace("Node", ""));

            return(num1.CompareTo(num2));
        }
Esempio n. 3
0
        // Takes the random collection of nodes and creates a random tree with them.
        // Respects the minimum and maximum number of links from each node.
        // (The minimum can be disregarded if we run out of nodes to link to)
        private ObservableCollection <LinkData> GenerateLinks(ObservableCollection <SimpleData> nodes)
        {
            var linkSource = new ObservableCollection <LinkData>();

            if (nodes.Count == 0)
            {
                return(linkSource);
            }
            int minLinks, maxLinks;

            if (!int.TryParse(txtMinLinks.Text, out minLinks))
            {
                minLinks = 1;
            }
            if (!int.TryParse(txtMaxLinks.Text, out maxLinks) || minLinks > maxLinks)
            {
                maxLinks = minLinks;
            }

            List <SimpleData> available = nodes.ToList <SimpleData>();

            foreach (SimpleData next in nodes)
            {
                available.Remove(next);
                int children = rand.Next(minLinks, maxLinks + 1);
                for (int i = 1; i <= children; i++)
                {
                    if (available.Count == 0)
                    {
                        break;
                    }
                    SimpleData to = available[0];
                    available.Remove(to);
                    linkSource.Add(new LinkData()
                    {
                        From = next.Key, To = to.Key
                    });
                }
            }
            return(linkSource);
        }