DDA Line Drawing Algorithm
In computer graphics, popular algorithms used to generate lines are-
- Digital Differential Analyzer (DDA) Line Drawing Algorithm
- Bresenham Line Drawing Algorithm
- Mid Point Line Drawing Algorithm
In this article, we will discuss about DDA Algorithm.
DDA Algorithm:
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
Then step = abs (dx)
Else
Step7: xinc=dx/step
yinc=dy/step
assign x = x1
assign y = y1
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))
y = y + yinc
Set pixels (Round (x), Round (y))
Step10: Repeat step 9 until x = x2
Step11: End Algorithm
PROGRAM IN CPP::
#include <graphics.h>
#include <stdio.h>
#include <math.h>
#include <dos.h>
void main( )
{
float x,y,x1,y1,x2,y2,dx,dy,step;
int i,gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi"); //initializing graphics
printf("Enter the value of x1 and y1 : ");
scanf("%f%f",&x1,&y1);
printf("Enter the value of x2 and y2: ");
scanf("%f%f",&x2,&y2);
dx=abs(x2-x1); //calculating constants
dy=abs(y2-y1);
if(dx>=dy) //deciding value of length of line
step=dx;
else
step=dy;
dx=dx/step; //varying distance
dy=dy/step;
x=x1;
y=y1;
i=1;
while(i<=step)
{
putpixel(x,y,5); //ploting graph
x=x+dx; //deciding next point's x cordinate
y=y+dy; //deciding next point's y cordinate
i=i+1;
delay(100);
}
closegraph();
}
Post a Comment