我的代码在slope=1上工作正常,但在其他坡度上就不行了。它为非1的坡度绘制水平线或垂直线。此code.Any帮助的错误之处将不胜感激。
#include <graphics.h>
#include <stdio.h>
#include <math.h>
int main( )
{
int x,y,x1,y1,x2,y2,dx,dy;
float step;
int i,gd,gm;
printf("Enter the value of x1,y1: ");
scanf("%f%f",&x1,&y1);
printf("Enter the value of x2,y2 : ");
scanf("%f%f",&x2,&y2);
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"");
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dx>=dy)
step=dx;
else
step=dy;
dx=dx/step;
dy=dy/step;
x=x1;
y=y1;
i=1;
while(i<=step)
{
putpixel(x,y,1);
x=x+dx;
y=y+dy;
i=i+1;
delay(100);
}
getch();
}发布于 2013-08-01 02:07:23
dx=dx/step;
dy=dy/step;您已将step设置为浮点数,但dx和dy是整数。因此,这个除法将在这两个值中的一个中给出一个0。我的印象是DDA例程都是整型的,所以其中的浮点数让我感到疑惑。我将更深入地研究算法,看看我还能找到什么。
Here's是一个例程,它以一种不会将步骤置零的方式使用浮点数。
和windows版的another。
发布于 2013-08-01 02:10:35
看起来您只接受了
scanf("%f%f",&x1);
scanf("%f%f",&y1);语句。尝试更正该错误,然后再次运行代码。
https://stackoverflow.com/questions/17977479
复制相似问题