CG Ex-2
CG Ex-2
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>
{
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();
getch();
closegraph();
}
b) Using Symmetrical DDA:
#include <conio.h>
#include <iostream.h>
#include <graphics.h>
#include <dos.h>
#include <math.h>
delay(50);
void main()
{
int gd = DETECT, gm;
cleardevice();
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);
}
sx = sign(xb - xa);
sy = sign(yb - ya);
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;
cleardevice();
getch();
closegraph();
}
4. Output:
Fig 1: DDA