DDA Line Drawing Algorithm
Line Drawing - C++ implementation - Graphics & Multimedia Lab
Sourcecode:
//DDA LINE DRAWING ALGORITHM
#include<graphics.h>
#include<iostream>
using namespace std;
int gd=DETECT,gm,maxy,points[1000];
float dx,dy,m,tmp,x,y;
FILE *fp;
int a0,b0,a1,b1;
int i,count,np;
void DDALine(int x0, int y0, int x1, int y1)
{
i=0;
dx=x1-x0;
dy=y1-y0;
maxy=getmaxy();
if(dx==0)
{
for(y=y0;y<=y1;y++)
{
putpixel(x0,y,WHITE);
points[i]=x0;
points[i+1]=y;
i+=2;
}
}
else if(dy==0)
{
for(x=x0;x<=x1;x++)
{
putpixel(x,y0,WHITE);
points[i]=x0;
points[i+1]=y;
i+=2;
}
}
else if(dy<=dx)
{
m=dy/dx;
y=y0;
for(x=x0;x<=x1;x++)
{
putpixel(x,(int)(maxy-y),WHITE);
points[i]=x;
points[i+1]=(int)y;
i+=2;
y=y+m;
}
}
else
{
m=dx/dy;
x=x0;
for(y=y0;y<=y1;y++)
{
putpixel((int)x,maxy-y,WHITE);
points[i]=(int)x;
points[i+1]=y;
i+=2;
x=x+m;
}
}
}
main()
{
initgraph(&gd,&gm,NULL);
fp=fopen("linput.txt","r");
if(fp==NULL)
{
printf("eRROR !");
getch();
exit(0);
}
fscanf(fp,"%d",&a0);
fscanf(fp,"%d",&b0);
fscanf(fp,"%d",&a1);
fscanf(fp,"%d",&b1);
fclose(fp);
DDALine(a0,b0,a1,b1);
fp=fopen("loutput.txt","w");
fprintf(fp,"%s","Plotted points are:-\n");
count=i;
np=0;
for(i=0;i<count;i+=2)
{
fprintf(fp,"(%d, %d) ",points[i],points[i+1]);
np++;
if(np==5) { fprintf(fp,"\n"); np=0; }
}
fclose(fp);
getch();
closegraph();
return 0;
}
Output:
nn@linuxmint ~ $ g++ lab1.cpp -lgraph nn@linuxmint ~ $ ./a.out
"linput.txt"
18 22 70 40
Plotted points are:- (18, 22) (19, 22) (20, 22) (21, 23) (22, 23) (23, 23) (24, 24) (25, 24) (26, 24) (27, 25) (28, 25) (29, 25) (30, 26) (31, 26) (32, 26) (33, 27) (34, 27) (35, 27) (36, 28) (37, 28) (38, 28) (39, 29) (40, 29) (41, 29) (42, 30) (43, 30) (44, 30) (45, 31) (46, 31) (47, 32) (48, 32) (49, 32) (50, 33) (51, 33) (52, 33) (53, 34) (54, 34) (55, 34) (56, 35) (57, 35) (58, 35) (59, 36) (60, 36) (61, 36) (62, 37) (63, 37) (64, 37) (65, 38) (66, 38) (67, 38) (68, 39) (69, 39) (70, 39)






Great post thannk you
ReplyDeleteHi thanks for sharinng this
ReplyDelete