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"); } }
List <Point> DDA() { double m = ((double)p_f.Y - (double)p_0.Y) / ((double)p_f.X - (double)p_0.X); double b = (double)p_0.Y - (double)m * (double)p_0.X; double y_i, x_i; bool band = false; bool band2 = false; int inc = 1; Point Coordenadas; List <Point> Linea = new List <Point>(); Coordenadas = new Point(); if (Double.IsInfinity(m)) { if (p_0.Y > p_f.Y) { inc = -1; } for (y_i = p_0.Y; (int)y_i != p_f.Y; y_i += inc) { x_i = p_0.X; colorpixel = Picture.GetPixel((int)Math.Round(x_i), (int)y_i); if (colorpixel == Blanco) { band = true; } if (colorpixel != Blanco && band == true) { band2 = true; } if (colorpixel == Blanco && band2 == true) { return(null); } } for (y_i = p_0.Y; (int)y_i != p_f.Y; y_i += inc) { x_i = p_0.X; Coordenadas.X = (int)x_i; Coordenadas.Y = (int)y_i; Linea.Add(Coordenadas); Grafo.SetPixel(Coordenadas.X, Coordenadas.Y, Color.Black); } return(Linea); } if (m <= 1 && m >= -1) { if (p_0.X > p_f.X) { inc = -1; } for (x_i = p_0.X; (int)x_i != p_f.X; x_i += inc) { y_i = m * x_i + b; colorpixel = Picture.GetPixel((int)x_i, (int)Math.Round(y_i)); if (colorpixel == Blanco) { band = true; } if (colorpixel != Blanco && band == true) { band2 = true; } if (colorpixel == Blanco && band2 == true) { return(null); } } for (x_i = p_0.X; (int)x_i != p_f.X; x_i += inc) { y_i = m * x_i + b; Coordenadas.X = (int)x_i; Coordenadas.Y = (int)Math.Round(y_i); Linea.Add(Coordenadas); Grafo.SetPixel(Coordenadas.X, Coordenadas.Y, Color.Black); } return(Linea); } else { if (p_0.Y > p_f.Y) { inc = -1; } for (y_i = p_0.Y; (int)y_i != p_f.Y; y_i += inc) { x_i = (y_i - b) / m; colorpixel = Picture.GetPixel((int)Math.Round(x_i), (int)y_i); if (colorpixel == Blanco) { band = true; } if (colorpixel != Blanco && band == true) { band2 = true; } if (colorpixel == Blanco && band2 == true) { return(null); } } for (y_i = p_0.Y; (int)y_i != p_f.Y; y_i += inc) { x_i = (y_i - b) / m; Coordenadas.X = (int)Math.Round(x_i); Coordenadas.Y = (int)y_i; Linea.Add(Coordenadas); Grafo.SetPixel(Coordenadas.X, Coordenadas.Y, Color.Black); } return(Linea); } }