Practical No 3:-Write A Program in C To Implement Bresenham's Line Drawing and DDA Digital Differential Analyzer (DDA)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Practical No 3:- Write a program in C to implement Bresenham’s line drawing and DDA

algorithm, and compare the results with each other.


Digital Differential Analyzer (DDA):-
DDA (Digital Differential Analyzer) is a line drawing algorithm used in computer graphics to
generate a line segment between two specified endpoints. It is a simple and efficient
algorithm that works by using the incremental difference between the x-coordinates and y-
coordinates of the two endpoints to plot the line.

// C program for DDA line drawing algorithm


#include<graphics.h>
#include<conio.h>
#include<stdio.h>
void main()
{
intgd = DETECT ,gm, i;
float x, y,dx,dy,steps;
int x0, x1, y0, y1;
initgraph(&gd, &gm, "C:\\TC\\BGI");
setbkcolor(WHITE);
x0 = 100 , y0 = 200, x1 = 500, y1 = 300;
dx = (float)(x1 - x0);
dy = (float)(y1 - y0);
if(dx>=dy)
{
steps = dx;
}
else
{
steps = dy;
}
dx = dx/steps;
dy = dy/steps;
x = x0;
y = y0;
i = 1;
while(i<= steps)
{
putpixel(x, y, RED);
x += dx;
y += dy;
i=i+1;
}
getch();
closegraph();
}
Output:

Advantages of DDA Algorithm:


• It is a simple and easy-to-implement algorithm.
• It avoids using multiple operations which have high time complexities.
• It is faster than the direct use of the line equation because it does not use any floating
point multiplication and it calculates points on the line.
Disadvantages of DDA Algorithm:
• It deals with the rounding off operation and floating point arithmetic so it has high
time complexity.
• As it is orientation-dependent, so it has poor endpoint accuracy.
• Due to the limited precision in the floating point representation, it produces a
cumulative error.

Bresenham's Line Drawing Algorithm


This algorithm is used for scan converting a line. It was developed by Bresenham. It is an
efficient method because it involves only integer addition, subtractions, and multiplication
operations. These operations can be performed very rapidly so lines can be generated quickly.

// C program for Bresenham’s line drawing algorithm


#include<stdio.h>
#include<graphics.h>
void drawline(int x0, int y0, int x1, int y1)
{
int dx, dy, p, x, y;
dx=x1-x0;
dy=y1-y0;
x=x0;
y=y0;
p=2*dy-dx;
while(x<x1)
{
if(p>=0)
{
putpixel(x,y,7);
y=y+1;
p=p+2*dy-2*dx;
}
else
{
putpixel(x,y,7);
p=p+2*dy;}
x=x+1;
}
}
int main()
{
int gdriver=DETECT, gmode, error, x0, y0, x1, y1;
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
printf("Enter co-ordinates of first point: ");
scanf("%d%d", &x0, &y0);
printf("Enter co-ordinates of second point: ");
scanf("%d%d", &x1, &y1);
drawline(x0, y0, x1, y1);
return 0;
}
Output:
Advantages of Bresenham’s line drawing algorithm:
1. It involves only integer arithmetic, so it is simple.
2. It avoids the generation of duplicate points.
3. It can be implemented using hardware because it does not use multiplication and division.
4. It is faster as compared to DDA (Digital Differential Analyzer) because it does not involve
floating point calculations like DDA Algorithm.

Disadvantage of Bresenham’s line drawing algorithm:


1. This algorithm is meant for basic line drawing only Initializing is not a part of Bresenham's
line algorithm. So to draw smooth lines, you should want to look into a different algorithm.

You might also like