0% found this document useful (0 votes)
17 views11 pages

C59 Exp1

Uploaded by

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

C59 Exp1

Uploaded by

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

Course: Computer Graphics

Faculty: Prof. Pradnya Jadhav


LAB Manual
PART A
Experiment No.01
A.1 Aim:
Implement DDA and Bresenham Line Drawing algorithms
A.2 Prerequisite:
1. C Language

A.3 Outcome:
After successful completion of this experiment students will
be able to

Implement various line, circle, ellipse drawing algorithms.

A.4 Theory:
DDA algorithm:
Input to the function is two endpoints (x1,y1) and (x2,y2)
1. length =abs(x2-x1);
2. if (abs(y2-y1) > length) then length =abs(y2-y1);
3. xincrement (x2-x1) / length;
4. yincrement (y2-y1) / length;
5. x =x + 0.5; y =Y + 0.5;
6. for i 1 to length follow steps 7 to 9
7. plot (trunc(x),trunc(y));
8. x= x + xincrement ;
9. y =y + yincrement ;
10.stop.
Bresenham's Line Drawing Algorithm:
1. Input the two line endpoints and store the left endpoint in(x0,y0)
2. Load ( x0,y0 ) into the frame buffer; that is , plot the first point.
3. Calculate constants dx, dy,2 dy and 2 dy -2 dx , and obtain the starting value for
the decision parameter as:
p0 = 2 dy - dx
4. At each xk, the next point the line , starting at k=0, perform the following test:
If pk < 0 , the next point to plot is (xk + 1 ,yk ) and
pk+1 = pk + 2 dy
Otherwise ,the next point to plot is (xk + 1, yk +1) and
pk+1 = pk + 2 dy - 2 dx
5.Repeat step 4 dx times.

A.5 Procedure:

DDA Algorithm:

Digital Differential Analyzer (DDA) algorithm is the simple line generation algorithm.
Step 1 − Get the input of two end points (X0,Y0) and (X1,Y1).

Step 2 − Calculate the difference between two end points.


dx = X1 - X0
dy = Y1 - Y0

Step 3 − Based on the calculated difference in step-2, you need to identify the number of steps to
put pixel.
If dx > dy, then you need more steps in x coordinate; otherwise in y coordinate.

if (dx > dy) Steps = absolute(dx);


else Steps = absolute(dy);

Step 4 − Calculate the increment in x coordinate and y coordinate.


X increment = dx / (float) steps;
Y increment = dy / (float) steps;
Step 5 − Put the pixel by successfully incrementing x and y coordinates accordingly and
complete the drawing of the line.

for(int v=0; v < Steps; v++)


{
x = x + Xincrement;
y = y + Yincrement;
putpixel(x,y);
}

BRESENHAM’S Line Drawing Algorithm.

Assumption :
Y=mX+b
where b is the intercept cut by line at Y axis and m is the
slope of line
(0 <= m < 1)
Derivation :
Initially we have plotted a point (Xk , Yk) and increase
X by 1 we come to Xk+1.
Decision Parameter(Pk) - (Xk+1 , Yk+1)
- (Xk+1 , Yk)
D1 = Y – Yk
D1 = m(Xk+1)+b – Yk
D2 = Yk+1 – Y
D2 = Yk+1 – [m(Xk+1)+b]
D1-D2 = m(Xk+1) – Yk - Yk+1 + m(Xk+1) + 2b
D1-D2 = 2m(Xk+1) + 2b – Yk – Yk+1
D1-D2 = 2mXk + 2m – 2Yk + (2b-1)
Put m = dY/dX
(D1-D2)dX = 2Xk dY – 2Yk dX + 2dY + (2b-1)dX

Pk = 2Xk dY – 2Yk dX + C
Where C = 2dY + (2b-1)dX

Pk+1 = 2Xk+1 dY – 2Yk+1 dX + C


Pk+1-Pk = 2(Xk+1 - Xk)dY – 2(Yk+1 - Yk)dX
Pk+1 = Pk + 2dY – 2(Yk+1 - Yk)dx -------------------------- (A)
If Pk is negative (Pk < 0)
(D1-D2)dX < 0
Xk+1 – Xk = 1
Yk+1 – Yk = 0
Put these value in A
Pk+1 = Pk +2dy
This is the decision parameter for less than zero.
If P is positive (P>=0)

(D1-D2)dx >= 0
Xk+1 – Xk = 1
Yk+1 – Yk = 1
put these value in A

Pk+1 = Pk + 2(dY - dY)


This is the decision parameter for greater than zero.
Initial value of decision parameter (Xo , Yo)
Po = 2Xo dY – 2Yo dX + 2dY + (2b-1)dX -------------------------- (B)
Yo = mXo +b
Yo – mXo = b
Yo – dY/dX Xo = b
2Yo – 2Xo dY/dX = 2b
2Yo dx – 2Xo dY - dX = (2b-1)dX

Put the value of (2b-1)dX in B


Po = 2Xo dY – 2Yo dX + 2dY + 2Yo dx – 2Xo dY – dX
Po = 2dY – dX
This is the initial decision parameter.

PART B

Roll No. C-59 Name: Nikhil Girish Kindre


Class : SE Comps Batch :C3
Date of Experiment: 06 Aug 2024 Date of Submission
Grade :

B.1 Document created by the student:

DDA Algorithm:
BRESENHAM’S Line Drawing Algorithm.
B.3 Observations and learning:
 DDA Algorithm: Simple but slower due to floating-point operations. Requires rounding
and handles all slopes.
 Bresenham's Algorithm: Faster as it uses only integer operations. More efficient and
accurate, especially for lines with steep slopes.

Learning:
 DDA is easier to implement but less efficient.
 Bresenham's is optimal for raster devices, minimizing computation and improving
performance in graphics applications.

B.4 Conclusion:
Bresenham's line drawing algorithm is preferred over DDA for its efficiency, accuracy, and use
of integer arithmetic, making it more suitable for real-time applications in computer graphics.

B.5 Question of Curiosity

Ipfilt-FU NextMsgñlt-F?Prev
Q.1] Write Disadvantages of DDA line algorithm. How it is overcome by Bresenham
Line Drawing algorithm.

Q.2] Plot the Line for co-ordinates 100,100 to 150,150 using DDA and Bresenham
Line Drawing algorithm.

1. DDA Line Drawing Algorithm:

Step-by-Step Calculation:

Start Point: (100, 100)

End Point: (150, 150)

dx = 150 - 100 = 50

dy = 150 - 100 = 50

Steps = max(|dx|, |dy|) = 50

Increment in x (x_inc) = dx / Steps = 50 / 50 = 1

Increment in y (y_inc) = dy / Steps = 50 / 50 = 1

Plotting Points:
Start at (100, 100)

Increment x and y by 1 for each step.

2. Bresenham Line Drawing Algorithm:

Step-by-Step Calculation:
 Start Point: (100, 100)
 End Point: (150, 150)

 dx = 50

 dy = 50

 Initial decision parameter p0 = 2 * dy - dx = 2 * 50 - 50 = 50

 Increment values:

o If p<0p < 0p<0, next pk+1=pk+2∗dyp_{k+1} = p_k + 2 * dypk+1=pk+2∗dy

o If p≥0p \geq 0p≥0, next pk+1=pk+2∗dy−2∗dxp_{k+1} = p_k + 2 * dy - 2 * dxpk+1=pk


+2∗dy−2∗dx

You might also like