Exemple #1
0
        private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                //Agregar Grafo
                if (bandera_agrega_grafo == true)
                {
                    Grafo g = new Grafo(e.X, e.Y, list_grafo.Count);
                    list_grafo.Add(g);
                    bandera_agrega_grafo = false;
                }
            }
            else if (e.Button == MouseButtons.Right) //Agregar arista con click Derecho
            {
                double  dist = 0;
                Vertice aux;

                if (bandera_agrega_arista_segundo == true) //Segundo click para agregar aristas
                {
                    bandera_agrega_arista_segundo = false;

                    if (nuevo_nodo != null)
                    {
                        dist = Math.Sqrt(Math.Pow(e.X - nuevo_nodo.x, 2) + Math.Pow(e.Y - nuevo_nodo.y, 2));
                        if (dist <= (nuevo_nodo.ANCHO / 2))
                        {
                            auxv2 = nuevo_nodo;
                        }
                    }

                    foreach (Grafo item in list_grafo)
                    {
                        aux = item.buscaNodo(e.X, e.Y);
                        if (aux != null)
                        {
                            auxv2 = aux;
                            break;
                        }
                    }

                    Arista nueva_arista = null;
                    if (auxv1 != null)
                    {
                        if (this.auxv1.indice_grafo == this.auxv2.indice_grafo || this.auxv1.indice_grafo == -1 || this.auxv2.indice_grafo == -1)
                        {
                            nueva_arista = new Arista(this.auxv1, this.auxv2, list_grafo);
                            foreach (Grafo item in list_grafo)
                            {
                                if (item.indice_grafo == auxv1.indice_grafo || item.indice_grafo == auxv2.indice_grafo)
                                {
                                    if (-1 == auxv1.indice_grafo)
                                    {
                                        nueva_arista.origen.indice_grafo = auxv2.indice_grafo;
                                        auxv1.indice_vertice             = item.list_vertice.Count();
                                        item.list_vertice.Add(this.auxv1);
                                    }
                                    else if (-1 == auxv2.indice_grafo)
                                    {
                                        nueva_arista.destino.indice_grafo = auxv1.indice_grafo;
                                        auxv2.indice_vertice = item.list_vertice.Count();
                                        item.list_vertice.Add(this.auxv2);
                                    }
                                    nueva_arista.indice_arista = item.list_arista.Count();
                                    nueva_arista.indice_grafo  = item.indice_grafo;
                                    item.list_arista.Add(nueva_arista);
                                    break;
                                }
                            }
                            this.auxv1 = null;
                            this.auxv2 = null;
                        }
                    }
                }
                else //Primer click derecho
                {
                    bandera_agrega_arista_segundo = true;
                    if (nuevo_nodo != null)
                    {
                        dist = Math.Sqrt(Math.Pow(e.X - nuevo_nodo.x, 2) + Math.Pow(e.Y - nuevo_nodo.y, 2));
                        if (dist <= (nuevo_nodo.ANCHO / 2))
                        {
                            auxv1 = nuevo_nodo;
                        }
                    }

                    foreach (Grafo item in list_grafo)
                    {
                        aux = item.buscaNodo(e.X, e.Y);
                        if (aux != null)
                        {
                            auxv1 = aux;
                            break;
                        }
                    }
                }
            }
            pictureBox1.Invalidate();
        }
Exemple #2
0
        public override Grafo complemento(Graphics g)
        {
            NodoP  nn;
            Arista nnr;

            Grafo nuevo = new Grafo();

            foreach (NodoP np in this)
            {
                nn = new NodoP(np.nombre, np.centro);
                nuevo.Add(nn);
            }
            nuevo = new GrafoNoDirigido(nuevo);

            foreach (NodoP aux1 in this)                // Ciclo que recorre los nodos del grafo
            {
                if (aux1.aristas.Count == 0)            // si el nodo no tiene aristas
                {
                    foreach (NodoP aux2 in nuevo)       // Ciclo que recorre los nodos del grafo "copia"
                    {
                        if (aux1.nombre != aux2.nombre) // Condición para que el nodo no apunte a si mismo
                        {
                            nnr         = new Arista(0);
                            nnr.origen  = nuevo.Find(x => x.nombre.Equals(aux1.nombre));
                            nnr.destino = aux2;

                            nuevo.Find(x => x.nombre.Equals(aux1.nombre)).aristas.Add(nnr);
                        }
                    }
                }

                else // Si el nodo ya tiene Aristas
                {
                    foreach (NodoP aux2 in nuevo) // Ciclo que recorre los nodos
                    {
                        if (aux1.nombre != aux2.nombre) // Compara que el nodo no se apunte a si mismo
                        {
                            Arista r = new Arista(0);
                            r = aux1.aristas.Find(x => x.destino.nombre.Equals(aux2.nombre));
                            if (r == null)
                            {
                                nnr         = new Arista(0);
                                nnr.origen  = nuevo.Find(x => x.nombre.Equals(aux1.nombre));
                                nnr.destino = aux2;
                                if (nnr != null)
                                {
                                    nuevo.Find(x => x.nombre.Equals(aux1.nombre)).aristas.Add(nnr);
                                }
                            }
                            else
                            {
                                continue;
                            }
                        }
                    }
                }
            }

            base.complemento(g);

            return(nuevo);
        }
Exemple #3
0
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics ga = CreateGraphics();

            ga = Graphics.FromImage(bmp1);
            ga.Clear(BackColor);


            if (bandF || band)
            {
                switch (opcion)
                {
                case 1:
                    ga.FillEllipse(grafo.brushN, p1.X - grafo.radio, p1.Y - grafo.radio, grafo.radio * 2, grafo.radio * 2);
                    ga.DrawEllipse(grafo.penN, p1.X - grafo.radio + (grafo.penN.Width / 2), p1.Y - grafo.radio + (grafo.penN.Width / 2), grafo.radio * 2 - (grafo.penN.Width / 2), grafo.radio * 2 - (grafo.penN.Width / 2));
                    if (grafo.numN >= 28 || grafo.edoNom)
                    {
                        ga.DrawString(nodoP.nombre.ToString(), grafo.font, grafo.brushF, p1.X - 6, p1.Y - 6);
                    }
                    else
                    {
                        ga.DrawString(((char)(nodoP.nombre + 64)).ToString(), grafo.font, grafo.brushF, p1.X - 6, p1.Y - 6);
                    }
                    break;

                case 2:
                case 9:
                    if (bandF)
                    {
                        if (nodoP.Equals(nodoAux))
                        {
                            ga.DrawBezier(grafo.penA, nodoP.centro.X - 15, nodoP.centro.Y - 15, nodoP.centro.X - 20, nodoP.centro.Y - 60, nodoP.centro.X + 20, nodoP.centro.Y - 60, nodoP.centro.X + 15, nodoP.centro.Y - 15);
                        }
                        else
                        {
                            ga.DrawLine(grafo.penA, grafo.BuscaInterseccion(nodoP.centro, nodoAux.centro), grafo.BuscaInterseccion(nodoAux.centro, nodoP.centro));
                        }
                    }
                    if (band)
                    {
                        ga.DrawLine(grafo.penA, grafo.BuscaInterseccion(nodoP.centro, p2), p2);
                    }
                    break;
                }
                bandF = false;
            }

            if (bandI)
            {
                ga.Clear(BackColor);
                grafo.ImprimirGrafo(ga, bpar);
                bandI = false;
            }

            if (opcion == 6 || opcion == 7)
            {
                ga.Clear(BackColor);
                grafo.Clear();
                grafo.numN   = 1;
                grafo.edoNom = false;
                if (opcion == 7)
                {
                    grafo = new Grafo();
                    MatrizInfinita.Enabled = false;
                    CambiaBotones(false);
                }
            }
            if (opcion != 6)
            {
                if (opcion != 7)
                {
                    grafo.ImprimirGrafo(ga, bpar);
                }
            }
            g.DrawImage(bmp1, 0, 0);
        }
Exemple #4
0
        private void metodosAdicionales(object sender, ToolStripItemClickedEventArgs e)
        {
            numericUpDown1.Visible = false;
            numericUpDown2.Visible = false;
            numericUpDown3.Visible = false;
            int  i     = 0;
            int  j     = 0;
            bool vacio = false;

            bool   band = false;
            Random rand = new Random();

            switch (e.ClickedItem.AccessibleName)
            {
            case "complemento":
                bpar = false;
                quitaPesos();
                quitaNumeric();
                obtenPropiedades();
                if (AristaNoDirigida.Enabled == true && AristaDirigida.Enabled == true)
                {
                    grafo      = new GrafoNoDirigido(grafo);
                    grafo.tipo = 2;
                    grafo      = grafo.complemento(g);
                    asignaPropiedades();
                    AristaDirigida.Enabled = false;
                }
                else
                if (AristaDirigida.Enabled == true && AristaNoDirigida.Enabled == false)
                {
                    grafo = grafo.complemento(g);
                    asignaPropiedades();
                    grafo.penA.CustomEndCap = arrow;
                }
                else if (AristaDirigida.Enabled == false && AristaNoDirigida.Enabled == true)
                {
                    grafo = grafo.complemento(g);
                    asignaPropiedades();
                }

                grafo.numN = grafo.Count;
                //grafo.ImprimirGrafo(g);
                Form1_Paint(this, null);
                break;

            case "preExamen_1":
                if (AristaNoDirigida.Enabled == true && AristaDirigida.Enabled == false)
                {
                    PreExamen pre = new PreExamen(grafo);
                    pre.Show();
                }
                else
                {
                    MessageBox.Show("Tiene que ser grafo no  dirigido");
                }

                break;

            case "nPartita":
                bpar = true;

                List <List <int> > partita = new List <List <int> >();
                List <Color>       color   = coloreate();
                partita = grafo.nPartita(g);
                //grafo.ImprimirGrafo(g, bpar);
                nPartita nPartita = new nPartita(partita, grafo);

                i = rand.Next(0, color.Count);
                foreach (List <int> aux1 in partita)
                {
                    foreach (int aux2 in aux1)
                    {
                        grafo.Find(x => x.nombre.Equals(aux2)).colorN = new SolidBrush(color[i]);
                        j = i;
                    }
                    if (i < color.Count - 1)
                    {
                        i++;
                    }
                    else
                    {
                        i = 0;
                    }
                }
                // grafo.ImprimirGrafo(g, bpar);
                // Form1_Paint(this, null);
                //  grafo.ImprimirGrafo(g, bpar);
                nPartita.Show();
                break;

            case "MatrizInfinita":

                MatrizInfinita matriz = new MatrizInfinita(grafo);
                matriz.Show();
                break;

            case "nodoPendiente":
                List <int> pendientes = new List <int>();
                List <int> cut        = new List <int>();
                grafo      = new GrafoNoDirigido(grafo);
                grafo.tipo = 2;
                bpar       = true;
                pendientes = grafo.nodoPendiente();
                if (pendientes.Count == 0)
                {
                    MessageBox.Show("No tiene nodo Pendiente");
                }
                //  cut = grafo.verticeCut();
                foreach (int aux1 in pendientes)
                {
                    grafo.Find(x => x.nombre.Equals(aux1)).colorN = new SolidBrush(Color.Red);
                    grafo.Find(x => x.nombre.Equals(aux1)).aristas[0].destino.colorN            = new SolidBrush(Color.Green);
                    grafo.Find(x => x.nombre.Equals(aux1)).aristas[0].colorA                    = new Pen(new SolidBrush(Color.Blue));
                    grafo.Find(x => x.nombre.Equals(aux1)).aristas[0].destino.aristas[0].colorA = new Pen(new SolidBrush(Color.Blue));
                }
                foreach (int nodo in cut)
                {
                    grafo.Find(x => x.nombre.Equals(nodo)).colorN = new SolidBrush(Color.Green);
                }
                bpar = true;
                break;

            case "Euleriano":
                Euler();
                break;

            case "warner":
                int warner;
                warner = grafo.warner(g, nodoW);
                if (warner == 1)
                {
                    MessageBox.Show("Tiene un K5");
                }
                else if (warner == 2)
                {
                    MessageBox.Show("Tiene un K3,3");
                }
                else
                {
                    MessageBox.Show("No tiene K5 ni K3,3");
                }
                break;

            case "dijkstra":
                algoritmoDijkstra();
                break;

            case "kruskal":
                iKruskal = 0;
                algoritmoKruskal();
                break;
            }
        }
Exemple #5
0
        private void Configuracion_Clicked(object sender, ToolStripItemClickedEventArgs e)
        {
            switch (e.ClickedItem.AccessibleName)
            {
            case "Preferencias":
                config = new Configuracion(grafo.width, grafo.widthA, grafo.radio, grafo.colorFuente, grafo.cArista, grafo.cRelleno, grafo.cNodo, grafo.font);
                if (config.ShowDialog() == DialogResult.OK)
                {
                    grafo.font   = config.font;
                    grafo.penN   = config.penN;
                    grafo.penA   = config.penA;
                    grafo.brushN = config.brushN;
                    grafo.brushF = config.brushF;
                    grafo.radio  = config.radio;

                    grafo.cNodo    = grafo.penN.Color;
                    grafo.width    = grafo.penN.Width;
                    grafo.cRelleno = grafo.brushN.Color;

                    grafo.cArista = grafo.penA.Color;
                    grafo.widthA  = grafo.penA.Width;

                    grafo.colorFuente = grafo.brushF.Color;
                    grafo.nameF       = grafo.font.Name;
                    grafo.sizeF       = grafo.font.Size;
                    grafo.styleF      = grafo.font.Style;

                    if (AristaDirigida.Enabled && !AristaNoDirigida.Enabled)
                    {
                        grafo             = new GrafoDirigido(grafo);
                        grafo.tipo        = 3;
                        grafo.widthA      = config.anchoArista;
                        grafo.radio       = config.radio;
                        grafo.font        = config.font;
                        grafo.cRelleno    = config.rellenoNodo;
                        grafo.colorFuente = config.colorFuente;
                        grafo.cNodo       = config.ContornoNodo;
                        grafo.width       = config.anchoNodo;
                        grafo.cArista     = config.colorArista;
                        grafo.penN        = new Pen(grafo.cNodo, grafo.width);
                        grafo.brushN      = new SolidBrush(grafo.cRelleno);
                        grafo.brushF      = new SolidBrush(grafo.colorFuente);
                        grafo.font        = new Font(grafo.nameF, grafo.sizeF, grafo.styleF);
                        grafo.penA        = new Pen(grafo.cArista, grafo.widthA);

                        /*
                         * grafo.font = config.font;
                         * grafo.penN = config.penN;
                         * grafo.penA = config.penA;
                         * grafo.brushN = config.brushN;
                         * grafo.brushF = config.brushF;
                         * grafo.radio = config.radio;
                         *
                         * grafo.cNodo = grafo.penN.Color;
                         * grafo.width = grafo.penN.Width;
                         * grafo.cRelleno = grafo.brushN.Color;
                         *
                         * grafo.cArista = grafo.penA.Color;
                         * grafo.widthA = grafo.penA.Width;
                         * grafo.penA.Width = grafo.penA.Width;
                         * grafo.colorFuente = grafo.brushF.Color;
                         * grafo.nameF = grafo.font.Name;
                         * grafo.sizeF = grafo.font.Size;
                         * grafo.styleF = grafo.font.Style;
                         */
                        grafo.penA.CustomEndCap = arrow;
                        //actualizaPropiedades();
                    }



                    bandF = false;
                    band  = false;
                    bandA = false;
                    bandI = true;
                    Form1_Paint(this, null);
                }
                config.Dispose();
                break;

            case "Cambia":
                CambiaNombre();
                break;
            }
        }
Exemple #6
0
        private void Archivo_Click(object sender, ToolStripItemClickedEventArgs e)
        {
            opcion = 0;
            NodoP A    = null;
            bool  band = false;
            int   i    = 0;

            iKruskal             = 0;
            bpar                 = false;
            intercambiaColor     = false;
            agregaPeso.BackColor = Color.Gray;
            quitaPesos();
            quitaNumeric();
            if (bpar == false)
            {
                grafo.ImprimirGrafo(g, bpar);
            }

            IFormatter formatter  = new BinaryFormatter();
            String     directorio = Environment.CurrentDirectory + "..\\Grafos";

            //String directorio = Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, @"..\\Grafos"));



            switch (e.ClickedItem.AccessibleName)
            {
            case "Save":
                saveFileDialog1.InitialDirectory = directorio;
                saveFileDialog1.FileName         = "";
                saveFileDialog1.Filter           = "(*.grafo)|*.grafo";
                if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    Stream stream = new FileStream(saveFileDialog1.FileName, FileMode.Create, FileAccess.Write, FileShare.None);


                    if (grafo.tipo == 1)
                    {
                        formatter.Serialize(stream, (Grafo)grafo);
                    }
                    else if (grafo.tipo == 2)
                    {
                        formatter.Serialize(stream, (GrafoNoDirigido)grafo);
                    }
                    else if (grafo.tipo == 3)
                    {
                        formatter.Serialize(stream, (GrafoDirigido)grafo);
                    }


                    stream.Close();
                }
                break;

            case "Open":
                openFileDialog1.FileName         = "";
                openFileDialog1.Filter           = "(*.grafo)|*.grafo";
                openFileDialog1.InitialDirectory = directorio;
                if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    g.Clear(BackColor);
                    grafo.Clear();
                    AristaNoDirigida.Enabled = true;
                    AristaDirigida.Enabled   = true;
                    Stream stream = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read, FileShare.None);

                    grafo = (Grafo)formatter.Deserialize(stream);

                    if (grafo.tipo == 2)    // GRAFO NO DIRIGIDO
                    {
                        obtenPropiedades();
                        asignaPropiedades();
                        grafo = new GrafoNoDirigido(grafo);
                        AristaNoDirigida.Enabled = true;
                        AristaDirigida.Enabled   = false;
                        euleriano.Enabled        = true;
                        dijkstra.Enabled         = false;
                        kruskal.Enabled          = false;
                        agregaPeso.Enabled       = true;
                        toolStripButton2.Enabled = true;
                    }
                    else if (grafo.tipo == 3)    // GRAFO DIRIGIDO
                    {
                        obtenPropiedades();
                        asignaPropiedades();
                        grafo = new GrafoDirigido(grafo);
                        AristaNoDirigida.Enabled = false;
                        AristaDirigida.Enabled   = true;
                        grafo.penA.CustomEndCap  = arrow;
                        euleriano.Enabled        = false;
                        dijkstra.Enabled         = true;

                        agregaPeso.Enabled       = true;
                        kruskal.Enabled          = true;
                        toolStripButton2.Enabled = true;
                    }

                    stream.Close();



                    band  = false;
                    bandA = false;
                    bandF = false;
                    bandI = false;

                    MueveNodo.Enabled  = true;
                    BorrarNodo.Enabled = true;
                    // AristaNoDirigida.Enabled = true;
                    //  AristaDirigida.Enabled = true;
                    BorrarArista.Enabled  = true;
                    MueveGrafo.Enabled    = true;
                    BorrarGrafo.Enabled   = true;
                    EliminarGrafo.Enabled = true;
                    Cambia.Enabled        = true;
                    Invalidate();
                }
                break;

            case "Imprimir":
                opcion = 10;
                band   = false;
                bandF  = false;
                bandA  = false;
                bandI  = true;
                Invalidate();
                break;
            }
        }