miércoles, 24 de octubre de 2007

tarea: linea y curva de bresenham

#include

void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
}

void IluminaPixel (int x, int y)
{
glColor3f(0.5,0.5,0.5);
glBegin(GL_POINTS);
glVertex2f(x,y);
glEnd();


}

void TrazaLinea (int xa, int ya, int xb, int yb)
{
int dx=abs(xa-xb),dy=abs(ya-yb);
int p=2*dy-dx;
int twoDy=2*dy, twoDyDx=2*(dy-dx);
int posx, posy, xEnd;
if(xa > xb) {
posx = xb;
posy = yb;
xEnd=xa;
}
else {
posx= xa;
posy= ya;
xEnd= xb;
}
IluminaPixel (posx, posy);
while (posx < xEnd) {
posx++;
if (p > 0)
p += twoDy;
else {
posy++;
p += twoDyDx;
}
IluminaPixel (posx, posy);
}
}

void Bresenham (void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);

glPushMatrix();
glTranslatef(0,0,0);
TrazaLinea(0,0,40,20);
glColor3f(0.5,0.5,0.5);
glBegin(GL_LINES);
glVertex2f(-10,-10);
glVertex2f(10,20);
glEnd();
glPopMatrix();
glFlush();
}

void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho(-30, 75, -30, 35, -200, 100);
else
glOrtho(-5.0*(GLfloat)w/(GLfloat)h,
5.0*(GLfloat)w/(GLfloat)h, -5.0, 5.0, -5.0, 5.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow ("Linea de Bresenham");
init ();
glutDisplayFunc(Bresenham);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}

No hay comentarios: