DDA LINE DRAWING PROGRAM
DDA Algorithm
Digital Differential Analyzer (DDA) algorithm is the simple line generation algorithm which is explained step by step here.
Step 1 − Get the input of two end points and .
Step 2 − Calculate the difference between two end points.
dx = X1 - X0 dy = Y1 - Y0
Step 3 − Based on the calculated difference in step-2, you need to identify the number of steps to put pixel. If dx > dy, then you need more steps in x coordinate; otherwise in y coordinate.
if (dx > dy) Steps = absolute(dx); else Steps = absolute(dy);
Step 4 − Calculate the increment in x coordinate and y coordinate.
Xincrement = dx / (float) steps; Yincrement = dy / (float) steps;
Step 5 − Put the pixel by successfully incrementing x and y coordinates accordingly and complete the drawing of the line.
for(int v=0; v < Steps; v++) { x = x + Xincrement; y = y + Yincrement; putpixel(x,y); }
Program
#include <iostream.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>
#include <graphics.h>
#include <math.h>
#include <dos.h>
void main(){
float xnew,ynew,x1,y1,x2,y2,dx,dy,length;
void main(){
float xnew,ynew,x1,y1,x2,y2,dx,dy,length;
int i, gd= DETECT, gm;
clrscr();
cout<<“Enter the value of x1 and y1: \t“;
cin>>x1>>y1;
cout<<“Enter the value of x2 and y2: \t“;
cin>>x2>>y2;
cout<<“Enter the value of x1 and y1: \t“;
cin>>x1>>y1;
cout<<“Enter the value of x2 and y2: \t“;
cin>>x2>>y2;
initgraph(&gd,&gm,”C:\TC\BGI”);
dx = abs(x2-x1);
dy = abs(y2-y1);
if(dx>=dy)
dy = abs(y2-y1);
if(dx>=dy)
{
length = dx;
}
else
else
{
length = dy;
}
dx = (x2-x1)/length;
dy = (y2-y1)/length;
xnew=x1+0.5;
dx = (x2-x1)/length;
dy = (y2-y1)/length;
xnew=x1+0.5;
ynew=y1+0.5;
i=1;
while(i<=length){
putpixel(xnew,ynew,RED);
xnew = xnew + dx;
ynew = ynew + dy;
i++;
}
xnew = xnew + dx;
ynew = ynew + dy;
i++;
}
getch();
closegraph();
}
