// ALGORITMO DE BRESENHAM PARA EL TRAZO DE UNA LINEA #include <GL/glut.h> #include <stdio.h> float colores[7][3]={1.,1.,1.,1.,0.,0.,0.,1.,0.,0.,0.,1.,0.,1.,1.,1.,0.,1.,1.,1.,0.}; void setPixel(int, int); void Desicion(int, int, int, int); void Bresenham(int, int, int, int, int); void Bresenham(int x, int y, int Ax, int Ay, int x_end) { int const1, const2, p; p=2*Ay-Ax; const1 = 2*Ay; const2 = 2*(Ay-Ax); while(x < x_end) { x = x + 1; if (p < 0) p = p + const1; else { y = y + 1; p = p + const2; } setPixel(x, y); } } void Desicion(int x1, int y1, int x2, int y2) { int Ax, Ay, x, y, x_end; Ax = abs(x1 - x2); Ay = abs(y1 - y2); if ( x1 > x2 ) { x=x2; y=y2;x_end=x1; } else {x=x1;y=y1;x_end=x2; } setPixel(x, y); Bresenham(x, y, Ax, Ay, x_end); } void setPixel(int x, int y) { glBegin(GL_POINTS); glColor3f(1,0,0); glVertex3f(x,y,0); glEnd(); glFlush(); } void init(void) { glEnable (GL_POINT_SMOOTH); glHint (GL_POINT_SMOOTH_HINT, GL_DONT_CARE); } void display(void) { glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-50, 50, -50, 50); glMatrixMode(GL_MODELVIEW); glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0, 1.0, 0.0); glBegin(GL_LINES); glColor3f(1,1,0); glVertex3f(-50,0,0); glVertex3f(50,0,0); glVertex3f(0,-50,0); glVertex3f(0,50,0); glEnd(); // BressenHam(1,1,20,20); // (x1, y1, x2, y2) OK // BressenHam(-20, 20, 30, 20); OK Desicion(-20, 20, 20, -20); //glutSwapBuffers(); // CAMBIAMOS EL BUFFER OCULTO POR EL ACTIVO } void mouse(int Boton, int Estado, int x, int y) { if (Boton==GLUT_LEFT_BUTTON && Estado==GLUT_DOWN) { } } void Medio(int id) { glColor3f(colores[id][0], colores[id][1], colores[id][2]); } int main(int argc, char** argv) { glutInit(&argc, argv); // Inicializa la libreria auxiliar GLUT glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); // Inicializa el modo de visualizacion glutInitWindowSize (500, 500); // Indica el tamano de la ventana (ancho,alto) glutInitWindowPosition (20, 20); // Indica la posiciOn inicial (xmin,ymin) glutCreateWindow ("Algoritmo de linea Bresenham"); // Abre la ventana con el tItulo indicado glutDisplayFunc(display); // Indica cual es la funciOn de dibujo glutCreateMenu(Medio); glutAddMenuEntry("uno", 0); glutAddMenuEntry("dos", 1); glutAttachMenu(GLUT_MIDDLE_BUTTON); glutMouseFunc(mouse); init(); glutMainLoop(); }