예제 #1
0
        public Dijkstra(Grafo grafo, Vertice Origen, Vertice Destino)
        {
            G = grafo;
            List <ElementoDijktra> ArregloDijkstra = new List <ElementoDijktra>();
            List <ElementoDijktra> Desencolados    = new List <ElementoDijktra>();

            Camino = new List <Vertice>();
            foreach (Vertice v in G.getLv())
            {
                ArregloDijkstra.Add(new ElementoDijktra(v));
            }
            foreach (ElementoDijktra v in ArregloDijkstra)
            {
                if (v.GetOrigen().Equals(Origen))
                {
                    v.SetDistanciaAcumulada(0);
                }
            }
            while (!Solucion(ArregloDijkstra))
            {
                ArregloDijkstra.Sort((x, y) => x.GetDistanciaAcumulada().CompareTo(y.GetDistanciaAcumulada()));
                ElementoDijktra menor = ArregloDijkstra[0];
                ArregloDijkstra.RemoveAt(0);
                Desencolados.Add(menor);
                ActualizaVector(ArregloDijkstra, menor);
            }
            foreach (ElementoDijktra a in Desencolados)
            {
                if (a.GetOrigen() == Destino)
                {
                    ElementoDijktra Proveniente = a;
                    while (Proveniente != null)
                    {
                        Camino.Add(Proveniente.GetOrigen());
                        Proveniente = Proveniente.GetProveniente();
                    }
                }
            }
        }
        Vertice EncontrarCentro(int x, int y)
        {
            int x_i, x_f, x_act, x_c;
            int y_i, y_f, y_act, y_c;

            y_act = y;
            y_i   = y;
            do
            {
                colorpixel = Picture.GetPixel(x, ++y_act);
            } while (colorpixel == Negro || colorpixel == Rojo);
            y_f   = y_act - 1;
            y_act = y;
            do
            {
                colorpixel = Picture.GetPixel(x, --y_act);
            } while (colorpixel == Negro || colorpixel == Rojo);
            y_i   = y_act + 1;
            y_c   = (y_i + y_f) / 2;
            x_act = x;
            do
            {
                colorpixel = Picture.GetPixel(--x_act, y_c);
            } while (colorpixel == Negro || colorpixel == Rojo);
            x_i   = x_act + 1;
            x_act = x;
            do
            {
                colorpixel = Picture.GetPixel(++x_act, y_c);
            } while (colorpixel == Negro || colorpixel == Rojo);
            x_f = x_act - 1;
            x_c = (x_f + x_i) / 2;
            int     r       = (x_f - x_c);
            Vertice vertice = new Vertice(x_c, y_c, r, ContVertices++);

            return(vertice);
        }
예제 #3
0
 public ElementoDijktra(Vertice inicio)
 {
     Origen             = inicio;
     DistanciaAcumulada = double.PositiveInfinity;
     Proveniente        = null;
 }
        public void addArista(Vertice vDest, double pond, Vertice vOrigen, List <Point> Lista)
        {
            Arista e = new Arista(vDest, pond, vOrigen, Lista);

            la.Add(e);
        }
        private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            List <Vertice>  Visitados = new List <Vertice>();
            Stack <Vertice> Pila      = new Stack <Vertice>();
            List <Vertice>  Posible   = new List <Vertice>();

            if (CbNumPresas.Text == "" || CbNumDepredadoras.Text == "")
            {
                return;
            }
            if (ContVertices < int.Parse(CbNumPresas.Text) + int.Parse(CbNumDepredadoras.Text))
            {
                Point   Coordenada = new Point(e.X, e.Y);
                Vertice Cercano    = VerticeCercano(Coordenada);
                if (!Usados.Contains(Cercano))
                {
                    if (ContVertices < int.Parse(CbNumPresas.Text))
                    {
                        Point Punto = new Point(Cercano.getX(), Cercano.getY());
                        DibujarCirculo(Punto, BrochaPresa, Animacion);
                        pictureBox1.Refresh();
                        Vertice DestinoElegido = Cercano;
                        do
                        {
                            EleccionDestino Destino = new EleccionDestino(G, Cercano, Grafo);
                            Destino.ShowDialog();
                            DestinoElegido = Destino.GetDestino();
                            Pila.Push(Cercano);
                            Visitados.Add(Cercano);
                            Posible.Add(Cercano);
                            DFS(Pila, Visitados, Posible);
                            bool Imposible = true;
                            foreach (Vertice v in Posible)
                            {
                                if (v.Equals(DestinoElegido))
                                {
                                    Imposible = false;
                                    break;
                                }
                            }
                            if (Imposible == true)
                            {
                                DestinoElegido = Cercano;
                            }
                        } while (DestinoElegido == Cercano);
                        Dijkstra D = new Dijkstra(G, Cercano, DestinoElegido);
                        D.GetCamino().Reverse();
                        Agente presa = new Agente(Cercano, ++ContVertices, DestinoElegido, D.GetCamino());
                        Presa.Add(presa);
                    }
                    else
                    {
                        Pila.Push(Cercano);
                        Visitados.Add(Cercano);
                        Posible.Add(Cercano);
                        DFS(Pila, Visitados, Posible);
                        Agente depredador = new Agente(Cercano, ++ContVertices, null, Posible);
                        Point  Punto      = new Point(Cercano.getX(), Cercano.getY());
                        DibujarCirculo(Punto, BrochaDepredador, Animacion);
                        ListaDepredadores.Items.Add(depredador.ToString());
                        Depredador.Add(depredador);
                    }
                    pictureBox1.Refresh();
                    this.NumAgentes.Text = ContVertices.ToString();
                    Usados.Add(Cercano);
                }
            }
        }
 void BtnCargarImagenClick(object sender, EventArgs e)
 {
     try
     {
         if (openFileDialog1.ShowDialog() == DialogResult.OK)
         {
             NombreImagen = openFileDialog1.FileName;
             Image imagen = Image.FromFile(NombreImagen);
             Picture = new Bitmap(imagen, pictureBox1.Width, pictureBox1.Height);
             Rectangle Dimensiones = new Rectangle(0, 0, Picture.Width, Picture.Height);
             System.Drawing.Imaging.PixelFormat Formato = Picture.PixelFormat;
             Grafo                           = Picture.Clone(Dimensiones, Formato);
             pictureBox1.Image               = Picture;
             labelMenor.Text                 = "";
             this.BtnAnimacion.Enabled       = false;
             this.CbNumPresas.Enabled        = false;
             this.CbNumDepredadoras.Enabled  = false;
             this.CbNumPresas.Text           = "";
             this.CbNumDepredadoras.Text     = "";
             this.BtnIngresarAgentes.Enabled = false;
             G = new Grafo();
             ListaRadar.Items.Clear();
             ListaDepredadores.Items.Clear();
             ContVertices = 1;
             this.CbNumPresas.Items.Clear();
             this.CbNumDepredadoras.Items.Clear();
             int            x, y;
             List <Vertice> lv = G.getLv();
             for (y = 0; y < Picture.Height; y += 20)
             {
                 for (x = 0; x < Picture.Width; x += 20)
                 {
                     colorpixel = Picture.GetPixel(x, y);
                     if (colorpixel == Negro)
                     {
                         Vaux = EncontrarCentro(x, y);
                         if (lv.Contains(lv.Find(l => l.getX() == Vaux.getX() && l.getY() == Vaux.getY())))
                         {
                             ContVertices--;
                         }
                         else
                         {
                             if (lv.Contains(lv.Find(l => l.getX() == Vaux.getX() && l.getY() == Vaux.getY() + 1)))
                             {
                                 ContVertices--;
                             }
                             else
                             {
                                 if (lv.Contains(lv.Find(l => l.getX() == Vaux.getX() + 1 && l.getY() == Vaux.getY())))
                                 {
                                     ContVertices--;
                                 }
                                 else
                                 {
                                     if (lv.Contains(lv.Find(l => l.getX() == Vaux.getX() && l.getY() == Vaux.getY() - 1)))
                                     {
                                         ContVertices--;
                                     }
                                     else
                                     {
                                         if (lv.Contains(lv.Find(l => l.getX() == Vaux.getX() - 1 && l.getY() == Vaux.getY())))
                                         {
                                             ContVertices--;
                                         }
                                         else
                                         {
                                             if (lv.Contains(lv.Find(l => l.getX() == Vaux.getX() + 1 && l.getY() == Vaux.getY() + 1)))
                                             {
                                                 ContVertices--;
                                             }
                                             else
                                             {
                                                 if (lv.Contains(lv.Find(l => l.getX() == Vaux.getX() - 1 && l.getY() == Vaux.getY() - 1)))
                                                 {
                                                     ContVertices--;
                                                 }
                                                 else
                                                 {
                                                     if (lv.Contains(lv.Find(l => l.getX() == Vaux.getX() + 1 && l.getY() == Vaux.getY() - 1)))
                                                     {
                                                         ContVertices--;
                                                     }
                                                     else
                                                     {
                                                         if (lv.Contains(lv.Find(l => l.getX() == Vaux.getX() - 1 && l.getY() == Vaux.getY() + 1)))
                                                         {
                                                             ContVertices--;
                                                         }
                                                         else
                                                         {
                                                             lv.Add(Vaux);
                                                         }
                                                     }
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
             Graphics graphics = Graphics.FromImage(Grafo);
             for (int i = 0; i < lv.Count; i++)
             {
                 for (int j = 0; j < lv.Count; j++)
                 {
                     p_0.X = lv[i].getX();
                     p_0.Y = lv[i].getY();
                     p_f.X = lv[j].getX();
                     p_f.Y = lv[j].getY();
                     if (lv[i].getId().ToString() == lv[j].getId().ToString())
                     {
                     }
                     else
                     {
                         List <Point> Resultado = DDA();
                         if (Resultado != null)
                         {
                             pictureBox1.Refresh();
                             double distancia = Math.Sqrt(Math.Pow((double)p_f.X - (double)p_0.X, 2) + Math.Pow((double)p_f.Y - (double)p_0.Y, 2));
                             lv[i].addArista(lv[j], distancia, lv[i], Resultado);
                         }
                     }
                 }
             }
             for (int i = 0; i < lv.Count; i++)
             {
                 List <Arista> la = lv[i].getLa();
                 DibujarCentro(lv[i].getX(), lv[i].getY());
                 graphics.DrawString(lv[i].getId().ToString(), drawFont, BrochaID, lv[i].getX(), lv[i].getY());
             }
             for (int i = 1; i < lv.Count; i++)
             {
                 CbNumPresas.Items.Add(i);
                 CbNumDepredadoras.Items.Add(i);
             }
             this.CbNumPresas.Enabled       = true;
             this.CbNumDepredadoras.Enabled = true;
             ContVertices                      = 0;
             this.NumAgentes.Text              = ContVertices.ToString();
             pictureBox1.BackgroundImage       = Grafo;
             pictureBox1.BackgroundImageLayout = ImageLayout.Zoom;
             Animacion         = Grafo.Clone(Dimensiones, Formato);
             copia             = Grafo.Clone(Dimensiones, Formato);
             pictureBox1.Image = Animacion;
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("El archivo seleccionado no es un tipo de imagen válido");
     }
 }
 private void BtnIngresarAgentes_Click(object sender, EventArgs e)
 {
     if (checkBox1.Checked == true)
     {
         List <Vertice> lv = G.getLv();
         Presa.Clear();
         Depredador.Clear();
         ListaDepredadores.Items.Clear();
         LimpiarBitmap(Animacion, Color.Transparent);
         pictureBox1.Refresh();
         Random random  = new Random();
         int    id      = 1;
         int[]  numeros = new int[lv.Count];
         int    cont    = 0;
         int    Elegido = int.Parse(CbNumPresas.Text) + int.Parse(CbNumDepredadoras.Text) + 1;
         bool   BanderaIgual;
         int    Aristas = 0;
         foreach (Vertice v in lv)
         {
             Aristas = Aristas + v.getLa().Count;
         }
         if (Aristas == 0)
         {
             return;
         }
         for (int j = 1; j < Elegido; j++)
         {
             int NumeroAleatorio = random.Next(1, lv.Count + 1);
             BanderaIgual = false;
             for (int i = 0; i < cont; i++)
             {
                 if (NumeroAleatorio.Equals(numeros[i]))
                 {
                     BanderaIgual = true;
                     break;
                 }
             }
             if (BanderaIgual == false)
             {
                 for (int i = 0; i < lv.Count; i++)
                 {
                     if (NumeroAleatorio == lv[i].getId())
                     {
                         List <Vertice>  Visitados = new List <Vertice>();
                         Stack <Vertice> Pila      = new Stack <Vertice>();
                         List <Vertice>  Posible   = new List <Vertice>();
                         if (cont < int.Parse(CbNumPresas.Text))
                         {
                             Vertice DestinoElegido = lv[i];
                             do
                             {
                                 EleccionDestino Destino = new EleccionDestino(G, lv[i], Grafo);
                                 Destino.ShowDialog();
                                 DestinoElegido = Destino.GetDestino();
                                 Pila.Push(lv[i]);
                                 Visitados.Add(lv[i]);
                                 Posible.Add(lv[i]);
                                 DFS(Pila, Visitados, Posible);
                                 bool Imposible = true;
                                 foreach (Vertice v in Posible)
                                 {
                                     if (v.Equals(DestinoElegido))
                                     {
                                         Imposible = false;
                                         break;
                                     }
                                 }
                                 if (Imposible == true)
                                 {
                                     DestinoElegido = lv[i];
                                 }
                             } while (DestinoElegido == lv[i]);
                             Dijkstra D = new Dijkstra(G, lv[i], DestinoElegido);
                             D.GetCamino().Reverse();
                             Agente presa = new Agente(lv[i], id, DestinoElegido, D.GetCamino());
                             Point  Punto = new Point(lv[i].getX(), lv[i].getY());
                             DibujarCirculo(Punto, BrochaPresa, Animacion);
                             Presa.Add(presa);
                         }
                         else
                         {
                             Pila.Push(lv[i]);
                             Visitados.Add(lv[i]);
                             Posible.Add(lv[i]);
                             DFS(Pila, Visitados, Posible);
                             Agente depredador = new Agente(lv[i], id, null, Posible);
                             Point  Punto      = new Point(lv[i].getX(), lv[i].getY());
                             DibujarCirculo(Punto, BrochaDepredador, Animacion);
                             ListaDepredadores.Items.Add(depredador.ToString());
                             Depredador.Add(depredador);
                         }
                         pictureBox1.Refresh();
                         id++;
                         numeros[cont] = NumeroAleatorio;
                         cont++;
                     }
                 }
             }
             else
             {
                 j--;
             }
         }
         this.NumAgentes.Text = cont.ToString();
     }
     else
     {
         if (ContVertices == 0)
         {
             MessageBox.Show("Haga Click en los Vertices en los Cuales desea Ingresar a las Presas y Depredadoras");
             ListaDepredadores.Items.Clear();
             Presa.Clear();
             Depredador.Clear();
             Inserciones = true;
         }
         if (Inserciones == true && ContVertices != int.Parse(CbNumPresas.Text) + int.Parse(CbNumDepredadoras.Text))
         {
             if (ContVertices != 0)
             {
                 MessageBox.Show("Presas o Depredadores Faltantes de Ingresar");
             }
         }
         else
         {
             LimpiarBitmap(Animacion, Color.Transparent);
             pictureBox1.Refresh();
             Inserciones          = false;
             ContVertices         = 0;
             this.NumAgentes.Text = ContVertices.ToString();
             Usados.Clear();
         }
     }
 }
 public void SetVerticeActual(Vertice vertice)
 {
     VerticeActual = vertice;
 }