Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Daniel Eduardo Pedroza Pedraza

6000459

Transformación Geométrica
#define nodos 6
#include<cstdlib>
#include "glut.h"
#include<math.h>
#include<iostream>
using namespace std;
int anc = 800, alt = 600, T = 0, E = 0, R = 0;
float Tx = 1, Ty = 0, tam = 0, Sx = 0.9999, Sy = 0.9999, angulo = 0.05;
float matrix[nodos][2] = {//LA figura vertice centro 3
{150,50},{100,150},{200,250},{300,150},{250,50},{150,50}
};
void pixel(float xi, float yi, float xf, float yf, float R, float G, float B) {
glLineWidth(3);
glBegin(GL_LINES);//cambiamos puntos a lineas
glColor3f(R, G, B);
glVertex2d(xi, yi);
glVertex2d(xf, yf);
glEnd();
glFlush();
}
void plano(float m[nodos][2]) {
for (int i = 0; i < nodos - 1; i++) {
pixel(m[i][0], m[i][1], m[i + 1][0], m[i + 1][1], 1, 0, 0);
}
}
void traslacion(float m[nodos][2]) {
float tras[3][3] = { {1,0,Tx},{0,1,Ty},{0,0,1} };
if (m[3][0] < anc && m[3][1] == 50) {
Tx = 1, Ty = 0;
}
else if (m[3][0] == anc && m[3][1] < alt) {
Tx = 0, Ty = 1;
}
else if (m[3][0] > 150 && m[3][1] == alt) {
Tx = -1, Ty = 0;
}
else if (m[3][0] == 150 && m[3][1] > alt) {
Tx = 0, Ty = -1;
}
float p1 = m[3][0], p2 = m[3][1];
for (int i = 0; i < nodos; i++) {
m[i][0] = ((m[i][0] * tras[0][0]) + (m[i][1] * tras[0][1]) +
tras[0][2]);
m[i][1] = ((m[i][0] * tras[1][0]) + (m[i][1] * tras[1][1]) +
tras[1][2]);
}
plano(m);
}
void escala(float m[nodos][2]) {

float Xc = m[3][0], Yc = m[3][1], esc[3][3] = { {Sx,0,Xc*(1 - Sx)},


{0,Sy,Yc*(1 - Sy)},{0,0,1} };
if (m[3][0] < anc && m[3][1] == 50) {
Sx = 1, Sy = 1;
}
else if (m[3][0] == anc && m[3][1] < alt) {
Sx = 1.005, Sy = 1.005;
}
else if (m[3][0] > 150 && m[3][1] == alt) {
Sx = 1, Sy = 1;
Daniel Eduardo Pedroza Pedraza
6000459
}
else if (m[3][0] == 150 && m[3][1] > alt) {
Sx = 1.005, Sy = 1.005;
}
for (int i = 0; i < nodos; i++) {
m[i][0] = ((m[i][0] * esc[0][0]) + (m[i][1] * esc[0][1]) + esc[0]
[2]);
m[i][1] = ((m[i][0] * esc[1][0]) + (m[i][1] * esc[1][1]) + esc[1]
[2]);
}
plano(m);
}
void rotar(float m[nodos][2]) {
float Xc = m[3][0], Yc = m[3][1], rot[3][3] = { {cos(angulo),-
sin(angulo),-Xc * (cos(angulo)) + Yc * sin(angulo) + Xc},
{sin(angulo),cos(angulo),-Xc * (sin(angulo)) - Yc * cos(angulo) + Yc},{0,0,1} };
for (int i = 0; i < nodos; i++) {
m[i][0] = ((m[i][0] * rot[0][0]) + (m[i][1] * rot[0][1]) + rot[0]
[2]);
m[i][1] = ((m[i][0] * rot[1][0]) + (m[i][1] * rot[1][1]) + rot[1]
[2]);
}
plano(m);
}
void teclado(unsigned char key, int x, int y) {
switch (key) {
case 't':
if (T == 0) {
T = 1;
}
else {
T = 0;
}
glutPostRedisplay();
break;
case 'e':
if (E == 0) {
E = 1;
}
else {
E = 0;
}
glutPostRedisplay();
break;
case 'r':
if (R == 0) {
R = 1;
}

else {
R = 0;
}
glutPostRedisplay();
break;
}
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
if (T + E + R == 0) {
plano(matrix);
}
if (T == 1) {
Daniel Eduardo Pedroza Pedraza
6000459
traslacion(matrix);
}
if (E == 1) {
escala(matrix);
}
if (R == 1) {
rotar(matrix);
}
glFlush;
glutSwapBuffers();
glutPostRedisplay();
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowSize(anc, alt);
glutInitWindowPosition(75, 50);
glutCreateWindow("Transformaciones 2d");
glClearColor(0, 0, 0, 1);
gluOrtho2D(0, anc, 0, alt);
glutDisplayFunc(display);
glutKeyboardFunc(teclado);
glutMainLoop();
return 0;
}

You might also like