分类: C/C++
2011-09-02 10:42:27
2-1 中点算法画圆
void CMyView::OnMidpontcircle()
{
// TODO: Add your command handler code here
CDC *pDC=GetDC();
int x0=250,y0=120,r=100,color=RGB(0,0,255),x,y,d; //定义圆心(x0,y0),半径为r
x=0;y=r;d=1-r;
pDC->SetPixel(x,y,color);
while(x<=y)
{
if(d<0)
d+=2*x+3;
else
{
d+=2*(x-y)+5;y--;
}
x++;
pDC->SetPixel(x+x0,y+y0,color);
pDC->SetPixel(-x+x0,y+y0,color);
pDC->SetPixel(-x+x0,-y+y0,color);
pDC->SetPixel(x+x0,-y+y0,color);
pDC->SetPixel(y+x0,x+y0,color);
pDC->SetPixel(-y+x0,x+y0,color);
pDC->SetPixel(-y+x0,-x+y0,color);
pDC->SetPixel(y+x0,-x+y0,color);
}
pDC->TextOut(220,80,"中点算法画圆");
ReleaseDC(pDC);
}
2-2 Bresenham算法画圆
void CMyView::OnBresenhamcircle()
{
// TODO: Add your command handler code here
CDC *pDC=GetDC();
int x0=100,y0=100,x,y,r=80,color=0;
float e,d;
e=3-2*r;x=0;y=r; //e为判别式
while(x<=y)
{
if(e<0) //即选择当前像素的正右方作为下一像素
{e=e+4*x+6;x++;}
else //即选择当前像素的右下方作为下一像素
{
e=e+4*(x-y)+10;x++;y--;
}
pDC->SetPixel(x+x0,y+y0,color);
pDC->SetPixel(-x+x0,y+y0,color);
pDC->SetPixel(-x+x0,-y+y0,color);
pDC->SetPixel(x+x0,-y+y0,color);
pDC->SetPixel(y+x0,x+y0,color);
pDC->SetPixel(-y+x0,x+y0,color);
pDC->SetPixel(-y+x0,-x+y0,color);
pDC->SetPixel(y+x0,-x+y0,color);
}
pDC->TextOut(50,50,"Bresenham算法画圆");
ReleaseDC(pDC);
}
以上两种画圆算法最终实现的效果截图如下: