#includewindows.h#includegl/glut.h#includestdio.hintm_PointNumber=0;//动画时绘制点的数目intm_DrawMode=1;//绘制模式1DDA算法画直线//2中点Bresenham算法画直线//3改进Bresenham算法画直线//4八分法绘制圆//5四分法绘制椭圆//绘制坐标线voidDrawCordinateLine(void){inti=-250;//坐标线为黑色glColor3f(0.0f,0.0f,0.0f);glBegin(GL_LINES);for(i=-250;i=250;i=i+10){glVertex2f((float)(i),-250.0f);glVertex2f((float)(i),250.0f);glVertex2f(-250.0f,(float)(i));glVertex2f(250.0f,(float)(i));}glEnd();}//绘制一个点,这里用一个正方形表示一个点voidputpixel(GLsizeix,GLsizeiy){glRectf(10*x,10*y,10*x+10,10*y+10);}/////////////////////////////////////////////////////////////////////DDA画线算法/////////////////////////////////////////////////////////////////////////////////voidDDACreateLine(GLsizeix0,GLsizeiy0,GLsizeix1,GLsizeiy1,GLsizeinum){//设置颜色glColor3f(1.0f,0.0f,0.0f);//对画线动画进行控制if(num==1)printf(DDA画线算法:各点坐标\n);elseif(num==0)return;//画线算法的实现GLsizeidx,dy,epsl,k;GLfloatx,y,xIncre,yIncre;dx=x1-x0;dy=y1-y0;x=x0;y=y0;if(abs(dx)abs(dy))epsl=abs(dx);elseepsl=abs(dy);xIncre=(float)dx/epsl;yIncre=(float)dy/epsl;for(k=0;k=epsl;k++){putpixel((int)(x+0.5),(int)(y+0.5));if(k=num-1){printf(x=%f,y=%f,取整后x=%d,y=%d\n,x,y,(int)(x+0.5),(int)(y+0.5));break;}x+=xIncre;y+=yIncre;if(x=25||y=25)break;}}/////////////////////////////////////////////////////////////////////中点Bresenham算法画直线(0=k=1)/////////////////////////////////////////////////////////////////////////////////voidBresenhamLine(GLsizeix0,GLsizeiy0,GLsizeix1,GLsizeiy1,GLsizeinum){glColor3f(1.0f,0.0f,0.0f);if(num==1)printf(中点Bresenham算法画直线各点坐标及判别式的值\n);elseif(num==0)return;//画线算法的实现GLsizeip=0;GLfloatUpIncre,DownIncre,x,y,d,k,dx,dy;if(x0x1){x=x1;x1=x0;x0=x;y=y1;y1=y0;y0=y;}x=x0;y=y0;dx=x1-x0;dy=y1-y0;k=dy/dx;if(k=0&&k=1){d=dx-2*dy;UpIncre=2*dx-2*dy;DownIncre=-2*dy;while(x=x1){putpixel(x,y);if(p=num-1){printf(x=%d,y=%d\n,x,y);break;}p++;x++;if(d0){y++;d+=UpIncre;}elsed+=DownIncre;}}if(k1){d=dy-2*dx;UpIncre=2*dy-2*dx;DownIncre=-2*dx;while(y=y1){putpixel(x,y);if(p=num-1){printf(x=%d,y=%d\n,x,y);break;}p++;y++;if(d0){x++;d+=UpIncre;}elsed+=DownIncre;}}if(k0&&k=-1){d=dx-2*dy;UpIncre=-2*dy;DownIncre=-2*dx-2*dy;while(x=x1){putpixel(x,y);if(p=num-1){printf(x=%d,y=%d\n,x,y);break;}p++;x++;if(d0){y--;d+=DownIncre;}elsed+=UpIncre;}}if(k-1){d=-dy-2*dx;UpIncre=-2*dx-2*dy;DownIncre=-2*dx;while(y=y1){putpixel(x,y);if(p=num-1){printf(x=%d,y=%d\n,x,y);break;}p++;y--;if(d0){x++;d+=UpIncre;}elsed+=DownIncre;}}}/////////////////////////////////////////////////////////////////////改进的Bresenham算法画直线(0=k=1)////////x1,y1终点坐标/////////////////////////////////////////////////////////////////////////voidBresenham2Line(GLsizeix0,GLsizeiy0,GLsizeix1,GLsizeiy1,GLsizeinum){glColor3f(1.0f,0.0f,0.0f);GLsizeix,y,dx,dy,e,k;if(num==1)printf(改进的Bresenham算法画直线各点坐标及判别式的值\n);elseif(num==0)return;//画线算法的实现GLsizeip=0;if(x0x1){x=x1;x1=x0;x0=x;y=y1;y1=y0;y0=y;}dx=x1-x0;dy=y1-y0;k=dy/dx;if(k=0&&k=1){e=-dx;x=x0;y=y0;while(x=x1){putpixel(x,y);if(p=num-1){printf(x=%d,y=%d\n,x,y);break;}p++;x++;e=e+2*dy;if(e0){y++;e=e-2*dx;}}}if(k1){e=-dy;x=x0;y=y0;while(y=y1){putpixel(x,y);if(p=num-1){printf(x=%d,y=%d\n,x,y);break;}p++;y++;e=e+2*dx;if(e0){x++;e=e-2*dy;}}}if(k0&&k=-1){e=-dx;x=x0;y=y0;while(x=x1){putpixel(x,y);if(p=num-1){printf(x=%d,y=%d\n,x,y);break;}p++;x++;e=e+2*dy;if(e0){y--;e=e+2*dx;}}}if(k-1){e=-dy;x=x0;y=y0;while(y=y1){putpixel(x,y);if(p=num-1){printf(x=%d,y=%d\n,x,y);break;}p++;y--;e=e-2*dx;if(e0){x++;e=e-2*dy;}}}}///////////////////////////////////////////////////////////Bresenham算法画圆///////////////////////////////////////////////////////////////////////voidCirclePoint(GLsizeix,GLsizeiy){putpixel(x,y);putpixel(x,-y);putpixel(y,-x);putpixel(-y,-x);putpixel(-x,-y);putpixel(-x,y);putpixel(-y,x);putpixel(y,x);}voidBresenhamCircle(GLsizeix,GLsizeiy,GLsizeiR,GLsizeinum){glColor3f(1.0f,0.0f,0.0f);GLsizeid;x=0;y=R;d=1-R;if(num==1)printf(Bresenham算法画圆:各点坐标及判别式的值\n);elseif(num==0)return;while(x=y){CirclePoint(x,y);if(x=num-1){printf(x=%d,y=%d,d=%d\n,x,y,d);break;}if(d0)d+=2*x+3;else{d+=2*(x-y)+5;y--;}x++;}}voidBresenham2Circle(GLsizeia,GLsizeib,GLsizeinum){glColor3f(1.0f,0.0f,0.0f);if(num==1)printf(Bresenham算法画椭圆:各点坐标及判别式的值\n);elseif(num==0)return;GLsizeix,y;floatd1,d2;x=0;y=b;d1=b*b+a*a*(-b+0.5);putpixel(x,y);putpixel(-x,-y);putpixel(-x,y);putpixel(x,-y);while(b*b*(x+1)a*a*(y-0.5)){if(x=num-1){printf(x=%d,y=%d,d1=%d\n,x,y,d1);break;}if(d1=0){d1+=b*b*(2*x+3);x++;}else{d1+=b*b*(2*x+3)+a*a*(-2*y+2);x++;y--;}putpixel(x,y);putpixel(-x,-y);putpixel(-x,y);putpixel(x,-y);}//while上半部分d2=b*b*(x+0.5)*(x+0.5)+a*a*(y-1)*(y-1)-a*a*b*b;while(y0){if(x=num-1){printf(x=%d,y=%d,d2=%d\n,x,y,d2);break;}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(x,y);putpixel(-x,-y);putpixel(-x,y);putpixel(x,-y);}}//初始化窗口voidInitial(void){//设置窗口颜色为蓝色glClearColor(0.0f,0.0f,1.0f,1.0f);}//窗口大小改变时调用的登记函数voidChangeSize(GLsizeiw,GLsizeih){if(h==0)h=1;//设置视区尺寸glViewport(0,0,w,h);//重置坐标系统glMatrixMode(GL_PROJECTION);glLoadIdentity();//建立修剪空间的范围if(w=h)glOrtho(-250.0f,250.0f,-250.