GRAFOS EN C#
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace appDibujar
{//---------------------------PARA CREAR EL PRIMER PARAMETRO ES EL ARRAYLIST LR, EL SEGUNDO EL NUMERO DE Is
//-------------------------Y EL TERCERO ES EL ARRAYLIST DE REFERENCIA
public partial class Form1 : Form
{
//ArrayList Pricipal
public ArrayList ListaConjuntosLR;
public ArrayList IDibujo;
//ArrayList de referencia
public ArrayList Referencia;
//Generales
public double mouseX;
public double mouseY;
public int nroIs;
public int centroX;
public int centroY;
int radio = 300;
int[] coorXs = new int[10];
int[] coorYs = new int[10];
int[] ptoInicial = new int[2];
int[] ptoFinal = new int[2];
Font letras = new Font("Comic Sans MS", 10, FontStyle.Bold);
Brush Col = Brushes.Green;
Brush ColorLetras = Brushes.Yellow;
Pen LapizNodo = new Pen(Color.Green, 2);
public Form1(ArrayList pListaConjuntoLR,int pnroIs,ArrayList pReferencia)
{
InitializeComponent();
ListaConjuntosLR = pListaConjuntoLR;
nroIs = pnroIs;
Referencia = pReferencia;
IDibujo = new ArrayList(nroIs);
CargarListaLR();
centroX = paDibujo.Width / 2;
centroY = paDibujo.Height / 2;
coorXs = new int[nroIs];
coorYs = new int[nroIs];
mouseX = 0;
mouseY = 0;
}
private double polarX(int n)
{
double x = radio * Math.Cos(2 * Math.PI / nroIs*n);
return x;
}
private double polarY(int n)
{
double y = radio * Math.Sin(2 * Math.PI / nroIs*n);
return y;
}
private void CargarListaLR()
{
ArrayList t = new ArrayList();
for (int i = 0; i < ListaConjuntosLR.Count; i++)
{
t = ListaConjuntosLR[i] as ArrayList;
string cadena = "{";
for (int j = 0; j < t.Count; j++)
{
cadena = cadena+","+(t[j].ToString());
}
cadena = cadena + "}";
IDibujo.Add(cadena);
}
}
private void DibujarGrafo(PaintEventArgs e)
{
//dibujamos vertices
for (int i = 0; i < nroIs; i++)
{
int x = (int)polarX(i);
int y = (int)polarY(i);
coorXs[i] = centroX + x;
coorYs[i] = centroY - y;
e.Graphics.FillEllipse(Col,centroX+x, centroY-y, 20, 20);
e.Graphics.DrawEllipse(LapizNodo, centroX+x, centroY-y, 20, 20);
e.Graphics.DrawString("I"+i, letras, ColorLetras, (centroX - 30)+x,centroY-y);
}
}
private void dibujarLinea(PaintEventArgs e)
{
Pen LapizAristas = new Pen(Color.FromArgb(255, 0, 0, 255), 8);
//LapizAristas.EndCap = System.Drawing.Drawing2D.LineCap.DiamondAnchor;
LapizAristas.StartCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
LapizAristas.EndCap =System.Drawing.Drawing2D.LineCap.RoundAnchor;
LapizAristas.Width = 10;
//Variables temporales
ArrayList temp=new ArrayList();
int origen = 0;
string arco = "";
int destino = 0;
for (int i = 0; i < Referencia.Count; i++)
{
temp = Referencia[i] as ArrayList;
origen = int.Parse(temp[0].ToString());
arco = temp[1].ToString();
destino = int.Parse(temp[2].ToString());
e.Graphics.DrawLine(LapizAristas, coorXs[destino], coorYs[destino], coorXs[origen], coorYs[origen]);
e.Graphics.DrawString(arco, letras, ColorLetras, (coorXs[destino] + 30), coorYs[destino] - 10);
}
}
private void paDibujo_Paint(object sender, PaintEventArgs e)
{
DibujarGrafo(e);
dibujarLinea(e);
}
private void paDibujo_MouseMove(object sender, MouseEventArgs e)
{
mouseX = e.X;
mouseY = e.Y;
}
private void paDibujo_Click(object sender, EventArgs e)
{
for (int i = 0; i < nroIs; i++)
{
if ((mouseX<coorxs i="i" mousex="mousex">coorXs[i]-20))
{
if ((mouseY < coorXs[i] + 20) && (mouseY > coorYs[i] - 20))
{
string mostrar = IDibujo[i].ToString();
MessageBox.Show("Elementos de " + "I"+i+":"+mostrar);
}
}
}
}
}
}
HOLA QUE TAL? UN MEGA FAVOR ME PODRÍAS PASAR UNA CAPTURA DE PANTALLA DEL PROGRAMA!! ASI PUEDO SABER QUE BOTONES LLEVA POR FAVOR ES PARA UN EXAMEN! ;) TE LO AGRADECERIA MUCHISIMO!!
ResponderEliminar