You are on page 1of 4

Crculo, Cono y Polgono, creado con lneas en C# .

NET En este programa vamos a crear una serie de figuras siguiendo formulas matemticas para crear la ilusin de un objeto, que en este caso sern 3, crearemos un circulo con lneas, un cono y un polgono. Usaremos:

Timer Variables Graphics Generacin de lneas con DrawLine Botones 1 PictureBox 1 Panel Y el uso de funciones matemticas.

Veamos primero la dimensin de nuestro proyecto y los componentes que usaremos :

Ahora vamos al cdigo: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Threading; namespace Efectos_con_Lineas { public partial class Form1 : Form { int LENGHT = 100; int boton; public Form1() { InitializeComponent();

} private void button1_Click(object sender, EventArgs e) { Refresh(); timer1.Enabled = true; boton = 1; timer2.Enabled = false; timer3.Enabled = false; } private void timer1_Tick(object sender, EventArgs e) { int CENTRO_X = 150; int CENTRO_Y = 150; Graphics g = pictureBox1.CreateGraphics(); float posx, posy; int angle; Random val = new Random(); Pen pluma; for (angle = 0; angle < 360 * 10; angle++) { posx = (float)(CENTRO_X + LENGHT * Math.Sin(angle / (180 * Math.PI))); posy = (float)(CENTRO_Y + LENGHT * Math.Cos(angle / (180 * Math.PI))); pluma = new Pen(Color.FromArgb(val.Next(256), val.Next(256), val.Next(256))); g.DrawLine(pluma, CENTRO_X, CENTRO_Y, posx, posy); //g.DrawEllipse(pluma, CENTRO_X, CENTRO_Y, posx, posy); } } private void button2_Click(object sender, EventArgs e) { timer2.Enabled = true; boton = 2; timer1.Enabled = false; timer3.Enabled = false; this.Refresh(); } private void timer2_Tick(object sender, EventArgs e) { int CENTRO_X = 100; int CENTRO_Y = 100; Graphics g = pictureBox1.CreateGraphics(); float posx, posy; int angle; Random val = new Random(); Pen pluma;

for (angle = 0; angle < 360; angle++) { posx = (float)(CENTRO_X + LENGHT * Math.Sin(angle / (180 * Math.PI))); posy = (float)(CENTRO_Y + LENGHT * Math.Cos(angle / (180 * Math.PI))); pluma = new Pen(Color.FromArgb(val.Next(256), val.Next(256), val.Next(256))); g.DrawLine(pluma, CENTRO_X, CENTRO_Y, posx, posy); } } private void numericUpDown1_ValueChanged(object sender, EventArgs e) { LENGHT = Convert.ToInt32(numericUpDown1.Value); Refresh(); } private void button4_Click(object sender, EventArgs e) { if (button4.Text == "DETENER") { button4.Text = "CONTINUAR"; button1.Enabled = false; button2.Enabled = false; button3.Enabled = false; switch (boton) { case 1: timer1.Enabled = false; break; case 2: timer2.Enabled = false; break; case 3: timer3.Enabled = false; break; } } else { button4.Text = "DETENER"; button1.Enabled = true; button2.Enabled = true; button3.Enabled = true; switch (boton) { case 1: timer1.Enabled = true; break; case 2: timer2.Enabled = true; break; case 3: timer3.Enabled = true; break; } }

} private void button3_Click(object sender, EventArgs e) { timer1.Enabled = false; timer2.Enabled = false; timer3.Enabled = true; boton = 3; this.Refresh(); } private void timer3_Tick(object sender, EventArgs e) { int X_CENTER = pictureBox1.Width / 2; int Y_CENTER = pictureBox1.Height / 2; int LENGHT2 = 90; int angle; double hypot = Math.Sqrt(LENGHT2 * LENGHT2 + LENGHT2 * LENGHT2); Graphics g = pictureBox1.CreateGraphics(); Random val = new Random(); for (angle = 0; angle < 360; angle++) { Pen pluma = new Pen(Color.FromArgb(val.Next(255),val.Next(255),val.Next(255))); float y_pos1 = (float)(Y_CENTER + LENGHT2 * Math.Sin(angle / 180.0 * Math.PI)); float x_pos1 = (float)(X_CENTER + LENGHT2 * Math.Cos(angle / 180.0 * Math.PI)); //Thread.Sleep(1); float y_pos2 = (float)(Y_CENTER + hypot * Math.Sin((angle + 45) / 180.0 * Math.PI)); float x_pos2 = (float)(X_CENTER + hypot * Math.Cos((angle + 45) / 180.0 * Math.PI)); g.DrawLine(pluma, x_pos1, y_pos1, x_pos2, y_pos2); //Thread.Sleep(1); float y_pos3 = (float)(Y_CENTER + LENGHT2 * Math.Sin((angle + 90) / 180.0 * Math.PI)); float x_pos3 = (float)(X_CENTER + LENGHT2 * Math.Cos((angle + 90) / 180.0 * Math.PI)); g.DrawLine(pluma, x_pos2, y_pos2, x_pos3, y_pos3); //Thread.Sleep(1); g.DrawLine(pluma, x_pos3, y_pos3, X_CENTER, Y_CENTER); //Thread.Sleep(1); }

} }

You might also like