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); } } }
private void BtnAnimacion_Click(object sender, EventArgs e) { List <Vertice> lv = G.getLv(); Graphics graphics = Graphics.FromImage(Animacion); pictureBox1.Image = Animacion; List <Point> Linea = new List <Point>(); List <Point> Puntos = new List <Point>(); Point punto = new Point(); int a = 0, b, c; int x = 0, y, z; while (Presa.Count != 0) { b = Presa[a].GetcontAristas(); foreach (Arista arista in Presa[a].getCaminoActual()[b].getLa()) { if (arista.getVertex().Equals(Presa[a].getCaminoActual()[b + 1])) { Linea = arista.getLinea(); Presa[a].SetAristaActual(arista); } } c = Presa[a].GetcontPuntos(); if (Linea.Count - 1 <= c)//5 { c = Linea.Count - 1; } punto = Linea[c]; Presa[a].SetPuntoActual(punto); Puntos.Add(punto); DibujarCirculo(punto, BrochaPresa, Animacion); DibujarId(punto, BrochaID, Animacion, Presa[a].getId()); Presa[a].SetcontPunto(Presa[a].GetcontPuntos() + 1);//5 if (Presa[a].GetcontPuntos() >= Linea.Count) { Presa[a].SetcontPunto(0); Presa[a].SetcontAristas(Presa[a].GetcontAristas() + 1); } if (Presa[a].GetcontAristas() == Presa[a].getCaminoActual().Count - 1) { DibujarCirculo(punto, BrochaPresa, Grafo); pictureBox1.BackgroundImage = Grafo; Presa.Remove(Presa[a]); } a++; if (a >= Presa.Count) { a = 0; while (true) { bool Principal = false; if (Depredador[x].Equals(DepredadorPrincipal)) { Principal = true; } y = Depredador[x].GetcontAristas(); foreach (Arista arista in Depredador[x].getCaminoActual()[y].getLa()) { if (arista.getVertex().getId() == Depredador[x].getCaminoActual()[y + 1].getId()) { Linea = arista.getLinea(); Depredador[x].SetAristaActual(arista); } } z = Depredador[x].GetcontPuntos(); if (Linea.Count - 1 <= z) { z = Linea.Count - 1; } punto = Linea[z]; Point radar = Radar(Depredador[x], Puntos, punto); if (Principal == true) { DibujarRadar(punto, Animacion); if (radar.X == 0 && radar.Y == 0) { Pen Direccion = new Pen(Color.DarkRed, 8); DibujarDireccionOptima(Animacion, punto, Linea[Linea.Count - 1], Direccion); } else { ListaRadar.Items.Add(radar); ListaRadar.Update(); Arista arista = EncontrarCamino(radar); int Origen = Math.Abs(radar.X - arista.getVertexOrigen().getX()) + Math.Abs(radar.Y - arista.getVertexOrigen().getY()); int Destino = Math.Abs(radar.X - arista.getVertex().getX()) + Math.Abs(radar.Y - arista.getVertex().getY()); Pen DireccionPresa = new Pen(Color.DeepSkyBlue, 8); if (Origen < Destino) { DibujarDireccionOptima(Animacion, radar, new Point(arista.getVertexOrigen().getX(), arista.getVertexOrigen().getY()), DireccionPresa); } else { DibujarDireccionOptima(Animacion, radar, new Point(arista.getVertex().getX(), arista.getVertex().getY()), DireccionPresa); } Pen Direccion = new Pen(Color.DarkRed, 8); DibujarDireccionOptima(Animacion, punto, radar, Direccion); } } Cazado(Puntos, punto, Depredador[x]); DibujarCirculo(punto, BrochaDepredador, Animacion); DibujarId(punto, BrochaID, Animacion, Depredador[x].getId()); Depredador[x].SetcontPunto(Depredador[x].GetcontPuntos() + 1); if (Depredador[x].GetcontPuntos() >= Linea.Count) { Depredador[x].SetcontPunto(0); Depredador[x].SetVerticeActual(Depredador[x].getCaminoActual()[y + 1]); Depredador[x].SetcontAristas(Depredador[x].GetcontAristas() + 1); if (radar.X == 0 && radar.Y == 0 && Depredador[x].GetPathDFS() == false) { List <Vertice> Visitados = new List <Vertice>(); Stack <Vertice> Pila = new Stack <Vertice>(); List <Vertice> CaminoNuevo = new List <Vertice>(); Pila.Push(Depredador[x].GetVerticeActual()); Visitados.Add(Depredador[x].GetVerticeActual()); CaminoNuevo.Add(Depredador[x].GetVerticeActual()); DFS(Pila, Visitados, CaminoNuevo); Depredador[x].SetCaminoActual(CaminoNuevo); Depredador[x].SetVerticeActual(CaminoNuevo[0]); Depredador[x].SetcontAristas(0); Depredador[x].SetPathDFS(true); } if (radar.X != 0 && radar.Y != 0) { Arista arista = EncontrarCamino(radar); if (arista.getVertexOrigen().Equals(Depredador[x].GetVerticeActual())) { Dijkstra D = new Dijkstra(G, Depredador[x].GetVerticeActual(), arista.getVertex()); D.GetCamino().Reverse(); Depredador[x].SetCaminoActual(D.GetCamino()); Depredador[x].SetVerticeActual(D.GetCamino()[0]); } else { Dijkstra D = new Dijkstra(G, Depredador[x].GetVerticeActual(), arista.getVertexOrigen()); D.GetCamino().Reverse(); Depredador[x].SetCaminoActual(D.GetCamino()); Depredador[x].SetVerticeActual(D.GetCamino()[0]); } Depredador[x].SetcontAristas(0); Depredador[x].SetPathDFS(false); } } if (Depredador[x].GetcontAristas() == Depredador[x].getCaminoActual().Count - 1) { Depredador[x].SetcontAristas(0); } x++; if (x >= Depredador.Count) { x = 0; pictureBox1.Refresh(); LimpiarBitmap(Animacion, Color.Transparent); break; } } Puntos.Clear(); } } this.BtnAnimacion.Enabled = false; }
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(); } } }