#include "graphics.h"
#include <stdio.h>
void MidBresenhamEllipse(int a,int b,int color)
{
int x,y;
float d1,d2;
x=0;y=b;
d1=b*b+a*a*(-b+0.5);
putpixel(x+200,y+100,color); putpixel(-x+200,-y+100,color);
putpixel(-x+200,y+100,color);putpixel(x+200,-y+100,color);
while(b*b*(x+1)<a*a*(y-0.5)){
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+200,y+100,color); putpixel(-x+200,-y+100,color);
putpixel(-x+200,y+100,color);putpixel(x+200,-y+100,color);
}
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(x+200,y+100,color); putpixel(-x+200,-y+100,color);
putpixel(-x+200,y+100,color);putpixel(x+200,-y+100,color);
}
}
void main( )
{
int gdriver,gmode;
gdriver=DETECT;
initgraph(&gdriver,&gmode,"");
setbkcolor(BLUE);
outtextxy(100,0,"中点Bresenham椭圆算法");
MidBresenhamEllipse(100,80,255);
getchar( );
closegraph( );
}