0% found this document useful (0 votes)
11 views5 pages

CG Ex-2

Computer graphics lab work chandigarh university
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)
11 views5 pages

CG Ex-2

Computer graphics lab work chandigarh university
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/ 5

DEPARTMENT OF

COMPUTER SCIENCE &


ENGINEERING
Experiment - 2
Student Name:
Branch: CSE Sec/Grp: 22BCS)
Semester: 6th Date of Performance:23/01/25
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 project is to implement and compare the performance of three prominent
line drawing algorithms—Simple DDA, Symmetrical DDA, and Bresenham’s algorithm. The
aim is to analyze the efficiency, accuracy, and computational performance of these algorithms by
evaluating the quality of the lines they produce and the time complexity for various line
orientations. By conducting this comparison, the project seeks to provide insights into the
strengths and weaknesses of each algorithm, particularly in terms of handling different slope
conditions in computer graphics.
3. Implementation/Code:
(A) Simple DDA
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
int main(){
float x,y,x1,x2,y1,y2,dx,dy,c,m,xe;
int gd = DETECT, gm;
initgraph(&gd,&gm,(char*)"");
printf("Enter the points(x1,y1)");
scanf("%f%f",&x1,&y1);
printf("Enter the points(x2,y2)");
scanf("%f %f",&x2,&y2);
outtextxy(150, 100, (char*)"Simple DDA");
dx = x2 - x1;
dy = y2 - y1;
m = dy/dx;
if(dx>0){
x = x1;
y = y1;
xe = x2;
}else if(dx<0){
x = x2;
y = y2;
xe = x1;
}
while(x<=xe){
putpixel(x,y,WHITE);
DEPARTMENT OF
COMPUTER SCIENCE &
ENGINEERING
x = x+1;
y = m*x + c;
}
getch();
closegraph();
return 0;
}

(B) Symmetrical DDA

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
int main(){
int x1,x2,y1,y2;
float x,y,dx,dy,length,i;
int gd = DETECT, gm;
initgraph(&gd,&gm,(char*)"");
printf("Enter the points(x1,y1)");
scanf("%d %d",&x1,&y1);
printf("Enter the points(x2,y2)");
scanf("%d %d",&x2,&y2);
outtextxy(150, 100, (char*)"Symmetrical Line Drawing DDA");
if(abs(x2-x1)>abs(y2-y1)){
length = abs(x2-x1);
}else{
length = abs(y2-y1);
}
dx = (x2-x1)/length;
dy = (y2-y1)/length;
x = x1; y = y1; i =0;
while(i<=length){
putpixel(x,y,WHITE);
x = x + dx;
y = y + dy;
i++;
}
getch();
closegraph();
return 0;
}

(C) Bresenham’s algorithm

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
DEPARTMENT OF
COMPUTER SCIENCE &
ENGINEERING
int main(){
int x1,x2,y1,y2,x,y,dx,dy,p,i;
int gd = DETECT, gm;
initgraph(&gd,&gm,(char*)"");
printf("Enter the points(x1,y1)");
scanf("%d %d",&x1,&y1);
printf("Enter the points(x2,y2)");
scanf("%d %d",&x2,&y2);
outtextxy(150, 100, (char*)"Breshenam Line Drawing");
dx = x2-x1;
dy = y2-y1;
p = 2*dy -dx;
x = x1; y = y1; i = 0;
while(i<=dx){
putpixel(x,y,WHITE);
if(p<0){
x = x+1;
p = p+2*dy;
}else{
x = x+1;
y = y+1;
p = p + 2*dy-2*dx;
}
i++;
}
getch();
closegraph();
return 0;
}

4. Output:
(A)Simple DDA

Fig(i). BGI output using simple dda algorithm


DEPARTMENT OF
COMPUTER SCIENCE &
ENGINEERING
(B)Symmetrical DDA

Fig(ii). BGI output using symmetrical dda algorithm

(C)Bresenham’s Algorithm

Fig(iii). BGI output using Bresenham’s algorithm

(D)Time taken by all algorithms when given same output to get idea about the efficiency

Fig(iv). Time taken by all algorithm to draw a line with similar inputs
DEPARTMENT OF
COMPUTER SCIENCE &
ENGINEERING
5. Learning Outcome:
 Gaining a thorough understanding of the principles and working mechanisms of the Simple
DDA, Symmetrical DDA, and Bresenham’s algorithms for line drawing in computer
graphics.
 Developing the ability to compare the performance of different line drawing algorithms
based on criteria such as accuracy, efficiency, and computational complexity for both
positive and negative slopes.
 Enhancing programming skills by implementing and testing the algorithms in a chosen
programming language (e.g., Java), and evaluating the results with different line slopes.
 Gaining insights into how optimization techniques in algorithms (such as Bresenham's error-
correction method) lead to improved performance and reduced computational cost.
 Understanding the impact of errors in line drawing (such as pixel deviation from the ideal
line) and how each algorithm handles these errors for different line orientations.
 Applying theoretical knowledge to real-world computer graphics problems, with a focus on
improving the efficiency of rendering lines in digital graphics and image processing.

You might also like