0% found this document useful (0 votes)
57 views7 pages

Computer Graphics 3

Computer Graphics Lab File Part 2

Uploaded by

Shubham Kamat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views7 pages

Computer Graphics 3

Computer Graphics Lab File Part 2

Uploaded by

Shubham Kamat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

COMPUTER GRAPHICS

Course Code: BTCS2401

Lab File

For

BACHELOR OF COMPUTER SCIENCE AND


ENGINEERING

Engineering & Technology

SCHOOL OF COMPUTING SCIENCE AND ENGINEERING

GALGOTIAS UNIVERSITY, GREATER NOIDA

UTTAR PRADESH

Student Name: CHAITANYA KUMAR SAXENA

Admission No: 20SCSE1290080

Semester : IV / WINTER 2020/21

1|Page
EXP. NO. : 3 STUDENT NAME: CHAITANYA SAXENA

DATE: 04/02/2022 ADMISSION NO. :20SCSE1290081

TITLE OF EPERIMENT: Implement the DDA algorithm for


drawing lines

AIM:- Write a program to generate a line using DDA algorithm.

PROGRAM :

1. DESCRIPTION (INTRODUCTION)
DDA stands for Digital Differential Analyzer. It is an incremental method of
scan conversion of line. In this method calculation is performed at each step but
by using results of previous steps.

Suppose at step i, the pixels is (xi,yi)


The line of equation for step i
yi=mxi+b......................equation 1
Next value will be
yi+1=mxi+1+b.................equation 2

m= yi+1-
yi=∆y.......................equation 3
yi+1-xi=∆x......................equation 4

2|Page
yi+1=yi+∆y ∆y=m∆x
yi+1=yi+m∆x ∆x=∆y/m
xi+1=xi+∆x
xi+1=xi+∆y/m
Case1: When |M|<1 then (assume that x1<x2)
x= x1,y=y1 set ∆x=1

yi+1=y1+m, x=x+1

Until x = x2</x

Case2: When |M|<1 then (assume that y1<y2)

x= x1,y=y1 set ∆y=1

xi+1= , y=y+1

Until y → y2</y Advantage:

1. It is a faster method than method of using direct use of line equation.

2. This method does not use multiplication theorem.

3. It allows us to detect the change in the value of x and y ,so plotting of same point twice is not possible.

4. This method gives overflow indication when a point is repositioned.

5. It is an easy method because each step involves just two additions.

Disadvantage:

1. It involves floating point additions rounding off is done. Accumulations of round off error cause accumulation

of error.

2. Rounding off operations and floating point operations consumes a lot of time.

3. It is more suitable for generating line using the software. But it is less suited for hardware implementation.

DDA Algorithm:

Step1: Start Algorithm

Step2: Declare x1,y1,x2,y2,dx,dy,x,y as integer variables.

3|Page
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. How many points will needed to generate 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=

For calculating next value of x takes x = x +

4|Page
5|Page
2. SOURCE CODE –
#include<graphics.h>
#include<conio.h>
#include<stdio.h> int
main()
{
int gd = 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);

6|Page
x += dx;
y += dy;
i=i+1;
}
getch();
closegraph();

return 0;
}
3. OUTPUT -

7|Page

You might also like