Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <GL/glut.h>
- typedef struct{
- float x;
- float y;
- } punto;
- typedef struct{
- punto P0;
- punto Pf;
- float dx;
- float dy;
- } arista;
- void poligono(punto pt1, punto pt2, punto pt3);
- void linea(punto A, punto B);
- int W = 700;
- int H = 700;
- int click = 0;
- punto p1, p2, p3;
- void display()
- {
- glClearColor(0.0, 0.0, 0.0, 0.0);
- glClear(GL_COLOR_BUFFER_BIT);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluOrtho2D(0.0, (double)W, 0.0, (double)H);
- glViewport(0.0, 0.0, W, H);
- poligono(p1, p2, p3);
- glColor3f(1.0, 0.0, 0.0);
- linea(p1, p2);
- linea(p2, p3);
- linea(p3, p1);
- glutSwapBuffers();
- }
- void poligono(punto pt1, punto pt2, punto pt3)
- {
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glColor4f(0.10f, 0.69f, 0.40f, 0.0f);
- glBegin(GL_POINTS);
- glVertex2i(pt1.x, pt1.y);
- glVertex2i(pt2.x, pt2.y);
- glVertex2i(pt3.x, pt3.y);
- glEnd();
- glFlush();
- }
- /* ojo*/
- void linea(punto A, punto B)
- {
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glColor4f(0.10f, 0.69f, 0.40f, 0.0f);
- glBegin(GL_POINTS);
- /*Recta forma cartesiana y=mx+b
- m=dy/dx
- Recta forma general f(x,y)=Ax+By+C=0
- donde m= -A/B y b= -C/B*/
- int dx, dy, dE, dNE, De, Dne, d, D, x, y, f, mv;
- dx = labs(B.x - A.x);
- dy = labs(B.y - A.y);
- //Si (dx > dy) entonces se toma que el negativo será B en lugar de A por ende A= dy, B= -dx y C= -bdx
- //Si (dx < dy) entonces se toma que el negativo será A en lugar de B por ende A= -dy, B= dx y C= bdx
- d = 2 * dy - dx; // Es el dk cuando (dx > dy) = (dy -1/2 dx) se multiplicó x2 para quitar los racionales.
- D = 2 * dx - dy; // Es el dk cuando (dx < dy) = (dx -1/2 dy) se multiplicó x2 para queitar los racionales
- dE = 2 * dy; // dE= dk+1 cuando d<0 y cuando (dx > dy)
- De = 2 * dx; // De= dk+1 cuando d<0 y cuando (dx < dy)
- dNE = 2 * (dy - dx); // dNe = dk+1 cuando d>=0 y cuando (dx > dy)
- Dne = 2 * (dx - dy); //Dne = dk+1 cuando d>=0 y cuando (dx < dy)
- x = A.x;
- y = A.y;
- mv = 1; // Dependiendo del octante en que se encuentre indica si la variable que lleva el punto medio aumenta o disminuye.
- if (dx > dy){ // Se ubica desde (pi/4 , 7pi/4) u (3pi/4 , 5pi/4 )
- if (A.x > B.x){
- x = B.x;
- y = B.y;
- f = A.x; // El punto f siempre se ubicará hacia la derecha por organización y cumplimiento de la condición
- //while dentro de un determinado octante
- if (B.y>A.y)
- mv = -1;
- }
- else{
- x = A.x;
- y = A.y;
- f = B.x;
- if (A.y>B.y)
- mv = -1;
- }
- glVertex2i(x, y);
- while (x < f){
- x++;
- if (d < 0)
- d = d + dE;
- else{
- y = y + mv;
- d = d + dNE;
- }
- glVertex2i(x, y);
- }
- } // Desde aquí se refleja en los octantes faltantes desde (pi/4 , 3pi/4) u (5pi/4 , 7pi/4)
- else{
- if (A.y > B.y){
- x = B.x;
- y = B.y;
- f = A.y;
- if (B.x>A.x)
- mv = -1;
- }
- else{
- x = A.x;
- y = A.y;
- f = B.y;
- if (A.x>B.x)
- mv = -1;
- }
- glVertex2i(x, y);
- while (y < f){
- y++;
- if (D < 0)
- D = D + De;
- else{
- x = x + mv;
- D = D + Dne;
- }
- glVertex2i(x, y);
- }
- }
- glEnd();
- glFlush();
- }
- void mouse(int btn, int state, int x, int y)
- {
- if (btn == GLUT_LEFT_BUTTON)
- if (state == GLUT_DOWN)
- {
- switch (click)
- {
- case 0:
- p1.x = x;
- p1.y = H - y;
- click = 1;
- break;
- case 1:
- p2.x = x;
- p2.y = H - y;
- click = 2;
- break;
- case 2:
- p3.x = x;
- p3.y = H - y;
- click = 0;
- break;
- }
- glutPostRedisplay();
- }
- }
- int ordenar()
- {
- }
- int main()
- {
- glutInitWindowSize(W, H);
- glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
- glutCreateWindow("Triangulo");
- glutDisplayFunc(display);
- glutMouseFunc(mouse);
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement