使用openGl画直线(DDA算法)、画圆、椭圆(Bresenham算法)#includestdlib.h#includeiostream.h#includeGL/glut.h/*initialization:*/voidmyinit(void){/*attributes*/glClearColor(1.0,1.0,1.0,0.0);/*whitebackground*/glColor3f(1.0,0.0,0.0);/*drawinred*//*setupviewing:*//*500x500windowwithoriginlowerleft*/glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(0.0,500.0,0.0,500.0);glMatrixMode(GL_MODELVIEW);}voidplot_circle_points(intxc,intyc,intx,inty)//画圆{glBegin(GL_POINTS);glVertex3f(xc+x,yc+y,0);glVertex3f(xc-x,yc+y,0);glVertex3f(xc+x,yc-y,0);glVertex3f(xc-x,yc-y,0);glVertex3f(xc+y,yc+x,0);glVertex3f(xc-y,yc+x,0);glVertex3f(xc+y,yc-x,0);glVertex3f(xc-y,yc-x,0);glEnd();}voiddrawcircle(intxc,intyc,intradius){intx,y,p;x=0;y=radius;p=3-2*radius;glClear(GL_COLOR_BUFFER_BIT);glBegin(GL_POINTS);while(xy){plot_circle_points(xc,yc,x,y);if(p0)p=p+4*x+6;else{p=p+4*(x-y)+10;y-=1;}x+=1;}if(x==y)plot_circle_points(xc,yc,x,y);}voidputpixel(intxc,intyc,intx,inty)//画椭圆{glBegin(GL_POINTS);glVertex3f(xc+x,yc+y,0);glEnd();}voiddrawEllipse(intxc,intyc,inta,intb){intx,y;floatd1,d2;x=0;y=b;d1=b*b+a*a*(-b+0.25);putpixel(xc,yc,x,y);putpixel(xc,yc,-x,-y);putpixel(xc,yc,-x,y);putpixel(xc,yc,x,-y);while(b*b*(x+1)a*a*(y-0.5)){if(d10){d1+=b*b*(2*x+3);x++;}else{d1+=b*b*(2*x+3)+a*a*(-2*y+2);x++;y--;}putpixel(xc,yc,x,y);putpixel(xc,yc,-x,-y);putpixel(xc,yc,-x,y);putpixel(xc,yc,x,-y);}d2=b*b*(x+0.5)*(x+0.5)+a*a*(y-1)*(y-1)-a*a*b*b;while(y0){if(d2=0){d2+=b*b*(2*x+2)+a*a*(-2*y+3);x++;y--;}else{d2+=a*a*(-2*y+3);y--;}putpixel(xc,yc,x,y);putpixel(xc,yc,-x,-y);putpixel(xc,yc,-x,y);putpixel(xc,yc,x,-y);}}voiddda_line(intxa,intya,intxb,intyb)//画直线{GLfloatdelta_x,delta_y,x,y;intdx,dy,steps;dx=xb-xa;dy=yb-ya;if(abs(dx)abs(dy))steps=abs(dx);elsesteps=abs(dy);delta_x=(GLfloat)dx/(GLfloat)steps;delta_y=(GLfloat)dy/(GLfloat)steps;x=xa;y=ya;glClear(GL_COLOR_BUFFER_BIT);glBegin(GL_POINTS);glVertex3f(x,y,0);for(intk=1;k=steps;k++){x+=delta_x;y+=delta_y;glBegin(GL_POINTS);glVertex3f(x,y,0);glEnd();}}/*thedisplaycallback:*/voiddisplay1()//画圆{glClear(GL_COLOR_BUFFER_BIT);/*clearthewindow*//*----------------------------------------*//*viewportstuff*//*----------------------------------------*//*setupaviewportinthescreenwindow*//*argstoglViewportareleft,bottom,width,height*/glViewport(0,0,500,500);/*NB:defaultviewporthassamecoordsasinmyinit,*//*sothiscouldbeomitted:*/dda_line(20,20,420,350);/*andflushthatbuffertothescreen*/glFlush();}voiddisplay2()//画圆{glClear(GL_COLOR_BUFFER_BIT);/*clearthewindow*//*----------------------------------------*//*viewportstuff*//*----------------------------------------*//*setupaviewportinthescreenwindow*//*argstoglViewportareleft,bottom,width,height*/glViewport(0,0,500,500);/*NB:defaultviewporthassamecoordsasinmyinit,*//*sothiscouldbeomitted:*/drawcircle(250,250,100);/*andflushthatbuffertothescreen*/glFlush();}voiddisplay3()//画圆{glClear(GL_COLOR_BUFFER_BIT);/*clearthewindow*//*----------------------------------------*//*viewportstuff*//*----------------------------------------*//*setupaviewportinthescreenwindow*//*argstoglViewportareleft,bottom,width,height*/glViewport(0,0,500,500);/*NB:defaultviewporthassamecoordsasinmyinit,*//*sothiscouldbeomitted:*/drawEllipse(250,250,60,40);/*andflushthatbuffertothescreen*/glFlush();}intmain(intargc,char**argv){/*StandardGLUTinitialization*/glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);/*default,notneeded*/glutInitWindowSize(500,500);/*500x500pixelwindow*/glutInitWindowPosition(0,0);/*placewindowtopleftondisplay*/cout************************************************endl;cout****请选择如下操作:*****endl;cout****1.用DDA画线*****endl;cout****2.用Bresenham算法画圆*****endl;cout****3.用Bresenham算法画椭圆*****endl;cout************************************************endl;cout请选择如下操作:;intchoice;cinchoice;switch(choice){case1:glutCreateWindow(Bresenhamcircile);/*windowtitle*/glutDisplayFunc(display1);/*displaycallbackinvokedwhenwindowopened*/myinit();/*setattributes*/glutMainLoop();/*entereventloop*/break;case2:glutCreateWindow(DigitalDifferentialAnalyserLine);/*windowtitle*/glutDisplayFunc(display2);/*displaycallbackinvokedwhenwindowopened*/myinit();/*setattributes*/glutMainLoop();/*entereventloop*/break;case3:glutCreateWindow(MidBresenhamEllipse);/*windowtitle*/glutDisplayFunc(display3);/*displaycallbackinvokedwhenwindowopened*/myinit();/*setattributes*/glutMainLoop();/*entereventloop*/default:return0;}return0;}