Vertice VerticeCercano(Point Coordenada)
        {
            List <Vertice> lv            = G.getLv();
            bool           PrimerVertice = true;
            Vertice        Cercano       = null;

            foreach (Vertice v in lv)
            {
                if (PrimerVertice == true)
                {
                    Cercano       = v;
                    PrimerVertice = false;
                }
                else
                {
                    int DifCercano = Math.Abs(Coordenada.X - Cercano.getX()) + Math.Abs(Coordenada.Y - Cercano.getY());
                    int DifNuevo   = Math.Abs(Coordenada.X - v.getX()) + Math.Abs(Coordenada.Y - v.getY());
                    if (DifNuevo < DifCercano)
                    {
                        Cercano = v;
                    }
                }
            }
            return(Cercano);
        }
        private void CbNumPresas_SelectedIndexChanged(object sender, EventArgs e)
        {
            LimpiarBitmap(Animacion, Color.Transparent);
            Rectangle Dimensiones = new Rectangle(0, 0, Picture.Width, Picture.Height);

            System.Drawing.Imaging.PixelFormat Formato = Picture.PixelFormat;
            Grafo = copia.Clone(Dimensiones, Formato);
            pictureBox1.BackgroundImage = Grafo;
            pictureBox1.Refresh();
            this.CbNumDepredadoras.Items.Clear();
            int Elegido   = this.CbNumPresas.SelectedIndex + 1;
            int Sobrantes = G.getLv().Count - Elegido;

            for (int i = 1; i <= Sobrantes; i++)
            {
                this.CbNumDepredadoras.Items.Add(i);
            }
            if (CbNumDepredadoras.Text != "" && CbNumPresas.Text != "")
            {
                this.BtnIngresarAgentes.Enabled = true;
            }
            Inserciones          = false;
            ContVertices         = 0;
            this.NumAgentes.Text = ContVertices.ToString();
            Usados.Clear();
            ListaDepredadores.Items.Clear();
            ListaRadar.Items.Clear();
        }
Ejemplo n.º 3
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();
                    }
                }
            }
        }
 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");
     }
 }