以下是 Bresenham 的直线生成算法和整圆生成算法,已调试过,没有任何问
题。
Bresenham 直线生成算法
#include "stdio.h"
#include "graphics.h"
Bresenham_line(x0,y0,x1,y1,color)
int x0,y0,x1,y1,color;
{
int x,y,dx,dy, i; float k,e;
dx=x1-x0;dy=y1-y0;
k=(dy*1.0)/dx; e=-0.5; x=x0; y=y0;
for (x=x0; x<=x1; x++)
{
putpixel(x,y,color);
e=e+k;
if(e>=0)
{ y++;e=e-1;}
}
}
int main()
{
int x0,y0,x1,y1,c;
int driver=DETECT,mode=0;
initgraph(&driver,&mode,"c:\tc");
setbkcolor(BLUE);
setcolor(YELLOW);
printf("input x0,y0,x1,y1,c");
scanf("%d%d%d%d%d",&x0,&y0,&x1,&y1,&c);
Bresenham_line(x0,y0,x1,y1,c);
getch();
closegraph();
}
当取 e=2*dy-dx 时,可以消除浮点和除法运算
#include "stdio.h"
#include "graphics.h"