#include "graphics.h"
#include <stdio.h>
#include <math.h>
#include <process.h>
/* she ru */
int Round(float a)
{
return (int)(a + 0.5);
}
/* DDA */
void LineWithDDA(int xStart,int yStart,int xEnd,int yEnd)
{
int dx =xEnd - xStart,dy =yEnd - yStart,steps,k;
float xIn, yIn, x = xStart, y = yStart;
if(fabs(dx) > fabs(dy))
{
steps = fabs(dx);
}
else
{
steps = fabs(dy);
}
xIn = (float)dx / (float)steps;
yIn = (float)dy / (float)steps;
putpixel(Round(x),Round(y),2);
for(k=0;k<steps;k++)
{
x+=xIn;
y+=yIn;
putpixel(Round(x),Round(y),255);
}
getchar();
}
void main()
{
int gdriver ,gmode;
gdriver = DETECT;
int x1,x2,y1,y2;
printf("请输入第一个端点坐标:\n");
scanf("%d%d",&x1,&y1);
printf("请输入第二个端点坐标:\n");
scanf("%d%d",&x2,&y2);
initgraph(&gdriver,&gmode,""); /* 图形系统初始化 */
setbkcolor(15);
LineWithDDA(x1,y1,x2,y2);
setcolor(255);
outtextxy(200,30,"DDA直线算法");
getchar();
getchar();
//printf("BESONHAM算法:\n");
//LineWithBresenham_One(x1,y1,x2,y2);
closegraph ( ); /*关闭图形系统,返回文本方式 */
}