C – Digital Differential Analyzer Program in C

Digital Differential Analyzer Program in C

The digital differential analyzer ( DDA ) is a scan-conversion line algorithm based on calculating either dx or dy, using the equations dy=m*dx and dx=dy/m.

In this algorithm, the line is sampled at unit intervals in one coordinate and determine corresponding integer values nearest the path for another coordinate.

If slope m<1, we sample at unit x intervals ( dx=1) and compute each successive y value as Yk+1=Yk+m. If m>1 we reverse the role of x andy.

We sample at unity intervals and calculate each succeeding x value as Xk+1=Xk+1/m.

Algorithm

  1. Input the values of ( x1,y1 ) and ( x2,y2 ) . 
  2. Compute the slope m=dy/dx=( y2-y1 )/( x2-x1 ) . 
  3. If m<1, increment x 1by 1.then y1=y1+m. 
  4. Plot ( x1 , y1 ). 
  5. Repeat the step3,4 till x1<=x2 . 
  6. If m>1, increment y1 by 1 and x1=x1+1/m 
  7. Plot (x1, y1 ) . 
  8. Repeat the sep 6,7 till y1<=y2 .

Source Code

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
int gd=DETECT,gm;
float i,x1,y1,x2,y2,dy,dx,m;
initgraph(&gd,&gm,"c:\tc\bgi");
printf("Direct line drawing algorithmn");
printf("put the values of x1 and y1n");
scanf("%f %f",&x1,&y1);
printf("put the values of x2 and y2n");
scanf("%f %f",&x2,&y2);
dx=x2-x1;
dy=y2-y1;
m=dy/dx;
if(m<=1)
{
for(i=1;i<=x2;i++)
{
x1=x1+1;
y1=y1+m;
putpixel(x1,y1,RED);
}
}
if(m>1)
{
while(y1<=y2)
{
x1=x1+1/m;
y1=y1+1;
putpixel(x1,y1,RED);
}
}
getch();
}