예제 #1
0
        private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            switch (e.ClickedItem.Name)
            {
            case "Agrega":
            case "AgregaNodo":
                pm = new Point();

                accion = 1;
                grafo.desseleccionar();
                break;

            case "Mueve":
            case "MueveNodo":
                band   = true;
                accion = 2;
                grafo.desseleccionar();
                break;

            case "EliminaNodo":
            case "EliminaNod":
                accion = 4;
                grafo.desseleccionar();
                break;

            case "MueveGrafo":
            case "MueveGraf":
                accion = 5;
                grafo.desseleccionar();
                break;

            case "EliminaArista":
                //      accion = 6;
                //     grafo.desseleccionar();

                break;

            case "PropiedadesGrafo":
                Form f;
                if (AristaDirigida.Enabled == false)
                {
                    f = new Form3(grafo, 2);
                }
                else
                {
                    f = new Form3(grafo, 1);
                }
                f.Activate();
                f.Show();
                grafo.desseleccionar();
                break;

            case "intercambio":
            case "intercambioTool":
                int  cont = 0;
                char name = 'A';
                bool num;
                int  aux;
                if ((int.TryParse(grafo[0].NOMBRE.ToString(), out aux)) == true)
                {
                    num = true;
                }
                else
                {
                    num = false;
                }
                if (num == true)
                {
                    foreach (NodoP cambio in grafo)
                    {
                        cambio.NOMBRE = name.ToString();
                        name++;
                    }
                    nombre = 'A';
                    for (int i = 0; i < grafo.Count; i++)
                    {
                        nombre++;
                    }
                }
                else
                {
                    numero = grafo.Count;
                    foreach (NodoP cambio in grafo)
                    {
                        cambio.NOMBRE = cont.ToString();
                        cont++;
                    }
                }
                break;

            case "cierraEuler":
                time.Stop();

                grafo.desColorea(resp);
                if (g2.Count > 1)
                {
                    grafo = new Grafo(g2);
                    g2    = new Grafo();
                }
                if (gis != null)
                {
                    gis = null;
                }
                grafo.bosque = false;
                foreach (Arista qt in grafo.Aristas)
                {
                    qt.bosqueT = 0;
                }
                band = false;
                grafo.pinta(g);

                orig   = null;
                destin = null;

                pm     = new Point();
                accion = 0;
                break;

            case "Dirigida":
                accion = 3;

                band = true;

                AgregaRelacion.Checked = true;

                fl.EndCap   = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
                fl.StartCap = LineCap.RoundAnchor;
                fl.Width    = 4;
                tipoarista  = 1;
                grafo.desseleccionar();


                break;

            case "NoDirigida":
                accion = 3;
                band   = true;
                AgregaRelacion.Checked = true;
                fl.EndCap   = System.Drawing.Drawing2D.LineCap.NoAnchor;
                fl.StartCap = LineCap.NoAnchor;
                fl.Width    = 4;
                tipoarista  = 2;
                grafo.desseleccionar();
                break;
            }
        }
예제 #2
0
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics au;

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

            if (bandComplemento == 1)
            {
                Grafo aux = new Grafo();
                aux             = grafo;
                grafo           = new Grafo(aux);
                bandComplemento = 0;
            }

            if (band)
            {
                //au.Clear(BackColor);
                switch (accion)
                {
                case 1:
                    bool num;
                    int  iaux;
                    if (grafo.Count > 0)
                    {
                        if ((int.TryParse(grafo[0].NOMBRE.ToString(), out iaux)) == true)
                        {
                            num = true;
                        }
                        else
                        {
                            num = false;
                        }
                    }
                    else
                    {
                        num = false;
                    }
                    if (num == false)
                    {
                        nu = new NodoP(pm, nombre);
                        nombre++;
                    }
                    else
                    {
                        nu        = new NodoP(pm, nombre);
                        nu.NOMBRE = numero.ToString();
                        numero++;
                    }
                    if (grafo.Count == 0)
                    {
                        //           Dirigida.Enabled = NoDirigida.Enabled = true;
                    }
                    if (grafo.Count > 1)
                    {
                        nu.COLOR = grafo[0].COLOR;
                    }
                    grafo.AgregaNodo(nu);
                    AgregaRelacion.Enabled = true;
                    gactivo = true;
                    nu      = null;
                    break;

                case 2:
                    nu = (NodoP)grafo.Find(delegate(NodoP a) { if (pm.X > a.POS.X - 15 && pm.X < a.POS.X + 30 && pm.Y > a.POS.Y - 15 && pm.Y < a.POS.Y + 30)
                                                               {
                                                                   return(true);
                                                               }
                                                               else
                                                               {
                                                                   return(false);
                                                               } });
                    break;

                case 3:
                    nu = (NodoP)grafo.Find(delegate(NodoP a) { if (pm.X > a.POS.X - 15 && pm.X < a.POS.X + 30 && pm.Y > a.POS.Y - 15 && pm.Y < a.POS.Y + 30)
                                                               {
                                                                   return(true);
                                                               }
                                                               else
                                                               {
                                                                   return(false);
                                                               } });
                    p = pm;

                    break;

                case 5:
                    Grafo aux = new Grafo();
                    aux = grafo;
                    aux.Sort(delegate(NodoP a, NodoP b) { return(a.POS.X.CompareTo(b.POS.X)); });
                    if (pm.X > aux.ToArray()[0].POS.X && pm.X < aux.ToArray()[aux.Count - 1].POS.X)
                    {
                        aux.Sort(delegate(NodoP a, NodoP b) { return(a.POS.Y.CompareTo(b.POS.Y)); });
                        if (pm.Y > aux.ToArray()[0].POS.Y && pm.Y < aux.ToArray()[aux.Count - 1].POS.Y)
                        {
                            mov = true;
                        }
                        else
                        {
                            mov = false;
                        }
                    }
                    else
                    {
                        mov = false;
                    }
                    break;

                case 6:
                    int    total = grafo.Aristas.Count;
                    Arista arista;
                    for (int i = 0; i < total; i++)
                    {
                        arista = grafo.Aristas[i];
                        if (arista.toca(pm))
                        {
                            grafo.RemueveArista(arista);

                            break;
                        }
                    }
                    break;

                case 14:
                    Arista ari;
                    NodoP  o, d;
                    o = d = null;
                    if (bandcam == true)
                    {
                        ari   = new Arista();;
                        grafo = new Grafo(g2);
                        grafo.Aristas.Clear();
                        foreach (NodoP rel in grafo)
                        {
                            rel.relaciones.Clear();
                        }
                        bandcam = false;
                        au.Clear(BackColor);
                    }

                    if (tck == true)
                    {
                        accion = 0;
                        tck    = false;
                        if (icam > 0)
                        {
                            grafo.Find(delegate(NodoP dx) { if (dx.NOMBRE == CCE[icam].NOMBRE)
                                                            {
                                                                return(true);
                                                            }
                                                            else
                                                            {
                                                                return(false);
                                                            } }).insertaRelacion(grafo.Find(delegate(NodoP ox) { if (ox.NOMBRE == CCE[icam - 1].NOMBRE)
                                                                                                                 {
                                                                                                                     return(true);
                                                                                                                 }
                                                                                                                 else
                                                                                                                 {
                                                                                                                     return(false);
                                                                                                                 } }), grafo.Aristas.Count);
                            d = grafo.Find(delegate(NodoP dx) { if (dx.NOMBRE == CCE[icam].NOMBRE)
                                                                {
                                                                    return(true);
                                                                }
                                                                else
                                                                {
                                                                    return(false);
                                                                } });
                            o = grafo.Find(delegate(NodoP ox) { if (ox.NOMBRE == CCE[icam - 1].NOMBRE)
                                                                {
                                                                    return(true);
                                                                }
                                                                else
                                                                {
                                                                    return(false);
                                                                } });
                            d.COLOR = Color.Blue;
                            o.COLOR = Color.Blue;
                            Pen penn = new Pen(Brushes.Red);
                            penn.Width = 4;
                            g.DrawEllipse(penn, new Rectangle(d.POS.X - 16, d.POS.Y - 16, 30, 30));

                            ari = new Arista(1, d, o, "e" + (CCE.Count - icam).ToString());

                            grafo.AgregaArista(ari);
                        }
                        grafo.pinta(g);
                    }
                    break;
                }
                if (gis != null)
                {
                    gis.pinta(au);
                    g.DrawImage(bmp1, 0, 0);
                }
                grafo.pinta(au);
                g.DrawImage(bmp1, 0, 0);
            }
            else
            {
                switch (accion)
                {
                case 1:
                    break;

                case 2:
                    if (nu != null)
                    {
                        nu.POS = pm;
                        au.Clear(BackColor);
                    }
                    break;

                case 3:
                    au.Clear(BackColor);
                    if (nu != null)
                    {
                        au.DrawLine(fl, p, pm);
                    }

                    break;

                case 5:
                    if (mov)
                    {
                        Point po = new Point(pm.X - p.X, pm.Y - p.Y);
                        foreach (NodoP n in grafo)
                        {
                            Point nue = new Point(n.POS.X + po.X, n.POS.Y + po.Y);
                            n.POS = nue;
                        }

                        p = pm;
                        au.Clear(BackColor);
                    }

                    break;
                }
                if (gis != null)
                {
                    gis.pinta(au);
                    g.DrawImage(bmp1, 0, 0);
                }
                grafo.pinta(au);
                g.DrawImage(bmp1, 0, 0);
            }
        }
예제 #3
0
        private void nuevoToolStripMenuItem_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            nuevoToolStripMenuItem.HideDropDown();
            switch (e.ClickedItem.Name)
            {
            case "Nuevo":
                band      = false;
                mov       = false;
                BackColor = Color.White;
                g.Clear(BackColor);
                AristaNoDirigida.Enabled = true;
                AristaDirigida.Enabled   = true;
                g2      = new Grafo();
                grafo   = new Grafo();
                nombre  = 'A';
                gactivo = false;
                break;

            case "Abrir":

                OpenFileDialog filed = new OpenFileDialog();
                filed.InitialDirectory = Application.StartupPath + "\\Ejemplos";
                filed.DefaultExt       = ".grafo";
                string namefile;
                filed.Filter = "Grafo Files (*.grafo)|*.grafo|All files (*.*)|*.*";
                if (filed.ShowDialog() == DialogResult.OK)
                {
                    namefile = filed.FileName;

                    try
                    {
                        using (Stream stream = File.Open(namefile, FileMode.Open))
                        {
                            BinaryFormatter bin = new BinaryFormatter();
                            grafo = (Grafo)bin.Deserialize(stream);
                            grafo.pinta(g);
                        }
                    }
                    catch (IOException exe)
                    {
                        MessageBox.Show(exe.ToString());
                    }
                    g2 = new Grafo();


                    accion = 1;
                    grafo.desseleccionar();
                    nombre = 'A';
                    for (int nn = 0; nn < grafo.Count; nn++)
                    {
                        nombre++;
                    }
                }
                break;

            case "Guardar":
                SaveFileDialog sav = new SaveFileDialog();
                sav.Filter           = "Grafo Files (*.grafo)|*.grafo|All files (*.*)|*.*";
                sav.InitialDirectory = Application.StartupPath + "\\ProyectosGrafo";
                String nombr;
                if (sav.ShowDialog() == DialogResult.OK)
                {
                    nombr = sav.FileName;
                    try
                    {
                        using (Stream stream = File.Open(nombr, FileMode.Create))
                        {
                            BinaryFormatter bin = new BinaryFormatter();
                            bin.Serialize(stream, grafo);
                        }
                    }
                    catch (IOException exe)
                    {
                        MessageBox.Show(exe.ToString());
                    }
                }
                break;

            case "BorraGrafo":
                grafo = new Grafo();
                g.Clear(BackColor);
                g2     = new Grafo();
                nombre = 'A';
                break;

            case "Salir":
                System.Windows.Forms.Application.Exit();
                break;
            }
        }