资源描述:
《openGL画直线、圆、椭圆》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、使用openGl画直线(DDA算法)、画圆、椭圆(Bresenham算法)#include#include#include/*initialization:*/voidmyinit(void){/*attributes*/glClearColor(1.0,1.0,1.0,0.0);/*whitebackground*/glColor3f(1.0,0.0,0.0);/*drawinred*//*setupviewing:*//*500x5
2、00windowwithoriginlowerleft*/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);gl
3、Vertex3f(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);
4、glBegin(GL_POINTS);while(x5、pse(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)6、+;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(y>0){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
7、(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_
8、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:*/voiddis