#include <graphics.h>
#include <conio.h>
#include <math.h>
#include <iostream.h>
void dda(float x1, float y1, float x2, float y2)
{
float dx, dy, steps;
float xinc, yinc;
float x, y;
int i;
dx = x2 - x1;
dy = y2 - y1;
if (fabs(dx) > fabs(dy))
steps = fabs(dx);
else
steps = fabs(dy);
xinc = dx / steps;
yinc = dy / steps;
x = x1;
y = y1;
putpixel((int)(x + 0.5), (int)(y + 0.5), WHITE);
for (i = 0; i < steps; i++)
{
x = x + xinc;
y = y + yinc;
putpixel((int)(x + 0.5), (int)(y + 0.5), WHITE);
}
}
int main()
{
int gd = DETECT, gm;
float x1, y1, x2, y2;
initgraph(&gd, &gm, "c:\\turboc3\\bgi");
cout << "Enter x1,y1:";
cin >> x1 >> y1;
cout << "Enter x2,y2:";
cin >> x2 >> y2;
dda(x1, y1, x2, y2);
outtextxy(10, 200, (char*)"DDA line drawing algorithm");
getch();
closegraph();
return 0;
}8 views