F How can we implement the DDA line algorithm using C ? | CodeTheta

How can we implement the DDA line algorithm using C ?

May 18, 2013

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
void dda(int, int, int, int);
void main()
{
int x1, y1, x2, y2;
int gd=DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
x1=y1=x2=y2=0;
printf("\nEnter Co-ordinates:\n");
scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
dda(x1, y1, x2, y2);
getch();
}
void dda(int x1, int y1, int x2, int y2)
{
float x=x1, y=y1, dx, dy;
int len, i;
putpixel(x1, y1, BLUE);
abs(x2-x1)>=abs(y2-y1)?(len=abs(x2-x1)):(len=abs(y2-y1));
dx=(float)(x2-x1)/len;
dy=(float)(y2-y1)/len;
for(i=1; i<=len; i++)
{
x+=dx;
y+=dy;
putpixel((int) x, (int) y, WHITE);
}
return;
}

Post a Comment