DDA LINE DRAWING ALGORITHM
The digital deferential analyzer (DDA) may be a scan-conversion line algorithm supported calculating either ∆y or ∆x.
In this method calculation is performed at each step but by using results of previous steps.
The Algorithm as follows:
Step1: Start Algorithm
Step2: Declare x1,y1,x2,y2,dx,dy,x,y as integer variables.
Step3: Enter value of x1,y1,x2,y2.
Step4: Calculate dx = x2-x1
Step5: Calculate dy = y2-y1
Step6: If ABS (dx) > ABS (dy)
Then step = abs (dx)
Else
Step7: xinc=dx/step
yinc=dy/step
assign x = x1
assign y = y1
Step8: Set pixel (x, y)-
Step9: x = x + xinc
y = y + yinc
Set pixels (Round (x), Round (y))
Step10: Repeat step 9 until x = x2
Step11: End Algorithm
Example: If a line is drawn from (2, 3) to (6, 15) with use of DDA. what number points will needed to get such line?
Solution: P1 (2,3) P11 (6,15)
x1=2
y1=3
x2= 6
y2=15
dx = 6 - 2 = 4
dy = 15 - 3 = 12
m = dy/dx = 12/4
For calculating next value of x takes x = x + 1/m
C Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
main()
{
int gd=DETECT,gm;
int x1,y1,x2,y2,dx,dy,a1,a2,b1,b2,i;
float step,xinc,yinc,x,y;
char a[20],b[20],c[20],d[20];
clrscr();
initgraph(&gd,&gm," ");
printf("\nEnter the starting point");
scanf("%d%d",&x1,&y1);
printf("\nEnter the ending point");
scanf("%d%d",&x2,&y2);
cleardevice();
a1=x1;
a2=x2;
b1=y1;
b2=y2;
x1+=20;
x2+=20;
y1=470-y1;
y2=470-y2;
line(20,20,20,470);
line(20,470,590,470);
outtextxy(190,60,"**********DDA LINE DRAWING ALGORITHM**********");
sprintf(a,"%c%d%c%d%c",'(',a1,',',b1,')');
sprintf(b,"%c%d%c%d%c",'(',a2,',',b2,')');
outtextxy(15,15,"^y");
outtextxy(590,469,">x");
outtextxy(x1,y1,a);
outtextxy(x2,y2,b);
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
step=abs(dx);
else
step=abs(dy);
xinc=dx/step;
yinc=dy/step;
x=x1;
y=y1;
for(i=1;i<=step;i++)
{
putpixel(x,y,RED);
x=x+xinc;
y=y+yinc;
}
getch();
closegraph();
}
FOR OUTPUT CLICK HERE!!
Comments
Post a Comment