中点画圆法
理论 :
如果我们构造函数 F(x,y)=x2+y2-R2,则对于圆上的点有 F(x,y)=0,对于圆外的
点有 F(x,y)>0,对于圆内的点 F(x,y)<0 。与中点画线法一样,构造判别式:
d=F(M)=F(xp+1,yp-0.5)=(xp+1)
2+(yp-0.5)
2-R2
若 d<0,则应取 P1为下一象素,而且再下一象素的判别式为:
d=F(xp+2,yp-0.5)=(xp+2)
2+(yp-0.5)
2-R2=d+2xp+3
若 d≥0,则应取 P2为下一象素,而且下一象素的判别式为
d=F(xp+2,yp-1.5)=(xp+2)
2+(yp-1.5)
2-R2=d+2(xp-yp)+5
我们这里讨论的第一个象素是(0,R),判别式 d的初始值为:
d0=F(1,R-0.5)=1.25-R
code:
/*
this is a test drawing a circle ;
made by lihe
2007 12
*/
#include "graphics.h"
void CirclePoints(int center_x,int center_y,int x,int y,int color)
{
putpixel(center_x+x, center_y+y, color);
putpixel(center_x+y, center_y+x, color);
putpixel(center_x-x, center_y+y, color);
putpixel(center_x+y, center_y-x, color);
putpixel(center_x+x, center_y-y, color);
putpixel(center_x-y, center_y+x, color);
putpixel(center_x-x, center_y-y, color);
putpixel(center_x-y, center_y-x, color);
}
void MidPointCircle(int center_x,int center_y,int r,int color)
{
int x,y;
float d;
x=0;
y=r;
d=1.25-r;
CirclePoints (center_x,center_y,x,y,color);
while(x<=y)
{
x++;
if(d<0)
{
d+=2*x+3;
}
else
{
d+=2*(x-y)+5;
y--;
}
CirclePoints(center_x,center_y,x,y,color);
}
}
void initgr(void)
{
int gd = DETECT, gm = 0;
registerbgidriver(EGAVGA_driver);
initgraph(&gd, &gm, "");
}
int main(void)
{
int x,y,r,color;
initgr();
printf("please enter the coordinate x y,and the radius and the color \n " );
scanf("%d %d %d %d",&x,&y,&r,&color);
MidPointCircle(x,y,r,color);
getch();
closegraph();
return 0;
}