else { h+=-(2*dx*dy)-(2*dy*dy); x++; } putpixel(x,y,color); y--; } } } return 0; }
main() { int x1,y1,x2,y2; char c; int color; int gdriver=DETECT,gmode; here: //color=6; //printf(\ printf(\请输入起点坐标:\ scanf(\ printf(\请输入终点坐标:\ scanf(\ printf(\请输入画笔颜色:\ scanf(\ initgraph(&gdriver,&gmode,\ Bresenham_line(0,240,640,240,255); Bresenham_line(320,0,320,480,255); Bresenham_line(x1+320,y1+240,x2+320,y2+240,color); getch(); printf(\ c=getch(); if(c=='y'||c=='Y') goto here; }
2.4.2Bresenham画圆
//Bresenham画圆算法 #include \#include
- 16 -
void Bresenham_Circle(int r,int a,int b,int color) { int x,y,delta,delta1,delta2,direction; x=0; y=r; delta=2*(1-r); while(y>=0) { putpixel(a+x,b+y,color); putpixel(a-x,b+y,color); putpixel(a+x,b-y,color); putpixel(a-x,b-y,color); if(delta<0) { delta1=2*(delta+y)-1; if(delta1<=0) direction=1; else direction=2; } else if(delta>0) { delta2=2*(delta-x)-1; if(delta2<=0) direction=2; else direction=3; } else direction=2; switch(direction) { case 1: { x++; delta+=2*x+1; break; } case 2: { x++; y--; delta+=2*(x-y+1); break; } case 3: {
- 17 -
y--; delta+=(-2*y+1); break; } default: break; } } }
void BoundaryFill4(int x,int y,int boundarycolor,int newcolor) { int color; color=getpixel(x,y);
if(color!=newcolor && color!=boundarycolor) { putpixel(x,y,newcolor); BoundaryFill4 (x,y+1, boundarycolor,newcolor); BoundaryFill4 (x,y-1, boundarycolor,newcolor); BoundaryFill4 (x-1,y, boundarycolor,newcolor); BoundaryFill4 (x+1,y, boundarycolor,newcolor); } }
void main() { int r,color,a,b,ncolor; int gdriver=DETECT,gmode; char m[100]={0}; printf(\请输入圆的半径r:\ scanf(\ printf(\请输入圆心坐标:\ scanf(\ printf(\请输入边界颜色和填充颜色:\ scanf(\ initgraph(&gdriver,&gmode,\ Bresenham_Circle(r,a,b,color); BoundaryFill4(a,b,color,ncolor); sprintf(m,\ outtextxy(a,b,m);
- 18 -
}
getch(); closegraph();
2.5运行结果图
图2.5.1 Bresenham画线算法当k>1时运行结果截图
图2.5.2 Bresenham画线算法当k>1时运行结果截图
图2.5.3 Bresenham画线算法当1>k>0时运行结果截图
- 19 -