Aneeket.15114802719@cse - Mait.ac: Experiment 2

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

[email protected].

ac
Experiment 2
AIM :- Implementation of DDA line Algorithm

THEORY :- DDA Algorithm is the simplest line drawing algorithm. Given the
starting and ending coordinates of a line, DDA Algorithm attempts to generate the
points between the starting and ending coordinates. In any 2-Dimensional plane if
we connect two points (x0, y0) and (x1, y1), we get a line segment. But in the case of
computer graphics we can not directly join any two coordinate points, for that we
should calculate intermediate point’s coordinate and put a pixel for each intermediate
point, of the desired colour with help of functions like putpixel(x, y, K) in C, where (x,
y) is our co-ordinate and K denotes some colour.
A DDA (Digital Differential Analyzer) algorithms is a scan-conversion method
for drawing a line which follows an incremental approach. In this algorithm to draw a
line the difference in the pixel points is analysed then according to that the line is
drawn. The method is said to be incremental because it performs computations at
each step and uses the outcome of the previous step.

SOURCE CODE:-

HUT :

#include<stdio.h>

#include<graphics.h>

#include<conio.h>

dda(int x0, int y0, int x1, int y1){

int I;

float x,y,dx,dy,steps;

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, BLUE);

x += dx;

y += dy;

i = i+1;

main() {

initwindow(800,800);

dda(100,100,200,200);

dda(100,100,325,100);
dda(200,200,400,200);

dda(325,100,400,200);

dda(200,200,20,200);

dda(20,200,100,100);

dda(400,200,400,400);

dda(20,400,400,400);

dda(20,200,20,400);

dda(200,200,200,400);

dda(65,290,65,400);

dda(150,290,150,400);

dda(65,290,150,290);

getch();

closegraph();

Output :-

 
    

Viva question and answer.

1) Define getpixel() and putpixel() function in graphics.h?


Answer:-

Function getpixel() returns the color of pixel present at point(x, y).

Function putpixel() plots a pixel at a point(x, y) of the specified color.

2) What is the difference between DDA Algorithm and


Bresenham’s Algorithm?
Answer:-
1. Bresenham’s algorithm is more efficient and accurate than DDA
algorithm.
2. The DDA algorithm involves floating point values while in
bresenham algorithm only integer values is included. This is the
major reason that made the computations in DDA difficult than the
bresenham algorithm.
3. DDA uses multiplication and division operations. As against,
bresenham involves addition and subtraction causing less
consumption of time. Therefore, DDA is slower than bresenham.
4. The values in DDA never rounded off. In contrast, bresenham
rounds off the value to the closest integer value.
5. Bresenham algorithm is optimized. Conversely, DDA is not and less
expensive.

3. Give disadvantages of DDA algorithm?


Answer:-
1. There is an extra overhead of using round off( ) function.
2. Using round off( ) function increases time complexity of the algorithm.
3. Resulted lines are not smooth because of round off( ) function.
4. The points generated by this algorithm are not accurate .

4. Give disadvantages of Bresenham’s algorithm?


Answer:-
1. Though it improves the accuracy of generated points but still the resulted line
is not smooth.
2. This algorithm is for the basic line drawing.
3. It can not handle diminishing jaggies.

You might also like