Cglab
Cglab
Engineering
SUBJECT: Computer graphics with Lab
(22CSH-352/22ITH-352)
LAB MANUAL
Chandigarh University
Gharuan, Mohali
Sr.
Particular Page No.
No.
3 PEO/POs/SOs/PSOs iii-vi
Detail of Experiments
Aim
Objective
7 Input/Apparatus Used
1-124
Procedure/Algorithm/Code
Observations/Outcome
Discussion
i
University-Vision and Mission
Offering programs & courses in consonance with National policies for nation building and
meeting global challenges.
Designing Curriculum to match International standards, needs of Industry, civil society and for
inculcation of traits of Creative Thinking and Critical Analysis as well as Human and Ethical
values.
Ensuring students delight by meeting their aspirations through blended learning, corporate
mentoring, professional grooming, flexible curriculum and healthy atmosphere based on co-
curricular and extra-curricular activities.
Establishing strategic relationships with leading National and International corporates and
universities for academic as well as research collaborations.
ii
Department-Vision and Mission
M1: To provide practical knowledge using state-of-the-art technological support for the
experiential learning of our students.
M2: To provide industry recommended curriculum and transparent assessment for quality
learning experiences.
M3: To create global linkages for interdisciplinary collaborative learning and research.
M4: To nurture advanced learning platform for research and innovation for students’ profound
future growth.
M5: To inculcate leadership qualities and strong ethical values through value based education.
iii
PEO Program Educational Objectives (PEOs)
The Program Educational Objectives of the Computer Science & Engineering undergraduate
program are for graduates to achieve the following, within few years of graduation. The
graduates of Computer Science & Engineering Program will
PEO1: Engage in successful careers in industry, academia, and public service, by applying the
acquired knowledge of Science, Mathematics and Engineering, providing technical leadership
for their business, profession and community.
PEO3: Exhibit commitment and engage in lifelong learning for enhancing their professional
and personal capabilities.
iv
PO Program Outcomes (PO's)
Program Outcomes(PO's)
PO 12. Life-long learning: Recognize the need for, and have the preparation and ability to
engage in independent and life-long learning in the broadest context of technological change.
v
Program Specific Outcomes (PSOs)
A Graduate of Computer Science and Engineering Program will be able:
PSO 1. Exhibit attitude for continuous learning and deliver efficient solutions for emerging
challenges in the computation domain.
PSO 2. Apply standard software engineering principles to develop viable solutions for
Information Technology Enabled Services (ITES).
vi
Student Outcomes
The Bachelor of Engineering is a program offered by the Department of Computer Science &
Engineering in accordance with the Student Outcome of Computing Accreditation
Commission (CAC) and Engineering Accreditation Commission (EAC) of ABET. The
Student Outcomes are as follows:
SO 1. Analyze a complex computing problem and apply principles of computing and other
relevant disciplines to identify solutions.
SO 2. Design, implement and evaluate a computing-based solution to meet a given set of
computing requirements in the context of the program’s discipline.
SO 3. Communicate effectively in a variety of professional contexts.
SO 4. Recognize professional responsibilities and make informed judgments in computing
practice based on legal and ethical principles.
SO 5. Function effectively as a member or leader of a team engaged in activities appropriate
to the program’s discipline.
SO 6. Apply computer science theory and software development fundamentals to produce
computing-based solutions.
vii
CODE OF ETHICS
1. I will respect my teachers, university staff and elders and I will support and help
younger ones
3. I will love my institution and work for its improvement. I will not criticize for minor
shortcomings. I will become a good example as human being in the society. I will
keep campus clean and not damage university property.
4. I will remain regular in attending university. I will never bunk classes. It is only on
urgent occasions that I will seek permission to go home. I will not be absent from
university because of petty reasons like headache, sleep, minor temperature etc.
5. I will do my home work daily and regularly. I understand that not doing homework
regularly lead to many problems like inefficiency, tensions and poor results. My room
at home will have sufficient books and learning material right from the beginning of
semester/year.
6. I will return Library book in time. I will not damage them, keep them neat and clean.
I will regularly visit library during spare time. I will make full use of labs and other
resources.
7. I will wear university I-Card and obey dress code decided by university from time to
time.
8. I will utilize my vacation for extra coaching, writing papers, educational articles and
personality development work and will not waste them.
9. I will watch TV, News, read Newspapers, technical magazines and keep myself aware
about the happenings in the rest of the world. I will use Internet for learning and
exploring opportunities.
10. I will not waste hard earned money of my parents. I will never bring outlay gadgets
to university to impress my university mates.
12. I will not indulge in cheating or any unfair means while appearing in my exams
/interviews.
13. I will take part in sports and other co-curricular activities regularly and I will take part
in various schemes for the welfare of the society like blood donation, tree plantation
and adult education.
viii
Syllabus
a. Course Description:
This course provides an in-depth understanding of the fundamental principles, algorithms, and applications of computer
graphics. It introduces students to the hardware and software aspects of graphical systems, emphasizing the design and
implementation of interactive graphical environments. The course explores key topics such as scan conversion,
transformations, clipping, 3D graphics, and hidden surface elimination, equipping students with the theoretical and
practical knowledge needed to develop graphic-rich applications.
b. Course Objectives:
c. Course Outcome
Describe the fundamental concepts of computer graphics, including display systems and
CO1 transformations.
Apply line and circle drawing algorithms, transformations, and clipping techniques in practical
CO2 scenarios.
Analyze the efficiency and accuracy of graphical algorithms like DDA, Bresenham's, and Cohen-
CO3 Sutherland.
Evaluate the outcomes of transformations and clipping operations, and interpret their visual results.
CO4
Develop interactive graphics applications using advanced techniques like Bezier curves and
CO5 graphical input devices.
UNIT 1 15 Hours
Introduction: Origins of Computer Graphics, Interactive Graphics display working, Scan conversion; Raster and
Random scan conversion. Point plotting techniques: Coordinate systems, Incremental methods, Line drawing
algorithms: Simple DDA, Symmetrical DDA, Bresenham’s algorithm. Midpoint circle drawing algorithm, Circle
generators.
Line Drawing Displays: Display devices and Controllers, CRT, Inherent memory devices, Storage tube display,
ix
Refresh line drawing displays.
2-D Transformations: Transformation principles, Homogeneous coordinate systems, Concatenations, Matrix
representations of transformations.
Experiment Demonstrate the use of graphics.h functions to draw basic shapes like lines, triangles, and
1.1 circles.
Experiment Implement and compare the performance of Simple DDA, Symmetrical DDA, and
1.2 Bresenham’s algorithm for line drawing.
Experiment Apply translation, scaling, and rotation transformations on a given triangle and observe the
1.3 changes.
Experiment a) Develop a program to draw a circle using the circle generator algorithm for a given center
1.4 and radius.
b) Develop a program to draw a circle using the midpoint circle algorithm for a given center
and radius.
Unit 2 15 Hours
Clipping and Windowing: Point clipping, Line clipping algorithms: Cohen Sutherland algorithm, Midpoint
subdivision algorithm. Polygon clipping, Viewing transformations, Windowing transformations.
Graphics Packages: Ground rules for graphics software design, Functional domains, Graphic primitives,
Windowing functions, miscellaneous functions, Display code generator.
Interactive Graphics: Graphical input devices: Pointing and Positioning devices, Mouse, Tablets, light pen, 3-D
Input devices, Comparators. Graphical input techniques: Positioning techniques, pointing and Selection, Inking
and Painting, On-line character recognition.
Experiment Implement clockwise and anticlockwise rotation of a triangle about a specified point and
2.1 evaluate the results.
Experiment Analyze and implement the reflection of a point about a line defined by the equation y=mx+c.
2.2
Experiment Evaluate the 4-bit region code for line endpoints and determine whether the line lies inside or
2.3 outside the viewport.
Unit 3 15 Hours
Solid Area Scan Conversion: Geometric representation of areas, Scan converting polygons, Priority, The Y-X
algorithm, Properties of scan conversion algorithms.
Graphics: Curves and Surfaces: Parametric functions, Bezier methods and B-Spline methods. Three dimensional
transformations, Perspective transformations, Perspective depth.
Hidden Surface Elimination: Basic approaches, Depth –Buffer algorithm, Scan line coherence algorithms, Area
coherence algorithms, Priority algorithms.
Experiment a) Apply the Cohen-Sutherland Line Clipping algorithm to clip a line intersecting at one
3.1 point with a given window.
b) Apply the Cohen-Sutherland Line Clipping algorithm to clip a line intersecting at two
or more points with a given window
x
Experiment Demonstrate the result of window-to-viewport transformation by implementing and visualizing
3.2 the process.
d. Text Books:
• Newman, William M., Sproull, Robert F., “Principles of Interactive Computer Graphics”, Tata
McGraw Hill Company, 2ndEdition.
• Hearn, D., Baker M. P. ,”Computer Graphics”, Pearsoneducation.
• Xiang Z., Plastock R., “ Computer Graphics”, Schaum’s outlines, Tata Mcgraw Hill Companies.
e. Reference Books:
CO-PO Mapping
Course PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO1 PO1 PO12
PSO
PSO
Outcome 0 1
2
CO1 3 2 1 1 1 - - - - 1 - 2 2 -
CO2 3 3 2 2 2 - - - - - - 2 - -
CO3 3 3 2 3 2 - - - - 1 - 2 2 -
CO4 2 3 2 3 2 1 - - - - - - - 2
CO5 3 2 3 3 3 1 - - 2 2 2 3 - -
AVG 2.8 2.6 2 2.4 2 1 - - 2 1.3 2 2.25 2 2
xi
List of Experiments (Mapped with Cos)
Experiment 4 a) Develop a program to draw a circle using the circle CO2, CO3
generator algorithm for a given center and radius.
Experiment 6 Analyze and implement the reflection of a point about a CO3, CO4
line defined by the equation y=mx+c.
Experiment 7 Evaluate the 4-bit region code for line endpoints and CO2, CO4
determine whether the line lies inside or outside the
viewport.
Experiment 8 a) Apply the Cohen-Sutherland Line Clipping algorithm to clip a CO4, CO5
line intersecting at one point with a given window.
b) Apply the Cohen-Sutherland Line Clipping algorithm to clip a
line intersecting at two or more points with a given window
xii
Experiment 1:
AIM: Demonstrate the use of graphics.h functions to draw basic shapes like lines, triangles, and circles.
Objective: To familiarize students with using the graphics.h library to create basic shapes like lines, triangles, and circles.
Code: #include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
setbkcolor(2);
circle(100,100,50);
outtextxy(75,98,"CIRCLE");
rectangle(210,60,400,140);
outtextxy(270,100,"RECTANGLE");
line(455,85,555,85);
outtextxy(490,95,"LINE");
arc(80,350,10,80,100);
outtextxy(100,340,"ARC");
ellipse(380,300,0,360,40,80);
outtextxy(355,300,"ELLIPSE");
getch();
}
Output:
a) DDA
A digital differential analyzer (DDA) is hardware or software used for linear interpolation of variables over
an interval between start and end point. DDAs are used for rasterization of lines, triangles and polygons. In its simplest
implementation, the DDA algorithm interpolates values in interval by computing for each xi in the equations.
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;
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()
{
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;
Department of Computer Science & Engineering Page 15
cout<<endl;
cout<<"Enter the x-coordinate of starting point : ";
cin>>x2;
cout<<"Enter the y-coordinate of ending point : ";
cin>>y2;
getch();
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
dda_line(x1,y1,x2,y2);
setcolor(4);
getch();
closegraph();
length=abs(dx);
Department of Computer Science & Engineering Page 16
else
length=abs(dy);
float n=log10(length)/log10(2);
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,"");
int xa,xb,ya,yb;
cout<<"enter the points";
cin>>xa>>xb>>ya>>yb;
cleardevice();
symDDA(xa,xb,ya,yb);
getch();
closegraph();
}
Viva Questions:
Q1: Differentiate between Simple DDA, Symmetrical DDA, and Bresenham’s Algorithm.
Q2: Why is Bresenham’s algorithm considered more efficient than DDA?
Q3: List the role of floating-point arithmetic in DDA?
Q4: How does the midpoint criteria work in Bresenham’s algorithm?
Q5: List the advantages of Symmetrical DDA over Simple DDA?
Output:
Code:
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,"c://turboc3//bgi");
int x1=100,y1=100,x2=80,y2=150,x3=120,y3=150,sx,sy;
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
cout<<"Enter scaling value among x-axis :";
cin>>sx;
cout<<"Enter scaling value among y-axis :";
cin>>sy;
x1*=sx; x2*=sx; x3*=sx;
y1*=sy; y2*=sy; y3*=sy;
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
getch();
}
Output:
Viva Questions:
Q1: Define homogeneous transformation matrix.
Q2: How do you perform translation mathematically?
Q3: Differentiate between scaling with respect to origin vs. scaling with respect to a point?
Q4: Define rotation matrix, and how does clockwise rotation differ from anticlockwise rotation?
Q5: How do you apply multiple transformations together?
Objective:
To develop and implement the circle generator and midpoint circle generator algorithm to draw a circle with
a given center and radius.
a) Mid point circle Algorithm:
Code:
#include<iostream.h>
#include<conio.h>
# include<graphics.h>
#include<math.h>
#include<dos.h>
#define round(a) ((int)a+0.5)
void putcircle(int xc,int yc,int x,int y)
{
putpixel(xc+x,yc+y,1);
putpixel(xc-x,yc+y,2);
putpixel(xc+x,yc-y,3);
putpixel(xc-x,yc-y,4);
putpixel(xc+y,yc+x,5);
putpixel(xc-y,yc+x,6);
putpixel(xc+y,yc-x,7);
putpixel(xc-y,yc-x,8);
}
void circlemid(int xc,int yc,float r)
{
float x=0,y=r;
int p=1-r;
while(x<y)
{
x++;
if(p<0)
p=p+(2*x)+1;
else
{
y--;
p=p+(2*(x-y)+1);
}
putcircle(xc,yc,round(x),round(y));
delay(10);
}
}
// METHOD1
int xc=100,yc=150,r=50;
float x=0,y=0;
for(int i=0;i<=45;i++)
{
double ang=double(i)*(3.142/180);
x = r*cos(ang);
y = r*sin(ang);
putpixel(xc+round(x),yc+round(y),15);
putpixel(xc-round(x),yc+round(y),15);
putpixel(xc+round(x),yc-round(y),15);
putpixel(xc-round(x),yc-round(y),15);
putpixel(xc+round(y),yc+round(x),15);
putpixel(xc-round(y),yc+round(x),15);
putpixel(xc+round(y),yc-round(x),15);
putpixel(xc-round(y),yc-round(x),15);
delay(100);
}
//METHOD2
x=0;y=r;
xc=300,yc=150;
for(x=0;x<y;x++)
{
double temp=((r*r)-(x*x));
y=sqrt(temp);
putpixel(xc+round(x),yc+round(y),15);
putpixel(xc-round(x),yc+round(y),15);
putpixel(xc+round(x),yc-round(y),15);
putpixel(xc-round(x),yc-round(y),15);
putpixel(xc+round(y),yc+round(x),15);
putpixel(xc-round(y),yc+round(x),15);
putpixel(xc+round(y),yc-round(x),15);
putpixel(xc-round(y),yc-round(x),15);
delay(100);
}
getch();
closegraph();
}
Viva Questions:
Q1: Differentiate between the circle generator algorithm and the midpoint circle algorithm?
Q2: Why do we calculate only one-eighth of the circle in the midpoint algorithm?
Q3: Define the role of decision parameter in the midpoint circle algorithm?
Q4: List the advantages of the midpoint circle algorithm over the Bresenham’s circle algorithm?
Q5: How do you modify the algorithm to draw an ellipse instead of a circle?
Code:-
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
int x1,y1,x2,y2,x3,y3;
cout<<"Enter (x1,y1),(x2,y2),(x3,y3) for triangle : ";
cin>>x1>>y1>>x2>>y2>>x3>>y3;
int a[]={x1,y1,x2,y2,x3,y3,x1,y1};
drawpoly(4,a);
int xf=(x1+x2+x3)/3;
int yf=(y1+y2+y3)/3;
float ang;
cout<<"Enter the rotation angle :";
cin>>ang;
float rad=ang*3.1428/180;
int X1=(x1-xf)*cos(rad)-(y1-yf)*sin(rad)+xf;
int Y1=(x1-xf)*sin(rad)+(y1-yf)*cos(rad)+yf;
int X2=(x2-xf)*cos(rad)-(y2-yf)*sin(rad)+xf;
int Y2=(x2-xf)*sin(rad)+(y2-yf)*cos(rad)+yf;
int X3=(x3-xf)*cos(rad)-(y3-yf)*sin(rad)+xf;
int Y3=(x3-xf)*sin(rad)+(y3-yf)*cos(rad)+yf;
int b[]={X1,Y1,X2,Y2,X3,Y3,X1,Y1};
drawpoly(4,b);
getch();
}
Code:-
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
int x1,y1,x2,y2,x3,y3;
cout<<"Enter (x1,y1),(x2,y2),(x3,y3) for triangle : ";
cin>>x1>>y1>>x2>>y2>>x3>>y3;
int a[]={x1,y1,x2,y2,x3,y3,x1,y1};
drawpoly(4,a);
int xf=(x1+x2+x3)/3;
int yf=(y1+y2+y3)/3;
float ang;
cout<<"Enter the rotation angle :";
cin>>ang;
float rad=ang*3.1428/180;
int X1=(x1-xf)*cos(rad)+(y1-yf)*sin(rad)+xf;
int Y1=-(x1-xf)*sin(rad)+(y1-yf)*cos(rad)+yf;
int X2=(x2-xf)*cos(rad)+(y2-yf)*sin(rad)+xf;
int Y2=-(x2-xf)*sin(rad)+(y2-yf)*cos(rad)+yf;
int X3=(x3-xf)*cos(rad)+(y3-yf)*sin(rad)+xf;
int Y3=-(x3-xf)*sin(rad)+(y3-yf)*cos(rad)+yf;
int b[]={X1,Y1,X2,Y2,X3,Y3,X1,Y1};
drawpoly(4,b);
getch();
}
Code:
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm," ");
int x1,y1,x2,y2;
cout<<"Enter the coordinates of the line\n";
cin>>x1>>y1>>x2>>y2;
int m=getmaxx();
int n=getmaxy();
setcolor(6);
line(x1,y1,x2,y2);
outtextxy(x1,y1+10,"Original Object");
setcolor(4);
line((m/2),0,(m/2),n);
line(0,(n/2),m,(n/2));
setcolor(3);
int c=(n/2)-y1;
int d=(n/2)-y2;
y2=y2+(d*2);
y1=y1+(c*2);
line(x2,y2,x1,y1);
outtextxy(x1,y1+10,"Reflection along X-axis");
setcolor(9);
int a=(m/2)-x1;
int b=(m/2)-x2;
x1=x1+(a*2);
x2=x2+(b*2);
line(x1,y1,x2,y2);
outtextxy(x2-20,y2+10,"Reflection along Y-axis");
getch();
closegraph();
}
Viva Questions:
Q1: What is the formula to reflect a point about x-axis, y-axis, and origin?
Q2: How do you reflect a point about a line of the form y = mx + c?
Q3: What happens if the line is y = x or y = -x?
Q4: Can reflection be performed using a single matrix transformation?
Q5: What is the difference between reflection and rotation?
Code:
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,NULL);
int xmax=400,ymax=300,xmin=200,ymin=150;
line(xmin,0,xmin,getmaxy());
line(xmax,0,xmax,getmaxy());
line(0,ymax,getmaxx(),ymax);
line(0,ymin,getmaxx(),ymin);
cout<<"Enter the endpoints of the line :";
int x[2],y[2],num[2];
cin>>x[0]>>y[0]>>x[1]>>y[1];
line(x[0],y[0],x[1],y[1]);
for(int i=0;i<2;i++)
{
int bit1=0,bit2=0,bit3=0,bit4=0;
if(y[i]<ymin)
bit1=1;
if(y[i]>ymax)
bit2=1;
if(x[i]>xmax)
bit3=1;
if(x[i]<xmin)
bit4=1;
cout<<”For “<<i<<”th endpoint region code is :”<<bit1<<bit2<<bit3<<bit4;
num[i]=bit4*1+bit3*2+bit2*4+bit1*8;
}
if(!(num[0]|num[1]))
cout<<"Line is completely in window";
else if(!(num[0]&num[1]))
cout<<"Line is required to be clipped";
else
cout<<"Line is off the window";
getch();
}
Output:
Output:
Code:
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
void main()
{
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,NULL);
float wxmin=10,wxmax=150,wymin=10,wymax=250;
float vxmin=200,vxmax=600,vymin=10,vymax=250;
int wx1=30,wy1=50,wx2=100,wy2=180;
rectangle(wxmin,wymin,wxmax,wymax);
rectangle(vxmin,vymin,vxmax,vymax);
float sx=(vxmax-vxmin)/(wxmax-wxmin);
float sy=(vymax-vymin)/(wymax-wymin);
line(wx1,wy1,wx2,wy2);
float vx1=sx*(wx1-wxmin)+vxmin;
float vy1=sy*(wy1-wymin)+vymin;
float vx2=sx*(wx2-wxmin)+vxmin;
float vy2=sy*(wy2-wymin)+vymin;
line(vx1,vy1,vx2,vy2);
outtextxy(60,260,"Window");
outtextxy(360,260,"Viewport");
getch();
}
Output:
*The segments mentioned above needs to be covered in each practical lab but the time allocated
to each segment can be changed as per the requirement of the practical delivery and orientation.
** Can be carried out simultaneously.
Viva Voce
2. 10 marks
Submission of Work Sheet (Record)
3. 8 marks
Total 30 marks
Course Experiment No 1 to 10
S.no
UID StudentName Section Code
conduct viva worksheet
12 10 8 Total