DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Experiment 1.2
Name: Vineet Karan Singh UID:22BCS16616
Branch: CSE Section: IOT 607/B
Semester: 6th DOP: 31/01/1025
Subject: Computer Graphics Subject Code: 22CSH-352
Aim: Implement and compare the performance of Simple DDA, Symmetrical DDA, and
Bresenham’s algorithm for positive and negative line slope.
Objective: To implement and compare the performance of Simple Digital Differential
Analyzer (DDA), Symmetrical DDA, and Bresenham’s line-drawing algorithms for rendering
lines with positive and negative slopes, analyzing their computational efficiency, accuracy, and
suitability for different scenarios in computer graphics.
Algorithm:
Calculate Differences:
• dx = x2 - x1
• dy = y2 - y1
Determine the number of steps:
• steps = max(abs(dx), abs(dy)) Calculate
the increments:
• xInc = dx / steps (For Simple DDA)
• yInc = dy / steps (For Simple DDA)
Set the initial points:
• x = x1
• y = y1
Error Handling (Symmetrical DDA):
• error = 0.5 (Error term to handle precision
issues)
Main Loop (Bresenham-like):
While steps > 0:
o Plot the point (round(x), round(y)) o If abs(dx) > abs(dy)
(Line has a shallower slope):
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Increment x by xInc Update error = error +
dy
If error >= 0.5, increment y by yInc and reset
error: error = error
- 1
o Else (Line has a steeper slope):
Increment y by yInc Update error = error +
dx
If error >= 0.5, increment x by xInc and reset
error: error = error
- 1
o Decrease
steps
Handle Negative Slopes (Symmetrical DDA-like adjustment):
• If dy < 0, reverse the direction and handle
accordingly by updating the increments (i.e.,
yInc = -yInc).
Repeat until the last point (x2, y2) is reached.
Code:
#include<iostream.h>
#include<dos.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
#define round(a) ((int)(a+0.5))
void dda_line(int x1, int y1, int x2, int y2)
{ int dx = (x2 - x1);
int dy = (y2 - y1);
int length;
if (abs(dy) > abs(dx)) length
= abs(dy);
else length =
abs(dx); float
xinc, yinc, x =
x1, y = y1; xinc
= dx /
(float)length;
yinc = dy / (float)length;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
putpixel(round(x), round(y), 15); // Set initial pixel for
(int k = 1; k <= length; k++)
{ x = x + xinc;
y = y + yinc;
putpixel(round(x), round(y), 15); // Set subsequent pixels
delay(50); // Adjust delay as needed }
// Calculate the midpoint of the line
int midX = (x1 + x2) / 2; int midY
= y1;
// Displaying name "Rosh" centered below the line setcolor(WHITE); // Set
text color
outtextxy(midX - (textwidth("Rosh") / 2), midY + 10, "Rosh"); }
void main()
{ clrscr();
int x1, x2, y1, y2; int
gd = DETECT, gm;
// Horizontal Line Input
cout << "Enter the x-coordinate of starting point: "; cin
>> x1;
cout << "Enter the y-coordinate of the line: "; cin
>> y1;
cout << "Enter the x-coordinate of ending point: ";
cin >> x2; y2 = y1; // Keep y2 same as y1 for a
horizontal line getch(); initgraph(&gd, &gm,
"c:\\turboc3\\bgi"); dda_line(x1, y1, x2, y2);
getch();
closegraph();
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Learning Outcomes:
1. Line Drawing Concepts: Understand the working of DDA and Bresenham's algorithms for
line drawing.
2. Performance Comparison: Analyze and compare the efficiency of Simple DDA,
Symmetrical DDA, and Bresenham’s algorithms.
3. Error Handling: Learn how error terms are managed in graphics algorithms to minimize
visual imperfections.
4. Optimization: Explore the efficiency of integer-based algorithms (like Bresenham's) vs.
floating-point methods (like DDA).
5. Coding and Debugging: Improve coding and debugging skills through algorithm
implementation and testing.