// ALGORITMO DE BRESENHAM PARA EL TRAZO DE UN CIRCULO
#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 circulo(int, int, int);
void EnCuadrantes(int, int);
void circulo(int Radio, int x, int y)
{
float p=0; // VALOR INICIAL DEL PARAMETRO DE DESICION
int xc, yc,CX, CY; // xc, yc coordenadas del circulo
// CX, CY terminos para el calculo del parametro de desicion
xc = 0; yc = Radio; // coordenadas iniciales
EnCuadrantes(xc, yc); // Coloca los puntos en los 8 octantes
p = 1 - Radio; // Es con enteros. Valor inicial del parametro de desicion
CX = 2 * xc;
CY = 2 * yc;
while (xc < yc) // Se repite hasta que xc >= yc
{
if (p < 0) { xc +=1; p += CX + 1; }
else { xc += 1; yc -= 1; p += CX + 1 - CY; }
CX = 2*(xc+1); CY = 2*yc;
EnCuadrantes(xc, yc);
x += xc; y += yc;
}
}
void EnCuadrantes(int x, int y) // Primer punto (xc, yc)
{
setPixel(x, y); setPixel(y, x); // CUADRANTE 1
setPixel(-y, x); setPixel(-x, y); // CUADRANTE 2
setPixel(-x, -y); setPixel(-y, -x); // CUADRANTE 3
setPixel(y, -x); setPixel(x, -y); // CUADRANTE 4
}
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(-100, 100, -100, 100);
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();
circulo(50, 0, 0); // (radio, x, y)
}
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 del punto medio para el circulo"); // Abre la ventana con el tItulo indicado
glutDisplayFunc(display); // Indica cual es la funciOn de dibujo
glutCreateMenu(Medio);
glutAddMenuEntry("Blanco", 0);
glutAddMenuEntry("Rojo", 1);
glutAttachMenu(GLUT_MIDDLE_BUTTON);
glutMouseFunc(mouse);
init();
glutMainLoop();
}