Program code to draw a line using DDA in C

#include<stdio.h>
#include<conio.h>
#include<graphics.h>

int round(float x)
{
int p;
p=x;
return p;
}

void main()
{
int gd=DETECT,gm,x1,y1,dx,dy,x2,y2,length,i,x,y;
initgraph(&gd,&gm,"C:\\TC\\BGI");

printf("Enter the starting points (x,y) : ");
scanf("%d %d",&x1,&y1);
printf("Enter the ending points (x,y) : ");
scanf("%d %d",&x2,&y2);
if(abs(x2-x1)>=abs(y2-y1))
length=abs(x2-x1);
else
length=abs(y2-y1);
i=1;
dx=(x2-x1)/length;

dy=(y2-y1)/length;
x=x1;
y=y1;
while(i<=length)
{
putpixel(round(x),round(y),2);
//x1=x1+dx;
y=y+dy;
i++;
}
i=1;
x=x1;
y=y1;
while(i<=length)
{
putpixel(round(x),round(y),2);
x=x+dx;
//y1=y1+dy;
i++;
}
i=1;
x=x1;
y=y1;
while(i<=length)
{
putpixel(round(x),round(y),3);
x=x+dx;
y=y+dy;
i++;
}


getch();
closegraph();
}




Comments

Popular posts from this blog

Pascal Triangle using 2D array

JAVA program to add two distance