Laboratory Manual: Computer Graphics Lab

Download as pdf or txt
Download as pdf or txt
You are on page 1of 58

LABORATORY MANUAL

Computer Graphics Lab


LIST OF EXPERIMENTS

Expt. Title of experiment


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

2. Write a program to draw line using digital differential


analyzer.
3. Write a program to implement Bresenham’s line drawing
algorithm.
4. Write a program to implement scan convert using polar co-
ordinates.
5. Write a program to implement Bresenham’s circle drawing
algorithm.

6. Write a program to implement midpoint circle drawing


algorithm.

7. To Perform 2D Transformations such as Translation,


Rotation, Scaling, Reflection and shearing.

8. To Implement Cohen-Sutherland 2D clipping and window-


view port mapping.

9. To Implement Liang Barsky Line Clipping Algorithm

10. To Perform 3D Transformations such as Translation,


Rotation, Scaling.

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


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


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


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.

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


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


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


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 13


COMPUTER GRAPHICS LAB FILE (RCS 653)

Name

Roll No.

Section- Batch

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


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

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


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 16


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 17


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 18


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 19


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 20


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 21


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 22


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 23


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 24


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 25


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 26


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 27


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 28


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 29


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 30


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 31


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 32


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 33


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 (tx, 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 34


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 35
xc=xc+tx;
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 36


case 2:
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 37


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 38


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 39


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 40


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 41


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 42


}

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 43


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 44


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 45
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;
Computer Graphics Lab (RCS-653) Manual (CS, VI SEM) Page 46
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 47


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 48


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 49
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 50


while(ch!=4)
{
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 51
}
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 52


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 53


Source Code:-

#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 54


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 55


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 56


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 57


EXPERIMENT - 13

Aim: Write a program to demonstrate the polygon filling.

Description
Polygon is an ordered list of vertices as shown in the following figure. For filling polygons with
particular colors, you need to determine the pixels falling on the border of the polygon and those which
fall inside the polygon. In this chapter, we will see how we can fill polygons using different techniques.
1. Scan line Algo
2. Flood fill Algo
3. Boundary fill Algo

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

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


maxx=getmaxx();

maxy=getmaxy();

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 59


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 60


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 & Multimedia, 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 61


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 62

You might also like