12:PROGRAM:
#include
#include
#include
void init(void)
{
glClearColor(1.0,1.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0,300.0,0.0,300.0);
}
void setPixel(GLint xcoord,GLint ycoord)
{
glBegin(GL_POINTS);
glVertex2i(xcoord,ycoord);
glEnd();
glFlush();
}
void line(GLint x0,GLint y0,GLint xend,GLint yend)
{
GLint
dx=fabs(xend-x0);
GLint
dy=fabs(yend-y0);
GLint
p=2*dy-dx;
GLint
twody=2*dy;
GLint
twodyminusdx=2*(dy-dx);
GLint
x,y;
if(x0>xend)
{
x=xend;
y=yend;
xend=x;
}
else
{
x=x0;
y=y0;
}
setPixel(x,y);
while(x
{
x++;
if(p<0 font="">0>
p+=twody;
else
{
y++;
p+=twodyminusdx;
}
setPixel(x,y);
}
}
void drawMyLine(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0,1.0,0.0);
glPointSize(4.0);
GLint
x0=10;
GLint
y0=15;
GLint
xend=250;
GLint
yend=100;
line(x0,y0,xend,yend);
}
void main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(400,400);
glutInitWindowPosition(0,0);
glutCreateWindow("Bresenham's
algorithm");
init();
glutDisplayFunc(drawMyLine);
glutMainLoop();
}
No comments:
Post a Comment