#include<stdio.h>
#include<graphics.h>
void MidPointCircle(int r,int color);
void CirclePoints(int x,int y,int color);
void main()
{
int gdriver=DETECT, gmode;
initgraph(&gdriver, &gmode," ");
setbkcolor(BLACK);
MidPointCircle(100,BLUE); /*下次画圆半径大点,你给的半径真夸张*/
getch();
closegraph();
}
void MidPointCircle(int r,int color)
{
int x,y;
float d;
x=0;
y=r;
d=1.25-r;
CirclePoints(x,y,color);
while(x<y)
{
if(d<0)
d+=2*x+3;
else
{
d+=2*(x-y)+5;
y--;
}
x++;
CirclePoints(x,y,color); /*重点是这里*/
}
}
void CirclePoints(int x,int y,int color) /*圆心为(320,240)*/
{
putpixel(320+x,240+y,color);
putpixel(320+y,240+x,color);
putpixel(320-x,240+y,color);
putpixel(320+y,240-x,color);
putpixel(320+x,240-y,color);
putpixel(320-y,240+x,color);
putpixel(320-x,240-y,color);
putpixel(320-y,240-x,color);
}
数值微分法:
int DDALine (int x0,int y0,int x1,int y1,int color)
{
int coordinate;
float dx=x1-x0/*X方向的增量*/,
dy=y1-y0/*Y方向的增量*/;
float eXY;
float coorXY;
if(dx==0) /*X方向的增量为0*/
{
coorXY=x0;
if(dy>=0) /*Y方向的增量为正方向*/
{
for(coordinate=y0;coordinate<=y1;coordinate++)
putpixel(coorXY,coordinate,color);
}
else /*Y方向的增量为负方向*/
{
for(coordinate=y0;coordinate>=y1;coordinate--)
putpixel(coorXY,coordinate,color);
}
}
else if(dy/dx<=1 && dy/dx>=-1) /*|dy/dx|<=1*/
{
/*X方向的增量为+1/-1,Y方向随之增量为+eXY/-eXY*/
coorXY=y0; /*Y方向的初始值为y0*/
if(dx>=0)
{
eXY=dy/dx; /*当dy>=0时,eXY=dy/dx>=0,
当dy<0 时,eXY=dy/dx<0*/
for(coordinate=x0;coordinate<=x1;coordinate++)
{
putpixel(coordinate,(int)(coorXY),color);
coorXY+=eXY; /*Y方向增量为eXY*/
}
}
else
{
eXY=-dy/dx; /*当dy>=0时,eXY=-dy/dx>=0,
当dy<0 时,eXY=-dy/dx<0*/
for(coordinate=x0;coordinate>=x1;coordinate--)
{
putpixel(coordinate,(int)(coorXY),color);
coorXY+=eXY; /*Y方向增量为eXY*/
}
}
}
else /*|dy/dx|>1*/
{
coorXY=x0; /*X方向的初始值为y0*/
if(dy>=0)
{
eXY=dx/dy; /*当dx>=0时,eXY=dx/dy>=0,
当dx< 0时,eXY=dx/dy<0*/
for(coordinate=y0;coordinate<=y1;coordinate++)
{
putpixel((int)(coorXY),coordinate,color);
coorXY+=eXY; /*X方向增量为eXY*/
}
}
else
{
eXY=-dx/dy; /*当dx>=0时,eXY=-dx/dy>=0,
当dx< 0时,eXY=-dx/dy<0*/
for(coordinate=y0;coordinate>=y1;coordinate--)
{
putpixel((int)(coorXY),coordinate,color);
coorXY+=eXY; /*X方向增量为eXY*/
}
}
}
return 0; /*返回值为0*/
}