Ejemplo n.º 1
0
        private void btnCompute2_Click(object sender, EventArgs e)
        {
            try
            {
                DijkstraFinding dijkstra = new DijkstraFinding(BusManager.Inst());

                string[] listRouter = tbRouters.Text.Split(',');
                for (int i = 0; i < listRouter.Length; i++)
                {
                    string[] parts    = listRouter[i].Split('_');
                    int      routerId = Int32.Parse(parts[0]);
                    int      turn     = Int32.Parse(parts[1]);

                    dijkstra.addRouterAtStation(routerId, Routers[routerId].getStations(turn)[0], turn);
                }
                Vertex dest = dijkstra.computePath(_startStation, _endStation);
                if (dest == null || dest.PreviewVertex == null)
                {
                    MessageBox.Show("Cannot find shortest path");
                    return;
                }
                List <int> pathStation = dijkstra.convertPathToStations(dest);
                //clearMap();
                sortedPathOverlay.Clear();
                drawStationPath(pathStation, Color.Red, sortedPathOverlay);
            } catch
            {
                MessageBox.Show("Invalid routers. Try again!");
            }
        }
Ejemplo n.º 2
0
        private void btnCompute_Click(object sender, EventArgs e)
        {
            if (listRouter.SelectedItem == null)
            {
                MessageBox.Show("Not selected router", "Error", MessageBoxButtons.OK);
                return;
            }
            int routerId = Int32.Parse(listRouter.SelectedItem.ToString().Split('_')[0]);
            int turn     = Int32.Parse(listRouter.SelectedItem.ToString().Split('_')[1]);

            DijkstraFinding dijkstra = new DijkstraFinding(BusManager.Inst());
            Router          router   = null;

            if (!Routers.ContainsKey(routerId))
            {
                MessageBox.Show("Invalid router", "Error", MessageBoxButtons.OK);
                return;
            }
            router = Routers[routerId];
            List <int> stationIds = null;

            if (turn == 0)
            {
                stationIds = router.Stations1;
            }
            else if (turn == 1)
            {
                stationIds = router.Stations2;
            }
            if (stationIds == null)
            {
                MessageBox.Show("Invalid turn", "Error", MessageBoxButtons.OK);
                return;
            }

            for (int i = 0; i < stationIds.Count - 1; i++)
            {
                dijkstra.addPath(stationIds[i], stationIds[i + 1]);
            }

            Vertex dest = dijkstra.computePath(stationIds[0], stationIds[stationIds.Count - 20]);

            if (dest == null)
            {
                MessageBox.Show("Cannot find shortest path");
                return;
            }

            List <int> listStation = new List <int>();

            while (dest.PreviewVertex != null)
            {
                listStation.Add(dest.StationId);
                dest = dest.PreviewVertex;
            }
            listStation.Reverse();
            clearMap();
            drawStationPath(listStation, Color.Red, sortedPathOverlay);
        }