TRANSFORMACIONES LINEALES EN C#-WINDOWS FORMS
Estudiantes:· Leandro Cardozo Arandia
· Danitza Benitez Paco
· David Willy Cruz Huanca
using System;
using
System.Drawing;
using
System.Windows.Forms;
namespace pf5
{
public partial class form1 : Form
{
//Declaración de las diferentes variables publicas que se usan en distintas funciones
private const int jugadorSize = 20;
private int jugadorX = 404;
private int jugadorY = 380;
private Label posicionLabel;
private Timer timer;
int
marioY;
int
luigiX, luigiY;
int
marioX;
private int factorMovimiento = 100;
public form1()
{
InitializeComponent();
//Las 2 siguientes lineas de codigo sirven para estrechar la imagen que se puse de fondo para imitar un plano cartesiano y limitando el tamaño de todo los que se muestra en pantalla a un tamaño 800x800, esto nos servirá mas adelante debido a que un formulario que se muestra en pantalla tiene como punto (0.0) al esquina izquierda superior.
this.BackgroundImageLayout
= ImageLayout.Stretch;
this.ClientSize
= new Size(800, 800);
this.StartPosition
= FormStartPosition.CenterScreen;
this.KeyDown
+= Form1_KeyDown;
KeyPreview =
true;
KeyDown +=
Form1_KeyDown;
DoubleBuffered = true;
timer = new
Timer();
timer.Interval = 100;
timer.Tick
+= Timer_Tick;
timer.Start();
// Aquí empiezan las diferentes funciones que se uso para el programa
}
//Esta función es utilizada para que el valor del posicionamiento de los vectores mario y luigii se vallan actualizando y también se les da un valor
private void Timer_Tick(object sender, EventArgs e)
{
// Para este programa el punto medio es (404,360), (Se escoge este punto como(0,0) por temas de mejor visualización de las imágenes que representan los vectores)
// entendido lo anterior se realiza los calculo para transformar que desde el punto medio (404,360) hasta los diferentes extremos existen alrededor de 400 pixeles por tanto cada 40 pixeles es equivalente a 1
//Por tanto si 404 es igual al punto X=0, el punto 444 = 1, el punto 484=2, así sucesivamente. Eso lo hace el siguiente calculo:
marioX = (((int)marioo.Left) - 404) / 40;
marioY = ((((int)marioo.Top) - 360) / 40) * -1;
luigiX = (((int)luigii.Left) - 404) / 40;
luigiY = ((((int)luigii.Top) - 360) / 40) * -1;
// Se manda a los textbox que representan como matrices las diferentes posiciones ya calculada para que imiten como si estuvieran en un plano cartesiano
textBox1.Text = marioX.ToString();
textBox3.Text = marioY.ToString();
textBox2.Text = luigiX.ToString();
textBox4.Text = luigiY.ToString();
}
tanto como para arriba, abajo, izquierda y derecha en ambos vectores
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
int
step = 40;
switch (e.KeyCode)
{
case
Keys.Left:
marioo.Left -= step;
break;
}
if
(e.KeyCode == Keys.I)
{
marioo.Top -= step;
}
else if
(e.KeyCode == Keys.K)
{
marioo.Top += step;
}
else if
(e.KeyCode == Keys.J)
{
marioo.Left -= step;
}
else if
(e.KeyCode == Keys.L)
{
marioo.Left += step;
}
if
(e.KeyCode == Keys.W)
{
luigii.Top -= step;
}
else if
(e.KeyCode == Keys.S)
{
luigii.Top += step;
}
else if
(e.KeyCode == Keys.A)
{
luigii.Left -= step;
}
else if
(e.KeyCode == Keys.D)
{
luigii.Left += step;
}
Invalidate();
}
private void Form1_Load(object sender, EventArgs e)
{
marioo.Location
= new System.Drawing.Point(100, 100);
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void pictureBox2_Click(object sender, EventArgs e)
{
}
(anteriormente solo se calcularon para que muestre en el textbox para imitar un plano cartesiano)
private void MoverPictureBox(int offset)
{
// Transformación lineal de Reflexión a través del eje x para Mario
int
x = marioo.Location.X;
int
nuevaX = x -((marioX * 40) + marioX * 40);
marioo.Location
= new System.Drawing.Point(nuevaX, marioo.Location.Y);
}
private void MoverPictureBox2(int offset)
{
// Transformación lineal de Reflexión a través del eje x para Luigi
int
x = luigii.Location.X;
int nuevaX = x - ((luigiX*40)+luigiX*40);
luigii.Location = new
System.Drawing.Point(nuevaX, luigii.Location.Y);
}
private void buttonIzquierda_Click_1(object sender,
EventArgs e)
{
MoverPictureBox(-factorMovimiento);
}
private void buttonDerecha_Click_1(object sender,
EventArgs e)
{
MoverPictureBox2(-factorMovimiento);
}
private void MoverPictureBox3(int offset)
{
// Transformación lineal de Reflexión a través del eje Y para Mario
int
y = marioo.Location.Y;
int
nuevaY = y - (((((marioY * 40) + marioY
* 40))*-1));
marioo.Location
= new System.Drawing.Point(marioo.Location.X,nuevaY);
}
private void MoverPictureBox4(int offset)
{
// Transformación lineal de Reflexión a través del eje Y para Luigi
int y = luigii.Location.Y;
int nuevaY = y - (((((luigiY * 40) + luigiY *
40)) * -1));
luigii.Location = new
System.Drawing.Point(luigii.Location.X, nuevaY);
}
private void button3_Click(object sender, EventArgs e)
{
MoverPictureBox3(-factorMovimiento);
}
private void button4_Click(object sender, EventArgs e)
{
MoverPictureBox4(-factorMovimiento);
}
private void MoverPictureBox5(int offset)
{
// Reflexión a través de la recta x2 = x1, donde solo se intercambian posiciones de Mario y Luigi
int
yl = luigii.Location.Y;
int
xl = luigii.Location.X;
int
ym = marioo.Location.Y;
int xm = marioo.Location.X;
luigii.Location
= new System.Drawing.Point(xm, ym);
marioo.Location
= new System.Drawing.Point(xl, yl);
}
private void button5_Click_1(object sender, EventArgs e)
{
MoverPictureBox5(-factorMovimiento);
}
private void button1_Click(object sender, EventArgs e)
{
// Reflexión a través de la recta x2 = -x1, donde se aprovecha que esta transformación lineal va a ser igual a la aplicación de las 3 anteriores transformaciones lineales y solo se las invoca y se usen al clicar este botón
buttonIzquierda_Click_1(sender, e);
button4_Click(sender, e);
button5_Click_1(sender, e);
}
}
}
Código sin comentarios:



Comentarios
Publicar un comentario