Example #1
0
        private void DrawPath(Graphics g)
        {
            Pen redPen = new Pen(Color.FromArgb(255, 255, 0, 0), 6);

            g.SmoothingMode = SmoothingMode.AntiAlias;
            if (from == null || to == null)
            {
                return;
            }
            if (cachedPath == null)
            {
                cachedPath = PathSolverFactory.Solver(graph, from, to, (SearchTypes)cbOpcionesBusuqueda.SelectedItem).PathTo(to);
            }

            foreach (Edge item in cachedPath)
            {
                Node  a  = graph.GetVertex(item.Either());
                Node  b  = graph.GetVertex(item.Other(item.Either()));
                float x1 = (float)map(minX, maxX, 0, mapX, a.Longitude);
                float y1 = (float)map(minY, maxY, 0, mapY, a.Latitude);

                float x2 = (float)map(minX, maxX, 0, mapX, b.Longitude);
                float y2 = (float)map(minY, maxY, 0, mapY, b.Latitude);

                g.DrawLine(redPen, x1, (float)(mapY - y1), x2, (float)(mapY - y2));
            }
        }
Example #2
0
        private void button2_Click(object sender, EventArgs e)
        {
            if (from == null || to == null)
            {
                MessageBox.Show("Primero escoja una ruta");
                return;
            }
            var           path          = PathSolverFactory.Solver(graph, from, to, (SearchTypes)cbOpcionesBusuqueda.SelectedItem).PathTo(to).ToList();
            Node          prev          = from;
            List <string> camino        = new List <string>();
            double        totalDistance = 0;

            for (int i = 0; i < path.Count;)
            {
                Edge   item     = path[i];
                Node   a        = graph.GetVertex(item.Either());
                Node   b        = graph.GetVertex(item.Other(item.Either()));
                Node   nue      = (prev == a) ? b : a;
                string calle    = StreetBetween(prev, nue);
                int    k        = i + 1;
                double distance = 0;
                distance += prev.Distance(nue);
                Node ant = nue;
                while (k < path.Count)
                {
                    Edge siguiente = path[k];
                    Node n1        = graph.GetVertex(siguiente.Either());
                    Node n2        = graph.GetVertex(siguiente.Other(siguiente.Either()));

                    Node nueAux = n1 == ant ? n2 : n1;

                    if (NodeContains(nueAux, calle))
                    {
                        distance += ant.Distance(nueAux);
                        ant       = nueAux;
                    }
                    else
                    {
                        break;
                    }
                    k++;
                }

                StringBuilder sb = new StringBuilder();
                sb.Append("Desde ");
                foreach (Way w in waysByNode[prev])
                {
                    sb.Append(w.Name + ", ");
                }
                string dis = distance > 1000 ? String.Format("{0:0.00}Km", distance / 1000) : String.Format("{0:0.00}m", distance);
                sb.AppendFormat("\nContinue por {0} {1} Hasta: \n", calle, dis);
                sb.Append(WaysByNode(ant));
                sb.Append("\n");
                camino.Add(sb.ToString());
                camino.Add("********************************");
                prev           = ant;
                totalDistance += distance;
                i              = k;
            }

            var solver = PathSolverFactory.Solver(graph, from, to, (SearchTypes)cbOpcionesBusuqueda.SelectedItem);

            double dist = solver.DistTo(to);
            string disS = totalDistance > 1000 ? String.Format("{0:0.00}Km", totalDistance / 1000) : String.Format("{0:0.00}m", totalDistance);

            camino.Add(String.Format("Ha llegado a su destino en {0} visitando {1} nodos", disS, solver.ExploredNodes));

            if (Math.Abs(totalDistance - dist) > 1)
            {
                throw new Exception("Camino incorrecto");
            }
            else
            {
                Console.WriteLine("todo bien " + dist);
            }

            PathFrm p = new PathFrm(camino);

            p.ShowDialog();
        }