直线DDA算法的C语言实现

注:输入线段起点和终点的x,y坐标,程序将生成直线,

每生成10个点暂停显示一下当前x,y坐标位置.

 

 
   #include<graphics.h>
void main()
{
int driver,mode;
int xa,ya,xb,yb,c;
int dx,dy,steps,k;
float fx,fy,x,y;
int wx,wy;
driver=DETECT;
mode=0;
initgraph(&driver,&mode,"");
printf("please enter the start point:/n");
printf(" xa= ");
scanf("%d",&xa);
printf(" ya= ");
scanf("%d",&ya);
printf("please enter the end point:/n");
printf(" xb= ");
scanf("%d",&xb);
printf(" yb= ");
scanf("%d",&yb);

dx=xb-xa;
dy=yb-ya;
if(abs(dx)>abs(dy))
steps=abs(dx);
else
steps=abs(dy);
fx=(float)dx/(float)steps;
fy=(float)dy/(float)steps;
x=xa;
y=ya;
putpixel(x,y,RED);
for(k=1;k<=steps;k++)
{
x+=fx;
y+=fy;
putpixel(x,y,RED);

if(k%10==0)
{
printf(" xc=%.2f yc=%.2f/n",x,y);
getch();
}

}
}
 
     

你可能感兴趣的:(c,算法,语言,float)