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

CG Ex-2

The document outlines an experiment in a Computer Graphics Lab focused on implementing and comparing the performance of three line-drawing algorithms: Simple DDA, Symmetrical DDA, and Bresenham’s algorithm. It includes code implementations for each algorithm and discusses their computational efficiency, accuracy, and rendering capabilities for both positive and negative line slopes. The learning outcomes highlight the trade-offs between accuracy and performance for each algorithm, with Bresenham’s being the most efficient.

Uploaded by

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

CG Ex-2

The document outlines an experiment in a Computer Graphics Lab focused on implementing and comparing the performance of three line-drawing algorithms: Simple DDA, Symmetrical DDA, and Bresenham’s algorithm. It includes code implementations for each algorithm and discusses their computational efficiency, accuracy, and rendering capabilities for both positive and negative line slopes. The learning outcomes highlight the trade-offs between accuracy and performance for each algorithm, with Bresenham’s being the most efficient.

Uploaded by

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

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 2
Student Name: Zatch UID:
Branch: CSE Section/Group:
Semester: 6 Date of Performance:
Subject Name: Computer Graphics Lab Subject Code: 22CSH-352

1. Aim:
Implement and compare the performance of Simple DDA, Symmetrical DDA,
and Bresenham’s algorithm for positive and negative line slope.
2. Objective:
The objective of this practical is to implement and compare the performance of
Simple DDA, Symmetrical DDA, and Bresenham’s line-drawing algorithms for
lines with both positive and negative slopes. The comparison focuses on
computational efficiency, accuracy, and their ability to render lines on a raster
display.
3. Implementation/Code:
a) DDA:
#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)


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

{
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;

putpixel(round(x),round(y),15);

for(int k=1;k<=length;k++)
{
x=x+xinc;
y=y+yinc;
putpixel(round(x),round(y),15);
delay(100);
}
}

void main()
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

{
clrscr();

int x1, x2, y1, y2;


int gd = DETECT, gm;

cout << "Enter the x-coordinate of starting point : ";


cin >> x1;

cout << "Enter the y-coordinate of ending point : ";


cin >> y1;
cout << endl;

cout << "Enter the x-coordinate of starting point : ";


cin >> x2;

cout << "Enter the y-coordinate of ending point : ";


cin >> y2;

initgraph(&gd, &gm, "c:\\turboc3\\bgi");

dda_line(x1, y1, x2, y2);


setcolor(4);
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

getch();
closegraph();
}
b) Using Symmetrical DDA:
#include <conio.h>
#include <iostream.h>
#include <graphics.h>
#include <dos.h>
#include <math.h>

#define ROUND(a) ((int)(a+0.5))

void symDDA(int xa, int ya, int xb, int yb)


{
int dx = xb - xa, dy = yb - ya;
float length;
float xinc, yinc, x = xa, y = ya;

if (abs(dx) > abs(dy))


length = abs(dx);
else
length = abs(dy);

float n = log10(length) / log10(2);


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

xinc = dx / (pow(2, n));


yinc = dy / (pow(2, n));

putpixel(ROUND(x), ROUND(y), 15);

delay(50);

for (int i = 0; i < length; i++)


{
x = x + xinc;
y = y + yinc;
putpixel(ROUND(x), ROUND(y), 15);
delay(50);
}
}

void main()
{
int gd = DETECT, gm;

initgraph(&gd, &gm, "C:\\Turboc3\\BGI");

int xa, xb, ya, yb;


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

cout << "Enter the points (xa ya xb yb): ";


cin >> xa >> ya >> xb >> yb;

cleardevice();

symDDA(xa, ya, xb, yb);

getch();
closegraph();
}
c) Using bresenham’s algorithm
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>

int sign(int x)
{
if(x < 0)
return(-1);
if(x > 0)
return(1);
else
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

return(0);
}

void lineBres(int xa, int ya, int xb, int yb)


{
int sx, sy, t, length, flag;
int x = xa;
int y = ya;
int dx = abs(xa - xb), dy = abs(ya - yb);

sx = sign(xb - xa);
sy = sign(yb - ya);

if(dy > dx)


{
t = dx;
dx = dy;
dy = t;
length = dy;
flag = 1;
}
else
{
length = dx;
flag = 0;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

int p = (2 * dy) - dx;


int twoDx = 2 * dx, twoDy = 2 * dy;

putpixel(x, y, 15); // Initial point


delay(50);

for(int i = 0; i < length; i++)


{
while(p > 0)
{
if(flag == 1)
x = x + sx;
else
y = y + sy;

p = p - twoDx;
}

if(flag == 1)
y = y + sy;
else
{
x = x + sx;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

p = p + twoDy;
}

putpixel(x, y, 15);
delay(50);
}
}

void main()
{
int gd = DETECT, gm;

initgraph(&gd, &gm, "c://turboc3//bgi");

int xa, ya, xb, yb;

cout << "Enter the starting point of x : ";


cin >> xa;
cout << "Enter the starting point of y : ";
cin >> ya;
cout << "Enter the ending point of x : ";
cin >> xb;
cout << "Enter the ending point of y : ";
cin >> yb;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

cleardevice();

lineBres(xa, ya, xb, yb);

getch();

closegraph();
}
4. Output:

Fig 1: DDA

Fig 2: Using Symmetrical DDA


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Fig 3: Using bresenham’s algorithm


5. Learning Outcome:
• Simple DDA is a basic algorithm that uses floating-point calculations to plot
points along the line. It helps in understanding the concept of line generation
and the trade-offs between accuracy and performance.
• Symmetrical DDA optimizes the DDA algorithm by reducing floating-point
operations, which improves efficiency without compromising the accuracy of
the plotted line.
• Bresenham’s Algorithm is the most efficient algorithm among the three,
utilizing only integer calculations. It provides the highest performance for line
drawing, especially when dealing with large lines or real-time graphics.

You might also like