#include <stdio.h>
#include <conio.h>
#include<math.h>
main()
{
int r,q,w,color;
int gdriver=DETECT,gmode; /*設置圖形驅動*/
int Bresenham_Ciecle(); /*定義子函數*/
printf("please input the centre of a circle x0,y0\n");
scanf("%d,%d",&q,&w);
if(q>=320||q<=-320||w<=-250||w>=250)
{printf("please input the centre of a circle again x0,y0\n");
scanf("%d,%d",&q,&w);} /*輸入圓心位置越界輸出信息*/
printf("please input the numble of radius r=");
scanf("%d",&r);
if(r<=0||r>=500)
{
printf("r is error,r>0,please input the numble of r=");
scanf("%d",&r);
}/*輸入半徑*/
printf("please input the numble of color=");
scanf("%d",&color);
initgraph(&gdriver,&gmode,"D:\\TC");
setcolor(color);/*設置圖形顏色*/
Bresenham_Ciecle(q,w,r,color); /*繪圖*/
getch();
}
Bresenham_Ciecle(int q,int w,int r,int color)
{
int x,y,t,v,delta,delta1,delta2,direction;
char buffera[20];
char bufferb[20];
t=getmaxx()/2;
v=getmaxy()/2; /*選定圓心*/
sprintf(buffera, "(%d,%d)", q,w); /*打印圓心坐標*/
sprintf(bufferb, "(0,0) R=%d",r); /*打印原點坐標及半徑*/
moveto(t,v+4);
outtext(bufferb); /*圓點坐標定位輸出*/
q=q+t;w=v-w;
moveto(q,w);
outtext(buffera);/*原點坐標定位輸出*/
line(1,v,2*t,v);
line(t,2*v,t,1);/*畫坐標*/
x=q; y=r+w;
line(q, w, x, y); /*畫半徑*/
delta=2*(1-r);
while(y-w>=0)
{
putpixel(x,y,color); /*右下方1/4個圓*/
putpixel(2*q-x,y,color);/*右上方1/4個圓(從右下方1/4個圓對稱復制)*/
putpixel(x,2*w-y,color);/*左下方1/4個圓(從右下方1/4個圓對稱復制)*/
putpixel(2*q-x,2*w-y,color);/*左上方1/4個圓(從右下方1/4個圓對稱復制)*/
if(delta<0)
{
delta1=2*(delta+y-w)-1;
if(delta1<=0) direction=1;
else direction=2;
}
else if(delta>0)
{
delta2=2*(delta-x+q)-1;
if(delta2<=0) direction=2;
else direction=3;
}
else
direction=2;
switch(direction)
{
case 1: x++;
delta+=2*(x-q)+1;
break;
case 2: x++;
y--;
delta+=2*(1+x-y-q+w);
break;
case 3: y--;
delta+=-2*(y-w)+1;
break;
}
}
}