Bresenham画线算法

时间:2013-12-30 15:32:57
【文件属性】:

文件名称:Bresenham画线算法

文件大小:2KB

文件格式:TXT

更新时间:2013-12-30 15:32:57

Bresenham

Bresenham画线算法 #include #include #define BLACK 0 void draw_pixel(int ix,int iy,int value) { glBegin(GL_POINTS); glVertex2i(ix,iy); glEnd(); } bres(int x1,int y1,int x2,int y2) { int dx,dy,i,e; int incx,incy,inc1,inc2; int x,y; dx=x2-x1; dy=y2-y1; if(dx<0) dx=-dx; if(dy<0) dy=-dy; incx=1; if(x2dy) { draw_pixel(x,y,BLACK); e=2*dy-dx; inc1 = 2*(dy-dx); inc2 = 2*dy; for(i=0;i=0) { y+=incy; e+=inc1; } else e+=inc2; x+=incx; draw_pixel(x,y,BLACK); } } else { draw_pixel(x,y,BLACK); e=2*dx-dy; inc1=2*(dx-dy); inc2=2*dx; for(i=0;i=0) { x+=incx; e+=inc1; } else e+=inc2; y+=incy; draw_pixel(x,y,BLACK); } } } void display() { glClear(GL_COLOR_BUFFER_BIT); bres(200,200,100,50); glFlush(); } void myinit() { glClearColor(1.0,1.0,1.0,1.0); glColor3f(1.0,0.0,0.0); glPointSize(1.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0,499.0,0.0,499.0); } void main(int argc,char **argv) { /* standard GLUT initialization */ glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB );/*default,not needed*/ glutInitWindowSize(500,500); /* 500×500 pixel window */ glutInitWindowPosition(0,0);/* place window top left on display */ glutCreateWindow("Bresenham's algorithm ");/* window title */ glutDisplayFunc(display); /* display callback invoked when window opened */ myinit(); /*set attributes */ glutMainLoop(); /* enter event loop */ }


网友评论