程序:罗健军
程序中var16为有符号16位数字型,uvar16为16位无符号数字型,uvar32为无符号32位数字型,var32为有符号32位数字型。Ellipse参数中的x0,y0为圆心坐标,r1和r2分别为横半径和纵半径。PutPixel为画点函数。
void Ellipse(var16 x0,var16 y0,uvar16 r1,uvar16 r2)
{
uvar32 r,r12,r22;
var16 x,y,xmax;
var32 tn;
x=0;y=r2;
r12=r1*r1;r22=r2*r2;
xmax=var16(r12/sqrt(r12+r22));
tn=r12-2*r2*r12;
while(x<=xmax)
{
if(tn<0||y==0)tn+=(4*x+2)*r22;
else
{
tn+=(4*x+2)*r22+(1-y)*4*r12;
y--;
}
PutPixel(x0+x,y0+y);
PutPixel(x0-x,y0+y);
PutPixel(x0+x,y0-y);
PutPixel(x0-x,y0-y);
x++;
}
PutPixel(x0+x,y0+y);
PutPixel(x0-x,y0+y);
PutPixel(x0+x,y0-y);
PutPixel(x0-x,y0-y);
r=r1;r1=r2;
r2=(uvar16)r;
x=0;y=r2;
r12=r1*r1;r22=r2*r2;
xmax=var16(r12/sqrt(r12+r22));
tn=r12-2*r2*r12;
while(x<=xmax)
{
if(tn<0||y==0)tn+=(4*x+2)*r22;
else
{
tn+=(4*x+2)*r22+(1-y)*4*r12;
y--;
}
PutPixel(x0+y,y0+x);
PutPixel(x0+y,y0-x);
PutPixel(x0-y,y0+x);
PutPixel(x0-y,y0-x);
x++;
}
PutPixel(x0+y,y0+x);
PutPixel(x0+y,y0-x);
PutPixel(x0-y,y0+x);
PutPixel(x0-y,y0-x);
}