private void panel1_MouseUp(object sender, MouseEventArgs e) { switch (var_control) { case 1: // Dibujando arco if ((NodoDestino = grafo.DetectarPunto(e.Location)) != null && NodoOrigen != NodoDestino) { ventanaArista.Visible = false; ventanaArista.control = false; ventanaArista.ShowDialog(); if (ventanaArista.control) { if (grafo.AgregarArco(NodoOrigen, NodoDestino, ventanaArista.valor)) //Se procede a crear la arista { int distancia = ventanaArista.valor; NodoOrigen.ListaAdyacencia.Find(v => v.nDestino == NodoDestino).peso = distancia; } nuevaArissta = true; } } var_control = 0; NodoOrigen = null; NodoDestino = null; panel1.Refresh(); break; } }
public CArista(CVertice destino, int peso) { this.nDestino = destino; this.peso = peso; this.grosor_flecha = 1; this.color = Color.Black; }
//=====================Operaciones Básicas================================= //Construye un nodo a partir de su valor y lo agrega a la lista de nodos public CVertice AgregarVertice(string valor) { CVertice nodo = new CVertice(valor); nodos.Add(nodo); return(nodo); }
// Crea la arista a partir de los nodos de origen y de destino public bool AgregarArco(CVertice origen, CVertice nDestino, int peso = 1) { if (origen.ListaAdyacencia.Find(v => v.nDestino == nDestino) == null) { origen.ListaAdyacencia.Add(new CArista(nDestino, peso)); return(true); } return(false); }
public CLista(CLista pLista) { if (pLista != null) { aElemento = pLista.aElemento; aSubLista = pLista.aSubLista; aPeso = pLista.aPeso; } }
public bool ExisteElemento(CVertice pElemento) { if ((aElemento != null) && (pElemento != null)) { return(aElemento.Equals(pElemento) || (aSubLista.ExisteElemento(pElemento))); } else { return(false); } }
public Form1() { InitializeComponent(); grafo = new CGrafo(); nuevoNodo = null; var_control = 0; ventanaVertice = new NuevoVértice(); nodosRuta = new List <CVertice>(); nodosOrdenados = new List <CVertice>(); ventanaArista = new NuevaArista(); this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true); }
private void panel1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == System.Windows.Forms.MouseButtons.Left) // Si se ha presionado el botón // izquierdo del mouse { if ((NodoOrigen = grafo.DetectarPunto(e.Location)) != null) { var_control = 1; // recordemos que es usado para indicar el estado en la panel1: // 0 -> sin accion, 1 -> Dibujando arco, 2 -> Nuevo vértice } if (nuevoNodo != null && NodoOrigen == null) { ventanaVertice.Visible = false; ventanaVertice.control = false; ventanaVertice.ShowDialog(); if (ventanaVertice.control) { if (grafo.BuscarVertice(ventanaVertice.valor) == null) { grafo.AgregarVertice(nuevoNodo); nuevoNodo.Valor = ventanaVertice.valor; } else { MessageBox.Show("Nodo " + ventanaVertice.valor + " ya existente", "YA EXISTE", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } nuevoNodo = null; nuevoVertice = true; var_control = 0; panel1.Refresh(); } } if (e.Button == System.Windows.Forms.MouseButtons.Right) // Si se ha presionado el botón // derecho del mouse { if (var_control == 0) { if ((NodoOrigen = grafo.DetectarPunto(e.Location)) != null) { nuevoVérticeToolStripMenuItem.Text = "Nodo " + NodoOrigen.Valor; } else { panel1.ContextMenuStrip = this.contextMenuStrip1; } } } }
public void Eliminar(CVertice pElemento) { if (aElemento != null) { if (aElemento.Equals(pElemento)) { aElemento = aSubLista.aElemento; aSubLista = aSubLista.SubLista; } else { aSubLista.Eliminar(pElemento); } } }
private void recorridoAnchura(CVertice vertice, Graphics g, string destino) { vertice.Visitado = true; cola.Enqueue(vertice); vertice.colorear(g); Thread.Sleep(1000); vertice.DibujarVertice(g); if (vertice.Valor == destino) { nodoEncontrado = true; return; } while (cola.Count > 0) { CVertice aux = (CVertice)cola.Dequeue(); foreach (CArista adya in aux.ListaAdyacencia) { if (!adya.nDestino.Visitado) { if (!nodoEncontrado) { adya.nDestino.Visitado = true; adya.nDestino.colorear(g); /************** **************/ listBox1.Items.Add(adya.nDestino.Valor); Thread.Sleep(1000); adya.nDestino.DibujarVertice(g); adya.nDestino.DibujarArco(g); if (destino != "") { distancia += adya.peso; } cola.Enqueue(adya.nDestino); if (adya.nDestino.Valor == destino) { nodoEncontrado = true; return; } } } } } }
private void recorridoProfundidad(CVertice vertice, Graphics g) { vertice.Visitado = true; vertice.colorear(g); Thread.Sleep(1000); vertice.DibujarVertice(g); foreach (CArista adya in vertice.ListaAdyacencia) { if (!adya.nDestino.Visitado) { /************** **************/ listBox1.Items.Add(adya.nDestino.Valor); recorridoProfundidad(adya.nDestino, g); } } }
public int PosicionElemento(CVertice pElemento) { if ((aElemento != null) || (ExisteElemento(pElemento))) { if (aElemento.Equals(pElemento)) { return(1); } else { return(1 + aSubLista.PosicionElemento(pElemento)); } } else { return(0); } }
public void Agregar(CVertice pElemento, int pPeso) { if (pElemento != null) { if (aElemento == null) { aElemento = new CVertice(pElemento.Valor); aPeso = pPeso; aSubLista = new CLista(); } else { if (!ExisteElemento(pElemento)) { aSubLista.Agregar(pElemento, pPeso); } } } }
// Constructores public CLista() { aElemento = null; aSubLista = null; aPeso = 0; }
public CLista(CVertice pElemento, CLista pSubLista, int pPeso) { aElemento = pElemento; aSubLista = pSubLista; aPeso = pPeso; }
private void nuevoVérticeToolStripMenuItem_Click(object sender, EventArgs e) { nuevoNodo = new CVertice(); var_control = 2; // recordemos que es usado para indicar el estado en la pizarra: 0 -> // sin accion, 1 -> Dibujando arco, 2 -> Nuevo vértice }
// Métodos public CArista(CVertice destino) : this(destino, 1) { this.nDestino = destino; }
//Agrega un nodo a la lista de nodos del grafo public void AgregarVertice(CVertice nuevonodo) { nodos.Add(nuevonodo); }