Exemplo n.º 1
0
        public static void InsertRandomNode(Graph g)
        {
            Random rndOne = new Random();
            Random rndTwo = new Random();
            int    one, two, type;

            one  = Convert.ToInt32(rndOne.NextDouble() * 40);
            two  = Convert.ToInt32(rndTwo.NextDouble() * 40);
            type = (one + two) % 3;

            switch (type)
            {
            case 0: g.AddNewNode(new Node(NodeType.BusStop, new Point(one, two)));
                break;

            case 1: g.AddNewNode(new Node(NodeType.Crossroads, new Point(one, two)));
                break;

            default: g.AddNewNode(new Node(NodeType.RestingPlace, new Point(one, two)));
                break;
            }
        }
Exemplo n.º 2
0
        private void panel2_MouseClick(object sender, MouseEventArgs e)
        {
            if (!ShortestPathButton.Checked)
            {
                if (InsertNodeButton.Checked) // vkladame vrcholy

                {
                    if (e.Button == MouseButtons.Left)
                    {
                        #region INSERT NODE

                        {
                            if (NodeTypeCombo.SelectedIndex == 0)
                            {
                                created = new Node(NodeType.Crossroads, new Point(e.Location.X, e.Location.Y));
                            }
                            else if (NodeTypeCombo.SelectedIndex == 1)
                            {
                                created = new Node(NodeType.RestingPlace, e.Location);
                            }
                            else
                            {
                                created = new Node(NodeType.BusStop, e.Location);
                            }

                            g.AddNewNode(created);
                            string id = created.GetId();
                            graphNodes.Add(new GraphNode(created.GetId(), created.GetLocation()));
                            drawingNodes.Add(new DrawingNode(e.Location, id, zoomstep));

                            // InsertNode();
                            panel2.Invalidate();
                            return;
                        }

                        #endregion
                    }
                    else if (e.Button == MouseButtons.Right)
                    #region REMOVE NODE
                    {
                        if (drawingNodes.Count != 0)
                        {
                            g.RemoveNode(FindNearestNode(e.Location));
                            DrawingNode removing     = null;
                            GraphNode   removingNode = null;
                            foreach (DrawingNode dn in drawingNodes)
                            {
                                if (FindNearestNode(e.Location).Equals(dn.nodeID))
                                {
                                    removing = dn;
                                }
                            }
                            foreach (GraphNode gn in graphNodes)
                            {
                                if (FindNearestNode(e.Location).Equals(gn.Key))
                                {
                                    removingNode = gn;
                                }
                            }
                            drawingNodes.Remove(removing);
                            graphNodes.Remove(removingNode);
                            panel2.Invalidate();
                        }
                        else
                        {
                            MessageBox.Show("V grafu nejsou žádné vrcholy k odebrání", "Nelze odebrat vrchol");
                        }
                    }
                }
                #endregion

                if (InsertEdgeButton.Checked) // vytvarime hrany

                #region INSERT EDGE

                {
                    if (drawingNodes.Count == 0)
                    {
                        return;
                    }
                    string nearestkey = FindNearestNode(e.Location);
                    {
                        // je u bodu

                        if (begining) // nastavujeme pocatecni bod
                        {
                            to       = null;
                            from     = g.FindNode(nearestkey); // oznacit jako pocatek
                            begining = false;

                            /*       if (e.Button == MouseButtons.Right && selecting == false)
                             *     {
                             *     //    XORloc = new Point(from.GetLocation().X, from.GetLocation().Y);
                             *         selectFirst = e.Location;
                             *         selecting = true;
                             *     }*/
                            //else
                            {
                                XORloc = new Point(from.GetLocation().X, from.GetLocation().Y);
                                cara(from.GetLocation(), XORloc, Color.MediumBlue, panel2.CreateGraphics());
                            }
                        }
                        else //nastavujeme koncovy bod
                        {
                            to       = g.FindNode(nearestkey); // oznacit jako konec
                            begining = true;
                        }
                        if (to != null)
                        {
                            if (e.Button == MouseButtons.Left)
                            {
                                try
                                {
                                    g.AddNewEdge(from, to);
                                }
                                catch (LoopEdgeException exx)
                                {
                                    MessageBox.Show(
                                        "Smyčková hrana nemůže být vložena ( počáteční vrchol se shoduje s koncovým)",
                                        "Nelze vložit smyčkovou hranu");
                                }
                            }
                            else if (e.Button == MouseButtons.Right)
                            {
                                /*  if (selecting)
                                 * {
                                 *    ;
                                 * }
                                 * selectFirst = e.Location;
                                 * List<GraphNode> selected = SelectNodes();*/

                                foreach (Follower f in from.GetFollowers())
                                {
                                    if (f.GetReferencedKey() == to.GetId())
                                    {
                                        f.DisableEdge();
                                    }
                                }
                                foreach (Follower f in to.GetFollowers())
                                {
                                    if (f.GetReferencedKey() == from.GetId())
                                    {
                                        f.DisableEdge();
                                    }
                                }
                            }
                        }
                    }


                    panel2.Invalidate();
                    return;
                }

                #endregion }
            }
            else
            {
                #region SHORTEST PATH
                if (drawingNodes.Count == 0 || drawingNodes.Count == 1)
                {
                    MessageBox.Show("Nedostatek vrcholů pro výpočet cesty", "Chyba hledání cesty");
                    return;
                }
                if (e.Button == MouseButtons.Right)
                {
                    pathBegin    = null;
                    pathEnd      = null;
                    beginingPath = true;
                    ShortestPathButton.Checked = false;
                    shortestPath.Clear();
                    return;
                }
                if (beginingPath)
                {
                    string key = FindNearestNode(e.Location);
                    pathBegin    = g.FindNode(key);
                    beginingPath = false;
                    pathEnd      = null;
                }
                else
                {
                    string key2 = FindNearestNode(e.Location);
                    pathEnd = g.FindNode(key2);

                    shortestPath     = dijkstra.ExecuteOptimized(pathBegin, pathEnd);
                    drawShortestPath = true;
                    panel2.Invalidate();
                }

                #endregion
            }
        }