////////////////////////////////////////////////////////////////////
//中点圆整数算法 画圆 //
/////////////////////////////////////////////////////////////////////
void roundMid(int x1,int y1,int R,CDC* pDC)
{
int x=0,y=R;
int d=1-R; //起点(0,R),下一点中点(1,R-0.5),d=1*1+(R-0.5)*(R-0.5)-R*R=1.25-R,d只参与整数运算,所以小数部分可省略
while(y>x) //y>x即第一象限的第2区八分圆
{
pDC->SetPixel(x+x1,y+y1,RGB(255,0,0)); //圆心(x1,y1),画点时直接相加平移,画2区
pDC->SetPixel(y+x1,x+y1,RGB(255,0,0)); //画1区
pDC->SetPixel(-x+x1,y+y1,RGB(255,0,0)); //画3区
pDC->SetPixel(-y+x1,x+y1,RGB(255,0,0)); //画4区
pDC->SetPixel(-x+x1,-y+y1,RGB(255,0,0)); //画5区
pDC->SetPixel(-y+x1,-x+y1,RGB(255,0,0)); //画6区
pDC->SetPixel(x+x1,-y+y1,RGB(255,0,0)); //画7区
pDC->SetPixel(y+x1,-x+y1,RGB(255,0,0)); //画8区
if(d<0)
d=d+2*x+3; //d的变化
else
{
d=d+2*(x-y)+5; //d<=0时,d的变化
y--; //y坐标减1
}
x++; //x坐标加1
}
}
////////////////////////////////////////////////////////////////////
//Bresenham算法画圆 //
/////////////////////////////////////////////////////////////////////
void RoundBre(int x1,int y1,int R,CDC* pDC)
{
//圆心(x1,y1),当前像素I(xi,yi),右像素H(xi+1,y),右下像素D(xi+1,yi-1),下像素V(xi,yi-1)
int xi,yi,dd,m,n;
//圆心距差值平方dh=(xi+1)*(xi+1)+yi*yi-R*R; dd=(xi+1)*(xi+1)+(yi-1)*(yi-1)-R*R; dv=xi*xi+(yi-1)*(yi-1)-R*R;
xi=0; //起点(0,R)
yi=R;
dd=2-2*R; //起点的右下像素(1,R-1)的圆心距差值dd=1+(R-1)*(R-1)-R*R
pDC->SetPixel(x1,y1,RGB(0,0,255)); //画圆心,证明圆心必须占用一个像素
while(yi>=xi) //循环到八分之一圆;yi>=0即可以画出四分圆
{
pDC->SetPixel(xi+x1,yi+y1,RGB(255,0,0)); //2区,坐标平移(x1,y1)画圆
pDC->SetPixel(yi+x1,xi+y1,RGB(255,0,0)); //1区
pDC->SetPixel(-xi+x1,yi+y1,RGB(255,0,0)); //3区
pDC->SetPixel(-yi+x1,xi+y1,RGB(255,0,0)); //4区
pDC->SetPixel(-yi+x1,-xi+y1,RGB(255,0,0)); //5区
pDC->SetPixel(-xi+x1,-yi+y1,RGB(255,0,0)); //6区
pDC->SetPixel(xi+x1,-yi+y1,RGB(255,0,0)); //7区
pDC->SetPixel(yi+x1,-xi+y1,RGB(255,0,0)); //8区
if(dd<0) //D在圆内;H在圆上或圆外;选择H或D,dh>=0,dd<0,m=|dh|-|dd|=2*(dd+yi)-1;
{
m=2*(dd+yi)-1;
if(m<=0) //取右像素H(xi+1,yi);m=0时 取右点
{
dd=dd+2*xi+3; //求H的右下像素(xi+2,yi-1),dd=(xi+2)*(xi+2)+(yi-1)*(yi-1)-R*R
xi=xi+1;
}
else //取右下像素D(xi+1,yi-1)
{
dd=dd+2*(xi-yi+3); //求D的右下像素(xi+2,yi-2),dd=(xi+2)*(xi+2)+(yi-2)*(yi-2)-R*R
xi=xi+1;
yi=yi-1;
}
}
else if(dd>0) //D在圆外;V在圆外或圆上;选择D或V;dd>0,dv<=0,n=|dd|-|dv|=2*(dd-xi)-1;
{
n=2*(dd-xi)-1;
if(n<=0) //取右下像素D(xi+1,yi-1);n=0时取
{
dd=dd+2*(xi-yi+3); //求D的右下像素(xi+2,yi-2),dd=(xi+2)*(xi+2)+(yi-2)*(yi-2)-R*R
xi=xi+1;
yi=yi-1;
}
else //取下像素V(xi,yi-1)
{
dd=dd-2*yi+3; //求V的右下像素(xi+1,yi-2)dd=(xi+1)*(xi+1)+(yi-2)*(yi-2)-R*R
yi=yi-1;
}
}
else if(dd==0) //D在圆上,取D(xi+1,yi-1)
{
dd=dd+2*(xi-yi+3); //求D的右下像素(xi+2,yi-2),dd=(xi+2)*(xi+2)+(yi-2)*(yi-2)-R*R
xi=xi+1;
yi=yi-1;
}
}
}