Bresenham的直线生成算法和整圆生成算法完整代码

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

以下是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"

Bresenham_line(x0,y0,x1,y1,color)

int x0,y0,x1,y1,color;

{

int x,y,dx,dy, i,e; float k;

dx=x1-x0;dy=y1-y0;

k=(dy*1.0)/dx; e=2*dy-dx; x=x0; y=y0;

for (x=x0; x<=x1; x++)

{

putpixel(x,y,color);

e=e+2*dy;

if(e>=0)

{ y++;e=e-2*dx;}

}

}

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();

}

Bresenham整圆生成算法

#include "stdio.h"

#include "graphics.h"

void circlePoints(int x0,int y0,int x,int y,int color) {

putpixel(x+x0,y+y0,color);

putpixel(x+x0,-y+y0,color);

putpixel(y+x0,x+y0,color);

putpixel(y+x0,-x+y0,color);

putpixel(-x+x0,-y+y0,color);

putpixel(-x+x0,y+y0,color);

putpixel(-y+x0,-x+y0,color);

putpixel(-y+x0,x+y0,color);

}

Bresenhamcircle (int x0,int y0,int r)

{

int x,y,d,color=15;

d=3-2*r;

x=0;y=r; /*从(0,r)开始画圆*/?

circlePoints(x0,y0,x,y,color);

while(x

{

if(d<0){d=d+4*x+6;x++;}

else{d= d+4*(x-y)+10;x++;y--;}

circlePoints(x0,y0,x,y,color);

}

}

main()

{

int r,x0,y0;

int driver=DETECT,mode=0;

initgraph(&driver,&mode,"c:\\tc");

setbkcolor(BLUE);

setcolor(YELLOW);

printf("input x0,y0,r\n");

scanf("%d%d%d",&x0,&y0,&r);

Bresenhamcircle(x0,y0,r);

getch();

closegraph();

}

相关文档
最新文档