You are on page 1of 64

Raj Kumar Goel Institute of Technology, Ghaziabad

LABORATORY MANUAL

Faculty Name : Mr NIKUNJ KUMAR Department : CSE


Course Name : Computer Graphics Lab Course Code : RCS-653

Year/Sem : NBA Code : C319


3 /6
rd th

Email ID : nkunjfcs@rkgit.edu.in Academic Year : 2018-19

Department of Computer Science and Engineering


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering

VISION OF THE INSTITUTE

To continually develop excellent professionals capable of providing sustainable solutions to


challenging problems in their fields and prove responsible global citizens.

MISSION OF THE INSTITUTE


We wish to serve the nation by becoming a reputed deemed university for providing value
based professional education.

VISION OF THE DEPARTMENT

To be recognized globally for delivering high quality education in the ever changing field of
computer science & engineering, both of value & relevance to the communities we serve.

MISSION OF THE DEPARTMENT


1. To provide quality education in both the theoretical and applied foundations of Computer
Science and train students to effectively apply this education to solve real world problems.
2. To amplify their potential for lifelong high quality careers and give them a competitive
advantage in the challenging global work environment.

PROGRAM EDUCATIONAL OUTCOMES (PEOs)

PEO 1: Learning: Our graduates to be competent with sound knowledge in field of


Computer Science & Engineering.

PEO 2: Employable: To develop the ability among students to synthesize data and
technical concepts for application to software product design for successful careers
that meet the needs of Indian and multinational companies.

PEO 3: Innovative: To develop research oriented analytical ability among students to


prepare them for making technical contribution to the society.

PEO 4: Entrepreneur / Contribution: To develop excellent leadership quality among


students which they can use at different levels according to their experience and
contribute for progress and development in the society.

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 2


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering

PROGRAM OUTCOMES (POs)

Engineering Graduates will be able to:

PO1: Engineering knowledge: Apply the knowledge of mathematics, science,


engineering fundamentals, and an engineering specialization to the solution of
complex engineering problems.
PO2: Problem analysis: Identify, formulate, review research literature, and

analyze complex engineering problems reaching substantiated conclusions using

first principles of mathematics, natural sciences, and engineering sciences.


PO3: Design/development of solutions: Design solutions for complex

engineering problems and design system components or processes that meet the
specified needs with appropriate consideration for the public health and safety, and
the cultural, societal, and environmental considerations.
PO4: Conduct investigations of complex problems: Use research-based

knowledge and research methods including design of experiments, analysis and


interpretation of data, and synthesis of the information to provide valid conclusions.
PO5: Modern tool usage: Create, select, and apply appropriate techniques,

resources, a n d modern engineering and IT tools including prediction and modelling to


complex engineering activities with an understanding of the limitations.
PO6: The engineer and society: Apply reasoning informed by the contextual

knowledge to assess societal, health, safety, legal and cultural issues and the
consequent responsibilities relevant to the professional engineering practice.
PO7: Environment and sustainability: Understand the impact of the professional

engineering solutions in societal and environmental contexts, and demonstrate the


knowledge of, and need for sustainable development.
PO8: Ethics: Apply ethical principles and commit to professional ethics and
responsibilities and norms of the engineering practice.
PO9: Individual and team work: Function effectively as an individual, and as a

member or leader in diverse teams, and in multidisciplinary settings.


PO10: Communication: Communicate effectively on complex engineering activities

with the engineering community and with society at large, such as, being able to

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 3


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering

comprehend and write effective reports and design documentation, make effective
presentations, and give and receive clear instructions.

PO11: Project management and finance: Demonstrate knowledge and understanding of


the engineering and management principles and apply these to one’s own work, as a
member and leader in a team, to manage projects and in multidisciplinary environments.

PO12: 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.

PROGRAM SPECIFIC OUTCOMES (PSOs)

PSO1: The ability to use standard practices and suitable programming environment to develop
software solutions.

PSO2: The ability to employ latest computer languages and platforms in creating innovative
career opportunities.

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 4


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering

COURSE OUTCOMES (COs)

C319.1 Implement C programs to demonstrate basic concepts of Computer Graphics.

C319.2 Create & formulate interactive Graphics for Line and circle drawing algorithms in
programming language.
C319.3 Create & formulate interactive Graphics for 2 D composite transformation in
programming language.
C319.4 Create & formulate interactive Graphics for 3 D composite transformation in
programming language.

CO-PO MAPPING
PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12

C319.1 2 2 1 1 1 1

C319.2 3 2 2 2 1 1 1

C319.3 2 1 2 1 1 1 1 1

C319.4 2 1 1 1 1 1 1

C319

CO-PSO MAPPING

PSO1 PSO2

C319.1 1

1
C319.2
C319.3

C319.4

C319

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 5


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering

LIST OF EXPERIMENTS

Expt. Title of experiment Corresponding


No. CO
1. Write a program to draw line using slope intercept equation. C319.1

2. Write a program to draw line using digital differential C319.2


analyzer.
3. Write a program to implement Bresenham’s line drawing C319.2
algorithm.
4. Write a program to implement scan convert using polar co- C319.2
ordinates.
5. Write a program to implement Bresenham’s circle drawing C319.2
algorithm.
6. Write a program to implement midpoint circle drawing C319.2
algorithm.
7. To Perform 2D Transformations such as Translation, C319.3
Rotation, Scaling, Reflection and shearing.
8. To Implement Cohen-Sutherland 2D clipping and window- C319.3
view port mapping.
9. To Implement Liang Barsky Line Clipping Algorithm C319.3

10. To Perform 3D Transformations such as Translation, C319.4


Rotation, Scaling.

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 6


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering

Content Beyond Syllabus


11. Write a Program to draw HUT or another Geometrical figure. C319.1

12. Write a Program to draw Decagon. C319.4

13. Write a program to demonstrate the polygon filling. C319.4

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 7


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering

INTRODUCTION

Computer graphics are pictures and films created using computers. Usually, the term refers to
computer-generated image data created with the help of specialized graphical hardware and
software. It is a vast and recently developed area of computer science. The phrase was coined in
1960, by computer graphics researchers Verne Hudson and William Fetter of Boeing. It is often
abbreviated as CG, though sometimes erroneously referred to as computer-generated imagery
(CGI).

Some topics in computer graphics include user interface design, sprite graphics, vector graphics,
3D modelling, shaders, GPU design, implicit surface visualization with ray tracing, and computer
vision, among others. The overall methodology depends heavily on the underlying sciences of
geometry, optics, and physics.

Computer graphics is responsible for displaying art and image data effectively and meaningfully
to the consumer. It is also used for processing image data received from the physical world.
Computer graphics development has had a significant impact on many types of media and has
revolutionized animation, movies, advertising, video games, and graphic design in general.
Pixel
In digital imaging, a pixel is a single point in a raster image. Pixels are placed on a regular 2-
dimensional grid, and are often represented using dots or squares. Each pixel is a sample of an
original image, where more samples typically provide a more accurate representation of the
original. The intensity of each pixel is variable; in color systems, each pixel has typically three
components such as red, green, and blue.
Graphics are visual presentations on a surface, such as a computer screen. Examples are
photographs, drawing, graphics designs, maps, engineering drawings, or other images. Graphics
often combine text and illustration. Graphic design may consist of the deliberate selection,
creation, or arrangement of typography alone, as in a brochure, flier, poster, web site, or book
without any other element. Clarity or effective communication may be the objective, association
with other cultural elements may be sought, or merely, the creation of a distinctive style.
Primitives
Primitives are basic units which a graphics system may combine to create more complex images
or models. Examples would be sprites and character maps in 2d video games, geometric
primitives in CAD, or polygons or triangles in 3d rendering. Primitives may be supported in
hardware for efficient rendering, or the building blocks provided by a graphics application.
Rendering
Rendering is the generation of a 2D image from a 3D model by means of computer programs. A
scene file contains objects in a strictly defined language or data structure; it would contain
geometry, viewpoint, texture, lighting, and shading information as a description of the virtual
scene. The data contained in the scene file is then passed to a rendering program to be processed
and output to a digital image or raster graphics image file. The rendering program is usually built
into the computer graphics software, though others are available as plug-ins or entirely separate
programs. The term "rendering" may be by analogy with an "artist's rendering" of a scene.
Although the technical details of rendering methods vary, the general challenges to overcome in
producing a 2D image from a 3D representation stored in a scene file are outlined as the graphics
pipeline along a rendering device, such as a GPU. A GPU is a device able to assist the CPU in
Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 8
Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering

calculations. If a scene is to look relatively realistic and predictable under virtual lighting, the
rendering software should solve the rendering equation. The rendering equation does not account
for all lighting phenomena, but is a general lighting model for computer-generated imagery.
'Rendering' is also used to describe the process of calculating effects in a video editing file to
produce final video output.
3D projection
3D projection is a method of mapping three dimensional points to a two dimensional plane. As
most current methods for displaying graphical data are based on planar two dimensional media,
the use of this type of projection is widespread, especially in computer graphics, engineering and
drafting.

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 9


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering

PREFACE

In order to develop efficient software systems, it is essential that efficient algorithms and
appropriate graphics are used. The purpose of this laboratory manual is to introduce
undergraduate students to techniques for developing efficient graphics and algorithms in a
systematic manner. The manual serves as a guide for learning and implementing the 2-
Diemensional and 3-Diemensional transformation in a programming language (C/C++). It
basically focuses on translation, scaling, rotation and various other operations on computer
graphics with algorithm analysis and design. The manual contains procedures, and pre-
experiment questions to help students prepare for experiments.

This practical manual will be helpful for students of Computer Science & Engineering for
understanding the course from the point of view of applied aspects. Though all the efforts
have been made to make this manual error free, yet some errors might have crept in
inadvertently. Suggestions from the readers for the improvement of the manual are most
welcomed.

Mr. Nikunj Kumar,


Assistant Professor, Dept. of CSE

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 10


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering

DO’S AND DONT’S

DO’s

1. Conform to the academic discipline of the department.


2. Enter your credentials in the laboratory attendance register.
3. Read and understand how to carry out an activity thoroughly before coming to the
laboratory.
4. Ensure the uniqueness with respect to the methodology adopted for carrying out the
experiments.
5. Shut down the machine once you are done using it.

DONT’S

1. Eatables are not allowed in the laboratory.


2. Usage of mobile phones is strictly prohibited.
3. Do not open the system unit casing.
4. Do not remove anything from the computer laboratory without permission.
5. Do not touch, connect or disconnect any plug or cable without your faculty/laboratory
Technician’s permission.

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 11


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering

GENERAL SAFETY INSTRUCTIONS

1. Know the location of the fire extinguisher and the first aid box and how to use them in
case of an emergency.

2. Report fire or accidents to your faculty /laboratory technician immediately.

3. Report any broken plugs or exposed electrical wires to your faculty/laboratory


technician immediately.

4. Do not plug in external devices without scanning them for computer viruses.

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 12


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering

DETAILS OF THE EXPERIMENTS CONDUCTED

(TO BE USED BY THE STUDENTS IN THEIR RECORDS)

DATE OF EXPT. TITLE OF THE MARKS FACULTY


PAGE
S. No No EXPERIMENT AWARDED SIGNATURE
CONDUCTION No.
(20) WITH
REMARK

10

11

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 13


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering

12

13

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 14


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering

GUIDELINES FOR LABORTORY RECORD PREPARATION

While preparing the lab records, the student is required to adhere to the following guidelines:

Contents to be included in Lab Records:


1. Cover page
2. Vision
3. Mission
4. PEOs
5. POs
6. PSOs
7. COs
8. CO-PO-PSO mapping
9. Index
10. Experiments
 Aim
 Source code
 Input-Output

A separate copy needs to be maintained for pre-lab written work.


The student is required to make the Lab File as per the format given on the next two pages.

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 15


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering

Raj Kumar Goel Institute of Technology, Ghaziabad

COMPUTER GRAPHICS LAB FILE (RCS 653)

Name

Roll No.

Section- Batch

Data Structures Using C/ Java Lab (RCS-355) Manual (CS, III SEM) Page 16
Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering

INDEX
Experiment Experiment Date of Date of Faculty
No. Name Conduction Submission Signature

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 17


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering

GUIDELINES FOR ASSESSMENT

Students are provided with the details of the experiment (Aim, pre-experimental questions,
procedure etc.) to be conducted in next lab and are expected to come prepared for each lab
class.
Faculty ensures that students have completed the required pre-experiment questions and they
complete the in-lab programming assignment(s) before the end of class. Given that the lab
programs are meant to be formative in nature, students can ask faculty for help before and
during the lab class.
Students’ performance will be assessed in each lab based on the following Lab Assessment
Components:
Assessment Criteria-1: Performance (Max. marks = 5)
Assessment Criteria-2: VIVA (Max. marks = 5)
Assessment Criteria-3: Record (Max. marks = 5)

In each lab class, students will be awarded marks out of 5 under each component head,
making it total out of 15 marks.

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 18


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering

EXPERIMENT - 1

Aim: Write a program to draw line using slope intercept equation.

Description:

To graph a linear equation in slope-intercept form, we can use the information given by that
form. For example, y=2x+3 tells us that the slope of the line is 2 and the y-intercept is at (0,3).
This gives us one point the line goes through, and the direction we should continue from that
point to draw the entire line.

Pre-Experiment Questions:

Q1. What is the slope?

Q2. What is the advantage of using slope intercept equations?

Q3. What is the slope intercept form of the equation of the line?

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 19


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering

Source Code:-

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int dx,dy,x1,x2,y1,y2,m,b,x,y,xnd;

/* request auto detection */


int gdriver = DETECT, gmode, errorcode;

/* initialize graphics mode */


initgraph(&gdriver, &gmode, "");

clrscr();
printf("enter x1,x2,y1,y2");
scanf("%d%d%d%d", &x1,&x2,&y1,&y2);
dx=x2-x1;
dy=y2-y1;
m=dy/dx;
b=y1-m*x1;

if(dx<0)
{
x=x2;
xnd=x1;
y=y2;
}
if(dx>0)
{
x=x1;
xnd=x2;
y=y1;
}
while(x<xnd)
{
putpixel(x,y,5);
x=x+1;
y=m*x+b;
}
getch();
}

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 20


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering

Output:-

Post-Experiment Questions:

Q1. How to implement this experiment in LINUX?

Q2. What are the challenges to face in this experiment?

Q3. How do you find the equation of a line with two coordinates?

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 21


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering

EXPERIMENT - 2

Aim: Write a program to draw line using digital differential analyzer.

Description:

In computer graphics, a digital differential analyzer (DDA) is hardware or software used for interpolation of
variables over an interval between start and end point. DDAs are used for rasterization of lines, triangles and
polygons. They can be extended to non linear functions, such as perspective correct texture mapping, quadratic
curves, and traversing voxels.

In its simplest implementation for linear cases such as lines, the DDA algorithm interpolates values in interval
by computing for each xi the equations xi = xi−1 + 1, yi = yi−1 + m, where Δx = xend − xstart and Δy = yend − ystart and
m = Δy/Δx

Pre-Experiment Questions:

Q1. How to compute line equations in DDA?

Q2. How many types line drawing algorithm?

Source Code:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int dx,dy,x1,x2,y1,y2,xin,yin,l;
float x,y,i;

/* request auto detection */


int gdriver = DETECT, gmode, errorcode;

/* initialize graphics mode */


initgraph(&gdriver, &gmode, "");

clrscr();
printf("enter x1,x2,y1,y2\n");
scanf("%d%d%d%d", &x1,&x2,&y1,&y2);
l=x2-x1;
dx=x2-x1;
dy=y2-y1;

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 22


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering

if (dy>l)
l=dy;
xin=dx/l;
yin=dy/l;
x=x1+0.5;
y=y1+0.5;

for(i=1;i<=l;i++)
{
putpixel(x,y,5);
x=x+xin;
y=y+yin;
}
getch();
}

Output:-

Post-Experiment Questions:

Q1 Implement DDA algorithm in coordinate between (0,0) and (6,6)?


Q2 What is rasterization computer graphics?

Q3. How to modify the program to from m>1 to m<1?

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 23


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering
EXPERIMENT - 3

Aim: Write a program to implement Bresenham’s line drawing algorithm

Descriptions:

Bresenham's line algorithm is an algorithm that determines the points of an n-dimensional


raster that should be selected in order to form a close approximation to a straight line between
two points. It is commonly used to draw line primitives in a bitmap image (e.g. on a computer
screen), as it uses only integer addition, subtraction and bit shifting, all of which are very
cheap operations in standard computer architectures. It is an incremental error algorithm. It is
one of the earliest algorithms developed in the field of computer graphics. An extension to the
original algorithm may be used for drawing circles.

While algorithms such as Wu's algorithm are also frequently used in modern computer
graphics because they can support antialiasing, the speed and simplicity of Bresenham's line
algorithm means that it is still important. The algorithm is used in hardware such as plotters
and in the graphics chips of modern graphics cards. It can also be found in many software
graphics libraries. Because the algorithm is very simple, it is often implemented in either the
firmware or the graphics hardware of modern graphics cards.

The label "Bresenham’s" is used today for a family of algorithms extending or modifying
Bresenham's original algorithm.

Pre-Experiment Questions:
Q1. Explain Bresenham’s Algo?.
Q2. How you compare between DDA and Bresenham’s line algo?

Source Code:-

#include<stdio.h>
#include<conio.h>
#include<graphics.h>

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 24


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering
void main()
{

int dx,dy,x1,x2,y1,y2,ds,d,x,y,xnd,ynd,dt;

/* request auto detection */


int gdriver = DETECT, gmode, errorcode;

/* initialize graphics mode */


initgraph(&gdriver, &gmode, "");

clrscr();

printf("enter x1,x2,y1,y2\n");
scanf("%d%d%d%d", &x1,&x2,&y1,&y2);
dx=x2-x1;
dy=y2-y1;
dt=2*(dy-dx);
ds=2*dy;
d=2*dy-dx;

if(dx<0)
{
x=x2;
xnd=x1;
y=y2;
ynd=y1;
}
else
{
x=x1;
xnd=x2;
y=y1;
ynd=y2;
}
while(x<xnd)
{
putpixel(x,y,0);
if(d<0)
{
d=d+2*dy;
}
else
{
d=d+2*(dy-dx);
y=y+1;
}

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 25


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering
x=x+1;
}
getch();
}

Output:-

Post-Experiment Questions:

Q1. Implement Bresenham’s Algo from coordinate (0,0) to (3,4) ?.

Q2. What are the advantages of Bresenham’s line drawing algorithm?

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 26


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering

EXPERIMENT - 4
Aim: Write a program to implement scan convert using polar co-ordinates.

Descriptions:

In mathematics, the polar coordinate system is a two-dimensional coordinate system in which


each point on a plane is determined by a distance from a reference point and an angle from a
reference direction.

The reference point (analogous to the origin of a Cartesian coordinate system) is called the
pole, and the ray from the pole in the reference direction is the polar axis. The distance from
the pole is called the radial coordinate or radius, and the angle is called the angular coordinate,
polar angle,

The process of representing continuous graphics objects as a collection of discrete pixels is


called scan conversion.

Scan conversion or scan converting rate is a video processing technique for changing the
vertical / horizontal scan frequency of video signal for different purposes and applications.
The device which performs this conversion is called a scan converter.

The application of scan conversion is wide and covers video projectors, cinema equipment,
TV and video capture cards, standard and HDTV televisions, LCD monitors, radar displays
and many different aspects of picture processing.

Pre-Experiment Question:

Q1. What is the principle of Scan Conversion?

Q2. Write polar coordinates of an scan conversion?

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 27


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering
Source Code:-

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int h,k,r,ang=0, angnd=2.1214;
float i=0.1;

/* request auto detection */


int gdriver = DETECT, gmode, errorcode;

/* initialize graphics mode */


initgraph(&gdriver, &gmode, "");

clrscr();
printf("enter h,k,r\n");
scanf("%d%d%d", &h,&k,&r);

while(angnd>ang)

{
x = r * cos ( ang) ;
y = r * sin (ang);
putpixel(x+h,y+k,5);
putpixel(y+h,x+k,5);
putpixel(-y+h,x+k,5);
putpixel(-x+h,y+k,5);
putpixel(-x+h,-y+k,5);
putpixel(-y+h,-x+k,5);
putpixel(y+h,-x+k,5);
putpixel(x+h,-y+k,5);
ang= ang + i;
}

getch();
}

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 28


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering

Output: -

Post-Experiment Questions

Q1. What is the advantage of scan Conversion ?


Q2. What is the type of circle generating algo?

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 29


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering

EXPERIMENT – 5

Aim: Write a program to implement Bresenham’s circle drawing algorithm .

Descriptions:

Now for each pixel, we will do the following operations:


1. Set initial values of (xc, yc) and (x, y)
2. Set decision parameter d to d = 3 – (2 * r).
3. call drawCircle(int xc, int yc, int x, int y) function.
4. Repeat steps 5 to 8 until x < = y
5. Increment value of x.
6. If d < 0, set d = d + (4*x) + 6
7. Else, set d = d + 4 * (x – y) + 10 and decrement y by 1.
8. call drawCircle(int xc, int yc, int x, int y) function
9. drawCircle() function:

Pre-Experiment Questions:
Q1. What is the difference between Bresenham’s circle drawing and midpoint circle?
Q2. What is symmetry concept of Circle? .

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 30


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering

Source code:-
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int h,k,x,y,r,d;

/* request auto detection */


int gdriver = DETECT, gmode, errorcode;

/* initialize graphics mode */


initgraph(&gdriver, &gmode, "");

clrscr();
printf("enter h,k,y\n");
scanf("%d%d%d", &h,&k,&y);
r=y;
d=3-2*r;
x=0;
while(x<y)
{
if(d<0)
{
d=d+4*x+6;
x=x+1;
}
else
{
d=d+4*(x-y)+10;
y=y-1;
x=x+1;
}
putpixel(x+h,y+k,5);
putpixel(y+h,x+k,5);
putpixel(-y+h,x+k,5);
putpixel(-x+h,y+k,5);
putpixel(-x+h,-y+k,5);
putpixel(-y+h,-x+k,5);
putpixel(y+h,-x+k,5);
putpixel(x+h,-y+k,5);
}
getch();
}

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 31


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering

Output: -

Post-Experiment Questions:

Q1. What is the application of circle generating Algo?


Q2. What do you understand by Pixel?

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 32


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering

EXPERIMENT - 6

Aim: Write a program to implement midpoint circle drawing algorithm.

Descriptions:
In computer graphics, the midpoint circle algorithm is an algorithm used to determine the
points needed for rasterizing a circle. Bresenham's circle algorithm is derived from the
midpoint circle algorithm. The algorithm can be generalized to conic sections.

This algorithm draws all eight octants simultaneously, starting from each cardinal direction
(0°, 90°, 180°, 270°) and extends both ways to reach the nearest multiple of 45° (45°, 135°,
225°, 315°). It can determine where to stop because when y = x, it has reached 45°. The
reason for using these angles is shown in the above picture: As y increases, it does not skip
nor repeat any y value until reaching 45°. So during the while loop, y increments by 1 each
iteration, and x decrements by 1 on occasion, never exceeding 1 in one iteration. This changes
at 45° because that is the point where the tangent is rise=run. Whereas rise>run before and
rise<run after.

Pre-Experiment Questions:
Q1. Compare between Bresenham’s circle drawing and midpoint circle.
Q2. What is the application of circle generating Algo?

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 33


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering
Source Code:-

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int h,k,x=0,y,r,d;

/* request auto detection */


int gdriver = DETECT, gmode, errorcode;

/* initialize graphics mode */


initgraph(&gdriver, &gmode, "");

clrscr();
printf("enter h,k,y\n");
scanf("%d%d%d", &h,&k,&y);
r=y;
d=1-r;
while(x<y)
{
if(d<0)
{
d=d+2*x+3;
}
else
{
d=d+2*(x-y)+5;
y=y-1;
}
x=x+1;
putpixel(x+h,y+k,5);
putpixel(y+h,x+k,5);
putpixel(-y+h,x+k,5);
putpixel(-x+h,y+k,5);
putpixel(-x+h,-y+k,5);
putpixel(-y+h,-x+k,5);
putpixel(y+h,-x+k,5);
putpixel(x+h,-y+k,5);
}
getch();
}

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 34


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering

Output:-

Post-Experiment Questions:
Q1. What is decision parameter computer graphics?
Q2. Why midpoint circle algorithm is so called?

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 35


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering

EXPERIMENT - 7

Aim: To Perform 2D Transformations such as Translation, Rotation, Scaling, Reflection and shearing

Descriptions:

Translation
A translation moves an object to a different position on the screen. You can translate a point in
2D by adding translation coordinate (t x, ty) to the original coordinate (X, Y) to get the new
coordinate (X’, Y’).

Rotation
In rotation, we rotate the object at particular angle θ (theta) from its origin. From the following
figure, we can see that the point P(X, Y) is located at angle φ from the horizontal X coordinate
with distance r from the origin.

Let us suppose you want to rotate it at the angle θ. After rotating it to a new location, you will
get a new point P’ (X’, Y’).

Scaling
To change the size of an object, scaling transformation is used. In the scaling process, you either
expand or compress the dimensions of the object. Scaling can be achieved by multiplying the
original coordinates of the object with the scaling factor to get the desired result.

Reflection
Reflection is the mirror image of original object. In other words, we can say that it is a rotation
operation with 180°. In reflection transformation, the size of the object does not change.

Shear
A transformation that slants the shape of an object is called the shear transformation. There are
two shear transformations X-Shear and Y-Shear. One shifts X coordinates values and other
shifts Y coordinate values. However; in both the cases only one coordinate changes its
coordinates and other preserves its values. Shearing is also termed as Skewing.

Pre-Experiment Questions:

Q1. The essential condition which is checked before insertion in a linked queue is?
Q2. The essential condition which is checked before deletion in a linked queue is?

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 36


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering
Source Code:-

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<dos.h>
#include<stdlib.h>
#include<graphics.h>
void scaleline(int x1,int y1,int x2,int y2)
{
float sx,sy;
printf("enter scaling factor(sx,sy):");
scanf("%f%f",&sx,&sy);
line(x1,y1,x2,y2);
x1=sx*x1;
y1=sy*y1;
x2=sx*x2;
y2=sy*y2;
sleep(1);
line(x1,y1,x2,y2);
}
void translateline(int x1,int y1,int x2,int y2,int tx,int ty)
{
x1=x1+tx;
x2=x2+tx;
y1=y1+ty;
y2=y2+ty;
line(x1,y1,x2,y2);
}
void rotateline(int x1,int y1,int x2,int y2,int a)
{
x1=x1*cos(a)+y1*sin(a);
y1=-1*x1*sin(a)+y1*cos(a);
x2=x2*cos(a)+y2*cos(a);
y2=-1*x2*sin(a)+y2*cos(a);
line(x1,y1,x2,y2);
}
\\

void scalecircle(int xc,int yc,int r)


{
float sy;
printf("\n enter scaling factor(sy)\n:");
scanf("%f",&sy);
r=sy*r;
circle(xc,yc,r);
}
void translatecircle(int xc,int yc,int r,int tx,int ty)
{
Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 37
Raj Kumar Goel Institute of Technology, Ghaziabad
xc=xc+tx; Department of Computer Science & Engineering
yc=yc+ty;
circle(xc,yc,r);
}
void rotatecircle(int xc,int yc,int r,int a)
{
xc=xc*cos(a)+yc*sin(a);
yc=-1*xc*sin(a)+yc*cos(a);
circle(xc,yc,r);
}
void main()
{
int gd=DETECT,gm,m,n,x1,y1,x2,y2,xc,yc,r,tx,ty,a;
float sx,sy;
clrscr();
initgraph(&gd,&gm,"");
error:printf("\n0.exit\n1.scaling\n3.rotation\n");
printf("\nenter your choice:");
scanf("%d",&m);
if(m<0&&m>3)
{
printf("incrrect choice");
goto error;
}
if(m==0)
exit(0);
error1:printf("\n1.Line\n2.Circle\n");
printf("\nenter your choice:");
scanf("%d",&n);
if(n<0&&m>1)
{
printf("incorrect choice for component");
goto error1;
}

switch(m)
{
case 1:switch(n)
{
case 1:scaleline(x1,y1,x2,y2);;
break;
case 2:scalecircle(xc,yc,r);
break;
}
break;

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 38


Raj Kumar Goel Institute of Technology, Ghaziabad
case 2: Department of Computer Science & Engineering
printf("\n enter translating factor(tx,ty)");
scanf("%d%d",&tx,&ty);
switch(n)
{
case 1:translateline(x1,y1,x2,y2,tx,ty);
break;
case 2:translatecircle(xc,yc,r,tx,ty);
break;
}
break;
case 3:
printf("\n enter rotating angle");
scanf("%d",&a);
switch(n)
{
case 1:rotateline(x1,y1,x2,y2,a);
break;
case 2:rotatecircle(xc,yc,r,a);
break;
}
break;
}
switch(n)
{
case 1:
printf("enter coordinates of line end points(x1,y1,x2,y2)\n");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
line(x1,y1,x2,y2);
break;
case 2:
printf("enter coordinates of center of circle &
radius\n"); scanf("%d%d%d",&xc,&yc,&r);
circle(xc,yc,r);
break;
}
getch();
closegraph();
restorecrtmode();
}

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 39


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering

Output:-

Post-Experiment Questions:

Q1. What is the matrix form of Translation?


Q2. How we differentiate between skewing and shearing?

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 40


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering

EXPERIMENT - 8

Aim: To Implement Cohen-Sutherland 2D clipping and window-view port mapping.

Description:

The Cohen–Sutherland algorithm is a computer-graphics algorithm used for line clipping. The
algorithm divides a two-dimensional space into 9 regions and then efficiently determines the lines
and portions of lines that are visible in the central region of interest


Both endpoints are in the viewport region (bitwise OR of endpoints = 00): trivial accept.

Both endpoints share at least one non-visible region, which implies that the line does not
cross the visible region. (Bitwise AND of endpoints ≠ 0): trivial reject.


Both endpoints are in different regions: in case of this nontrivial situation the algorithm
finds one of the two points that is outside the viewport region (there will be at least one
point outside). The intersection of the outpoint and extended viewport border is then
calculated (i.e. with the parametric equation for the line), and this new point replaces the
outpoint. The algorithm repeats until a trivial accepts or reject occurs.

The numbers in the figure below are called outcodes. An outcode is computed for each of
the two points in the line. The outcode will have 4 bits for two-dimensional clipping, or 6
bits in the three-dimensional case. The first bit is set to 1 if the point is above the
viewport. The bits in the 2D outcode represent: top, bottom, and right, left. For example,
the outcode 1010 represents a point that is top-right of the viewport.

left central right

top 1001 1000 1010

central 0001 0000 0010

bottom 0101 0100 0110

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 41


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering

Note that the outcodes for endpoints must be recalculated on each iteration after the
clipping occurs.

The Cohen–Sutherland algorithm can be used only on a rectangular clip window. For
other convex polygon clipping windows, use the Cyrus–Beck algorithm.

Pre-Experiment Questions:

Q1. What is Cohen Sutherland line clipping algorithm?

Q2. What is 2d clipping?

Source Code:-

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<math.h>

void clip(float,float,float);

int i,j=0,n;

int rx1,rx2,ry1,ry2;

float x1[8],y1[8];

void main()

int gd=DETECT,gm;

int i,n;

float x[8],y[8],m;

clrscr();

initgraph(&gd,&gm,"");

printf("coordinates for rectangle : ");

scanf("%d%d%d%d",&rx1,&ry1,&rx2,&ry2);

printf("no. of sides for polygon : ");

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 42


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering

scanf("%d",&n);

printf("coordinates : ");

for(i=0;i<n;i++)

scanf("%f%f",&x[i],&y[i]);

cleardevice();

outtextxy(10,10,"Before clipping");

outtextxy(10,470,"Press any key....");

rectangle(rx1,ry1,rx2,ry2);

for(i=0;i<n-1;i++)

line(x[i],y[i],x[i+1],y[i+1]);

line(x[i],y[i],x[0],y[0]);

getch();

cleardevice();

for(i=0;i<n-1;i++)

m=(y[i+1]-y[i])/(x[i+1]-x[i]);

clip(x[i],y[i],m);

clip(x[0],y[0],m);

outtextxy(10,10,"After clipping");

outtextxy(10,470,"Press any key....");

rectangle(rx1,ry1,rx2,ry2);

for(i=0;i<j-1;i++)

line(x1[i],y1[i],x1[i+1],y1[i+1]);

getch();

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 43


Raj Kumar Goel Institute of Technology, Ghaziabad
} Department of Computer Science & Engineering

void clip(float e,float f,float m)

while(e<rx1 e>rx2 f<ry1 f>ry2)

if(e<rx1)

f+=m*(rx1-e);

e=rx1;

else if(e>rx2)

f+=m*(rx2-e);

e=rx1;

if(f<ry1)

e+=(ry1-f)/m;

f=ry1;

else if(f>ry2)

e+=(ry2-f)/m;

f=ry2;

x1[j]=e;

y1[j]=f;

j++;

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 44


Raj Kumar Goel Institute of Technology, Ghaziabad
} Department of Computer Science & Engineering

Output:-
Enter the x1 & y1 coordinates:
X1:70
Y1:80
Enter the x2 & y2 coordinates:
X2 : 250
Y2 : 280

Post-Experiment Questions:

Q1. What is 2d viewing in computer graphics?

Q2. What are the types of clipping?

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 45


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering

EXPERIMENT - 9

Aim: To Implement Liang Barsky Line Clipping Algorithm

Descriptions:

In computer graphics, the Liang–Barsky algorithm (named after You-Dong Liang and Brian A.

Barsky) is a line clipping algorithm. The Liang–Barsky algorithm uses the parametric equation of a

line and inequalities describing the range of the clipping window to determine the intersections

between the line and the clip window. With these intersections it knows which portion of the line

should be drawn. This algorithm is significantly more efficient than Cohen–Sutherland. The idea of

the Liang–Barsky clipping algorithm is to do as much testing as possible before computing line

intersections.

To compute the final line segment:

1. A line parallel to a clipping window edge has {\displaystyle p_{i}=0}for that boundary.
2. If for that{\displaystyle i} {\displaystyle q_{i}<0}then the line is completely outside and can be
eliminated.
3. When {\displaystyle p_{i}<0}the line proceeds outside to inside the clip window, and
when {\displaystyle p_{i}>0} the line proceeds inside to outside.
4. For nonzero{\displaystyle p_{k}} {\displaystyle u=q_{i}/p_{i}}gives the intersection point.

Pre-Experiment Questions

Q1. What is line clipping algorithm?


Q2. What is CG clipping?

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 46


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering

Source Code:-

#include<iostream>
#include<graphics.h>
#include<math.h>

using namespace std;

/ this function gives the maximum


float maxi(float arr[],int n) {
float m = 0;
for (int i = 0; i < n; ++i)
if (m < arr[i])
m = arr[i];
return m;
}

/ this function gives the minimum


float mini(float arr[], int n) {
float m = 1;
for (int i = 0; i < n; ++i)
if (m > arr[i])
m = arr[i];
return m;
}

void liang_barsky_clipper(float xmin, float ymin, float xmax, float


ymax, float x1, float y1, float x2, float y2)
{
/ defining variables
float p1 = -(x2 -
x1); float p2 = -p1;
float p3 = -(y2 -
y1); float p4 = -p3;

float q1 = x1 - xmin;
float q2 = xmax - x1;
float q3 = y1 - ymin;
float q4 = ymax - y1;

float posarr[5], negarr[5];


int posind = 1, negind = 1;
Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 47
Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering
posarr[0] = 1;
negarr[0] = 0;

rectangle(xmin, 467 - ymin, xmax, 467 - ymax); // drawing the


clipping window
if ((p1 == 0 && q1 < 0) || (p3 == 0 && q3 < 0)) {

outtextxy(80, 80, "Line is parallel to clipping window!");


return;
}
if (p1 != 0) {
float r1 = q1 / p1;
float r2 = q2 / p2;
if (p1 < 0) {
negarr[negind++] = r1; // for negative p1, add it to negative array
posarr[posind++] = r2; // and add p2 to positive array
} else {
negarr[negind++] = r2;
posarr[posind++] = r1;
}
}
if (p3 != 0) {
float r3 = q3 / p3;
float r4 = q4 / p4;
if (p3 < 0) {
negarr[negind++] = r3;
posarr[posind++] = r4;
} else {
negarr[negind++] = r4;
posarr[posind++] = r3;
}
}
float xn1, yn1, xn2, yn2;

float rn1, rn2;


rn1 = maxi(negarr, negind); // maximum of negative array
rn2 = mini(posarr, posind); // minimum of positive array
if (rn1 > rn2) { // reject

outtextxy(80, 80, "Line is outside the clipping window!");


return;
}
xn1 = x1 + p2 * rn1;

yn1 = y1 + p4 * rn1; // computing new points


xn2 = x1 + p2 * rn2; Page 48

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM)


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering
yn2 = y1 + p4 * rn2;

setcolor(CYAN);

line(xn1, 467 - yn1, xn2, 467 - yn2); // the drawing the new line

setlinestyle(1, 1, 0);

line(x1, 467 - y1, xn1, 467 - yn1);


line(x2, 467 - y2, xn2, 467 - yn2);
}

int main() {
cout << "\nLiang-barsky line clipping";
cout << "\nThe system window outlay is: (0,0) at bottom left and (631,
467) at top right";
cout << "\nEnter the co-ordinates of the window(wxmin, wxmax,
wymin, wymax):";
float xmin, xmax, ymin, ymax;
cin >> xmin >> ymin >> xmax >> ymax;
cout << "\nEnter the end points of the line (x1, y1) and (x2,
y2):"; float x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;

int gd = DETECT, gm;

/ using the winbgim library for C++, initializing the graphics


mode initgraph(&gd, &gm, "");
liang_barsky_clipper(xmin, ymin, xmax, ymax, x1, y1, x2,
y2); getch();
closegraph();
}

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 49


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering

Output:

Post-Experiment Questions:

Q1. Disadvantage of using Liang Barsky Line Clipping Algorithm?


Q2. How to overcome problem which arises using this Algorithm?

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 50


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering
EXPERIMENT - 10

Aim: To Perform 3D Transformations such as Translation, Rotation, Scaling.

Description:.

Pre-Experiment Questions:

Q1. What is Transformation computer graphics?

Q2. What is composite transformation in CG?

Source Code:-

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>

float tx,ty,tz,sx,sy,sz,r;
float x[15][15],y[15][15],z[15][15];
int i,j=2,ch;

void transform()
{
x[i][j-1]=x[i][j-1]+tx;
x[i][j]=x[i][j]+tx;
y[i][j-1]=y[i][j-1]+ty;
y[i][j]=y[i][j]+ty;
z[i][j-1]=z[i][j-1]+tz;
z[i][j]=z[i][j]+tz;
}

void rotate()
{

if(i<=8)
{
x[i][j-1]=(x[i][j-1]*cos(r))-(y[i][j-1]*sin(r));
y[i][j-1]=(x[i][j-1]*sin(r))+(y[i][j-1]*cos(r));
x[i][j]=(x[i][j]*cos(r))-(y[i][j]*sin(r));
y[i][j]=(x[i][j]*sin(r))+(y[i][j]*cos(r));
}
else
Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 51
Raj Kumar Goel Institute of Technology, Ghaziabad
{ Department of Computer Science & Engineering
z[i][j-1]=(z[i][j-1]*cos(r))-(y[i][j-1]*sin(r));
y[i][j-1]=(z[i][j-1]*sin(r))+(y[i][j-1]*cos(r));
z[i][j]=(z[i][j]*cos(r))-(y[i][j]*sin(r)); y[i]
[j]=(z[i][j]*sin(r))+(y[i][j]*cos(r)); }

}
void scale()
{
x[i][j-1]=x[i][j-1]*sx;
x[i][j]=x[i][j]*sx;
y[i][j-1]=y[i][j-1]*sy;
y[i][j]=y[i][j]*sy;
z[i][j-1]=z[i][j-1]*sz;
z[i][j]=z[i][j]*sz;
}

int main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
x[1][1]=200,x[1][2]=250,y[1][1]=200,y[1][2]=200;
x[2][1]=200,x[2][2]=200,y[2][1]=200,y[2][2]=150;
x[3][1]=200,x[3][2]=250,y[3][1]=150,y[3][2]=150;
x[4][1]=250,x[4][2]=250,y[4][1]=150,y[4][2]=200;
x[5][1]=220,x[5][2]=270,y[5][1]=140,y[5][2]=140;
x[6][1]=270,x[6][2]=270,y[6][1]=140,y[6][2]=190;
x[7][1]=220,x[7][2]=220,y[7][1]=140,y[7][2]=190;
x[8][1]=220,x[8][2]=270,y[8][1]=190,y[8][2]=190;
z[9][1]=200,z[9][2]=220,y[9][1]=150,y[9][2]=140;
z[10][1]=250,z[10][2]=270,y[10][1]=150,y[10][2]=140;
z[11][1]=250,z[11][2]=270,y[11][1]=200,y[11][2]=190;
z[12][1]=220,z[12][2]=200,y[12][1]=190,y[12][2]=200;
textattr(0);

for(i=1;i<=12;i++)
{
if(i<=8)
line(x[i][j-1],y[i][j-1],x[i][j],y[i][j]);

else
line(z[i][j-1],y[i][j-1],z[i][j],y[i][j]);
}

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 52


Raj Kumar Goel Institute of Technology, Ghaziabad
while(ch!=4) Department of Computer Science & Engineering
{
printf("\n 3D-COMPOSITE TRANSFORMATION");
printf("\n1.TRANSLATION & ROTATION");
printf("\n2.TRANSLATION & SCALING");
printf("\n3.SCALING & ROTATION");
printf("\n4.EXIT");
printf("\nENTER YOUR CHOICE:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nENTER X Y & Z VALUES:");
scanf("%f%f%f",&tx,&ty,&tz);
printf("\nENTER ROTATION ANGLE:");
scanf("%f",&r);
clrscr();
r=(r*3.14/180);
for(i=1;i<=12;i++)
{
transform();
rotate();
if(i<=8)
line(x[i][j-1],y[i][j-1],x[i][j],y[i][j]);
else
line(z[i][j-1],y[i][j-1],z[i][j],y[i][j]);
delay(500);
}
break;
case 2:
printf("\nENTER X Y & Z VALUES:");
scanf("%f%f%f",&tx,&ty,&tz);
printf("\nENTER X Y & Z VECTORS:");
scanf("%f%f%f",&sx,&sy,&sz);
clrscr();

for(i=1;i<=12;i++)
{
transform();
scale();
if(i<=8)
line(x[i][j-1],y[i][j-1],x[i][j],y[i][j]);
else
line(z[i][j-1],y[i][j-1],z[i][j],y[i][j]);
delay(500);
Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 53
Raj Kumar Goel Institute of Technology, Ghaziabad
} Department of Computer Science & Engineering
break;

case 3:
printf("\nENTER X Y & Z VECTORS:");
scanf("%f%f%f",&sx,&sy,&sz);
printf("\nENTER ROTATION ANGLE:");
scanf("%f",&r);
clrscr();
r=(r*3.14/180);
for(i=1;i<=12;i++)
{
scale();
rotate();
if(i<=8)
line(x[i][j-1],y[i][j-1],x[i][j],y[i][j]);
else
line(z[i][j-1],y[i][j-1],z[i][j],y[i][j]);
delay(500);
}
break;

case 4:
printf("\nEND OF PROGRAM");
exit(0);
break;
}
}
getch();
return 0;
}

OUTPUT:

Post-Experiment Questions:
Q1. What is viewing transformation in computer graphics?
Q2. What is a scaling transformation?

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 54


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering

EXPERIMENT - 11

Aim: Write a Program to draw HUT or another Geometrical figure

Description:

A figure drawing is a drawing of the human form in any of its various shapes and postures using any of the
drawing media. The term can also refer to the act of producing such a drawing. The degree of representation
may range from highly detailed, anatomically correct renderings to loose and expressive sketches. A "life
drawing" is a drawing of the human figure from observation of a live model. A figure drawing may be a
composed work of art or a figure study done in preparation for a more finished work such as a painting
Figure drawing is arguably the most difficult subject an artist commonly encounters, and entire courses are
dedicated to the subject. The human figure is one of the most enduring themes in the visual arts, and the
human figure can be the basis of portraiture, illustration, sculpture, medical illustration, and other fields.

Pre-Experiment Questions:

Q1. What do you understand by Animation?


Q2. Write the various names of geometrical figures?

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 55


Raj Kumar Goel Institute of Technology, Ghaziabad
Source Code:- Department of Computer Science & Engineering

#include<conio.h>
#include<iostream.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
#include<process.h>
void main()
{
int graphdriver=DETECT,graphmode;
initgraph(&graphdriver,&graphmode,"...\\bgi");

line(100,100,150,50);
line(150,50,200,100);
line(100,100,200,100);
line(150,50,350,50);
line(200,100,350,100);
line(350,50,350,100);
circle(150,75,10);
rectangle(100,100,200,300);
rectangle(200,100,350,300);
rectangle(250,175,300,225);
line(250,175,300,225);
line(300,175,250,225);
line(125,300,125,225);
line(175,300,175,225);
arc(150,225,0,180,25);
getch();
closegraph();

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 56


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering

Output:-

Post-Experiment Questions

Q1. What do you understand by polygon filling?

Q2.Draw the HUT in LINUX Platform?

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 57


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering
EXPERIMENT - 12

Aim: Write a program to draw a decagon.


Description:

Pre-Experiment Questions:
Q1. Describe blobby objects?

Q2. Reflection can be possible in 3-Diemension?

Algorithm

Source Code:-

#include<stdio.h>
#include<conio.h>
#include<graphics.h>

void main()
{

/* request auto detection */


int gdriver = DETECT, gmode, errorcode;

/* initialize graphics mode */


initgraph(&gdriver, &gmode, "");

line(100,100,200,20);
line(200,20,25,250);
line(25,250,175,175);
line(175,175,230,330);
line(230,330,132,123);
line(132,123,210,190);
line(210,190,321,234);
line(321,234,345,267);
line(345,267,324,354);
line(324,354,100,100);

getch();
}

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 58


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering

Output:-

Post-Experiment Questions:
Q1. What do you understand by color filing?
Q2. What is B-Spline?

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 59


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering

EXPERIMENT - 13

Aim: Write a program to demonstrate the polygon filling.

Description

Source Code:-

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

void main()

int i,maxx,maxy

/* request auto detection */

int gdriver = DETECT, gmode, errorcode;

/* initialize graphics mode */

initgraph(&gdriver, &gmode, "");

clrscr();

int poly[8];

maxx=getmaxx();

maxy=getmaxy();

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 60


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering

poly[0]=20;

poly[1]=maxy/20;

poly[2]=maxx-20;

poly[3]=20;

poly[4]=maxx-50;

poly[5]=maxx-20;

poly[6]=maxx-2;

poly[7]=maxx-2;

for(i=empty_fill; i<user_fill; i++)

setfillstyle(i,getmaxcolor());

fillpoly(4,poly);

getch();

closegraph();

return;

Output:-

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 61


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering

Post-Experiment Questions:
Q1. What do you understand by Polygon filing?
Q2. What is Bezier Curve?

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 62


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering
References

1. Donald Hearn and M Pauline Baker, “Computer Graphics C Version”, Pearson

Education

2. Amrendra N Sinha and Arun D Udai,” Computer Graphics”, Tata MCGraw Hill.

3. Donald Hearn and M Pauline Baker, “Computer Graphics with OpenGL”, Pearson

education

4. R.K. Maurya, “Computer Graphics ” Wiley Dreamtech Publication.

5. Rogers, “ Procedural Elements of Computer Graphics”, McGraw Hill

6. Mukherjee, Fundamentals of Computer graphics & M ultimedia, PHI Learning Private

Limited, Delhi India.

7. Foley, Vandam, Feiner, Hughes –“Computer Graphics principle”, Pearson

Education.

8. W. M. Newman, R. F. Sproull –“Principles of Interactive computer Graphics” –

Tata MCGraw Hill.

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 63


Raj Kumar Goel Institute of Technology, Ghaziabad
Department of Computer Science & Engineering

APPENDIX

AKTU SYLLABUS

RCS 653: COMPUTER GRAPHICS LAB


Program in C or C++ for following:

1. To implement DDA algorithms for line and circle.

2. To implement Bresenham’s algorithms for line, circle and ellipse drawing

3. To implement Mid Point Circle algorithm using C .

4. To implement Mid Point Ellipse algorithm using C .

5. To perform 2D Transformations such as translation, rotation, scaling, reflection.

6. To implement Cohen–Sutherland 2D clipping and window–viewport mapping.

7. To implement Liang Barksy Line Clipping Algorithm.

8. To perform 3D Transformations such as translation, rotation and scaling.

9. To convert between color models.

10. To perform animation using any Animation software

11. To perform basic operations on image using any image editing software

12. To draw different shapes such as hut, face, kite, fish etc.

Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 64

You might also like