You are on page 1of 30

COMPUTER GRAPHICS 1

SOFTWARE LAB VII (CS-319)

Practical no.1
Introduction to computer graphics.
Computer Graphics are graphics created using computers and, more
generally,
the representation and manipulation of image data by
a computer with
help
from
specialized software and hardware.
The
development of computer graphics has made computers easier to interact
with, and better for understanding and interpreting many types of data.
Developments in computer graphics have had a profound impact on many
types of media and have revolutionized animation, movies and the video
game industry. The term computer graphics includes almost everything on
computers that is not text or sound. Today almost every computer can do
some graphics, and people have even come to expect to control their
computer through icons and pictures rather than just by typing.
Here in our lab at the Program of Computer Graphics, we think of computer
graphics as drawing pictures on computers, also called rendering. The
pictures can be photographs, drawings, movies, or simulations -- pictures of
things which do not yet exist and maybe could never exist. Or they may be
pictures from places we cannot see directly, such as medical images from
inside your body.
We spend much of our time improving the way computer pictures can
simulate real world scenes. We want images on computers to not just look
more realistic, but also to BE more realistic in their colors, the way objects
and rooms are lighted, and the way different materials appear. We call this
work "realistic image synthesis", and the following series of pictures will
show some of our techniques in stages from very simple pictures through
very realistic ones.
Subfields in computer graphics
A broad classification of major subfields in computer graphics might be:
1. Geometry: studies ways to represent and process surfaces
2. Animation: studies with ways to represent and manipulate motion
3. Rendering: studies algorithms to reproduce light transport

SATWINDER SINGH

|| 100210390828

COMPUTER GRAPHICS 2
SOFTWARE LAB VII (CS-319)

4. Imaging: studies image acquisition or image editing


Geometry
Successive approximations of a surface computed using quadric error
metrics. The subfield of geometry studies the representation of threedimensional objects in a discrete digital setting. Because the appearance of
an object depends largely on its exterior, boundary representations are most
commonly used. Two dimensional surfaces are a good representation for
most objects, though they may be non-manifold. Since surfaces are not
finite, discrete digital approximations are used. Polygonal meshes (and to a
lesser extent subdivision surfaces) are by far the most common
representation, although point-based representations have become more
popular recently (see for instance the Symposium on Point-Based Graphics).
These representations are Lagrangian, meaning the spatial locations of the
samples are independent. Recently, Eulerian surface descriptions (i.e., where
spatial samples are fixed) such as level sets have been developed into a
useful representation for deforming surfaces which undergo many
topological changes (with fluids being the most notable example).

Geometry Subfields

Implicit surface modeling - an older subfield which examines the use of


algebraic surfaces, constructive
representation.

solid

geometry,

etc.,

for

surface

Digital geometry processing - surface reconstruction, simplification,


fairing, mesh repair, parameterization, re-meshing, mesh generation,
surface compression, and surface editing all fall under this heading.

Discrete differential geometry - a nascent field which defines geometric


quantities for the discrete surfaces used in computer graphics.
SATWINDER SINGH

|| 100210390828

COMPUTER GRAPHICS 3
SOFTWARE LAB VII (CS-319)

Point-based graphics - a recent field which focuses on points as the


fundamental representation of surfaces.

Subdivision surfaces

Out-of-core mesh processing - another recent field which focuses on

mesh datasets that do not fit in main memory.


Animation
The subfield of animation studies descriptions for surfaces (and other
phenomena) that move or deform over time. Historically, most work in this
field has focused on parametric and data-driven models, but
recently physical simulation has become more popular as computers have
become more powerful computationally.
2D animation
2D animation figures are created and/or edited on the computer using
2D bitmap graphics or created and edited using 2D vector graphics. This
includes automated computerized versions of
traditional animation techniques such as
of, interpolated morphing, onion,skinning and i
nterpolatedrotoscoping. 2D animation has
many applications, including analog computer
animation, Flash
animation and PowerPoint
animation. Cinema graphs are still photographs in the form of an animated
GIF file of which part is animated.

3D animation
3D animation is digitally modeled and manipulated by an animator. In order
to manipulate a mesh, it is given a digital skeletal structure that can be used
to control the mesh. This process is called rigging. Various other techniques
can be applied, such as mathematical functions (ex. gravity, particle
simulations), simulated fur or hair, effects such as fire and water and the use
SATWINDER SINGH

|| 100210390828

COMPUTER GRAPHICS 4
SOFTWARE LAB VII (CS-319)

of motion capture to name but a few,


these techniques fall under the
category of 3D dynamics. Wellmade 3D animations can be difficult to
distinguish from live action and are
commonly used as visual effects for
recent movies. Toy Story (1995, USA)
is the first feature-length film to be
created and rendered entirely using
3D graphics.
Subfields

Performance capture

Character animation

Physical simulation (e.g. cloth modeling, animation of fluid dynamics,


etc.)

Rendering
Indirect diffuse scattering simulated using path
tracing and irradiance caching.
Rendering generates images from a model.
Rendering may simulate light transport to
create realistic images or it may create images
that have a particular artistic style in nonphotorealistic rendering. The two basic
operations in realistic rendering are transport
(how much light passes from one place to
another) and scattering (how surfaces interact
with
light).
See Rendering
(computer
graphics) for more information.
SATWINDER SINGH

|| 100210390828

COMPUTER GRAPHICS 5
SOFTWARE LAB VII (CS-319)

Transport
Transport describes how illumination in a scene gets from one place to
another. Visibility is a major component of light transport.
Scattering
Models of scattering and shading are used to describe the appearance of a
surface. In graphics these problems are often studied within the context of
rendering since they can substantially affect the design of rendering
algorithms. Shading can be broken down into two orthogonal issues, which
are often studied independently:
1. scattering - how light interacts with the surface at a given point
2. shading - how material properties vary across the surface
The former problem refers to scattering, i.e., the relationship between
incoming and outgoing illumination at a given point. Descriptions of
scattering are usually given in terms of a bidirectional scattering distribution
function or BSDF. The latter issue addresses how different types of scattering
are distributed across the surface (i.e., which scattering function applies
where). Descriptions of this kind are typically expressed with a program
called a shader. (Note that there is some confusion since the word "shader"
is sometimes used for programs that describe local geometric variation.)
Other subfields

Physically based rendering - concerned with generating images


according to the laws of geometric optics

Real

time

rendering -

focuses

on

rendering

for

interactive

applications, typically using specialized hardware like GPUs

Non-Photorealistic Rendering

Relighting - recent area concerned with quickly re-rendering scenes

Some Important terms related to Computer Graphics


Aspect Ratio
SATWINDER SINGH

|| 100210390828

COMPUTER GRAPHICS 6
SOFTWARE LAB VII (CS-319)

The aspect ratio of an image is the ratio of the width of the image to its
height, expressed as two numbers separated by a colon. That is, for
an x:y aspect ratio, no matter how big or small the image is, if the width is
divided into x units of equal length and the height is measured using this
same length unit, the height will be measured to be y units. For example,
consider a group of images, all with an aspect ratio of 16:9. One image is
16 inches wide and 9 inches high. Another image is 16 centimeters wide and
9 centimeters high. A third is 8 yards wide and 4.5 yards high.
Aspect ratios are mathematically expressed as x:y (pronounced "x-to-y")
and xy (pronounced "x-by-y"), with the latter particularly used for pixel
dimensions, such as 640480. Cinematographic aspect ratios are usually
denoted as a (rounded) decimal multiple of width vs. unit height, while
photographic and video graphic aspect ratios are usually defined and
denoted by whole number ratios of width to height. In digital images there is
a subtle distinction between the Display Aspect Ratio (the image as
displayed) and the Storage Aspect Ratio (the ratio of pixel dimensions);
see distinctions, below.
The most common aspect ratios used today in the presentation of films in
movie theaters are 1.85:1 and 2.39:1. Two common video graphic aspect
ratios are 4:3(1.33:1), the universal video format of the 20th century
and; 16:9 (1.77:1),
universal
for high-definition
television and
European digital television. Other cinema and video aspect ratios exist, but
are used infrequently. As of 2010, nominally 21:9 (2.33) aspect TVs have
been introduced by Philips and Visio (the latter using an LCD from AU
Optronics) as "cinema" displays, though the resolution is more precisely
2560 / 1080 = 2.37 (2.370370) exactly), and the aspect ratio is not
standardized in HDTV.
In still camera photography, the most common aspect ratios are 4:3, 3:2,
and more recently being found in consumer cameras 16:9.Other aspect
ratios, such as5:4, 6:7, and 1:1 (square format), are used in photography as
well, particularly in medium format and large format.

SATWINDER SINGH

|| 100210390828

COMPUTER GRAPHICS 7
SOFTWARE LAB VII (CS-319)

With television, DVD and Blu-ray, converting formats of unequal ratios is


achieved by either: enlarging the original image (by the same factor in both
directions) to fill the receiving format's display area and cutting off any
excess picture information (zooming and cropping), by adding horizontal
mattes (letterboxing) or vertical mattes (pillarboxing) to retain the original
format's aspect ratio, or (for TV and DVD) by stretching (hence distorting) the
image to fill the receiving format's ratio, by scaling by different factors in
both directions, possibly scaling by a different factor in the center and at the
edges (as in Wide Zoom mode).

Pixel
The pixel (a word invented from "picture
element")
is
the
basic
unit
of
programmable color on a computer display
or in a computer image. Think of it as a
logical - rather than a physical - unit. The
physical size of a pixel depends on how
you've set the resolution for the display
screen. If you've set the display to its
maximum resolution, the physical size of a
pixel will equal the physical size of the dot
pitch (let's just call it the dot size) of the
display. If, however, you've set the
resolution to something less than the
maximum resolution, a pixel will be larger
than the physical size of the screen's dot
(that is, a pixel will use more than one
dot).
The specific color that a pixel describes is some blend of three components
of the color spectrum - RGB.
Up to three bytes of data are allocated for specifying a pixel's color, one byte
for each major color component. Atrue color or 24-bit color system uses all
three bytes. However, many color display systems use only one byte
(limiting the display to 256 different colors).
A bitmap is a file that indicates a color for each pixel along the horizontal
axis or row (called the x coordinate) and a color for each pixel along the
SATWINDER SINGH

|| 100210390828

COMPUTER GRAPHICS 8
SOFTWARE LAB VII (CS-319)

vertical axis (called the y coordinate). A Graphics Interchange Format file, for
example, contains a bitmap of an image (along with other data).
Screen image sharpness is sometimes expressed as dpi (dots per inch). (In
this usage, the term dot means pixel, not dot as in dot pitch.) Dots per inch is
determined by both the physical screen size and the resolution setting. A
given image will have lower resolution - fewer dots per inch - on a larger
screen as the same data is spread out over a larger physical area.

Practical No. 2
Write a program to generate a line using line function.

#include <graphics.h>
#include <iostream.h>
#include <conio.h>
void main()
{
int gdriver = DETECT, gmode, errorcode;
int x1,y1,x2,y2;
initgraph(&gdriver, &gmode, "c:\\tc\\bgi\\");
cout<<"ENTER CO-ORDINATES OF LINE \n";
cout<<"X1:";
cin>>x1;
cout<<"Y1:";
cin>>y1;
cout<<"X2:";
cin>>x2;
cout<<"Y2:";
cin>>y2;
SATWINDER SINGH

|| 100210390828

COMPUTER GRAPHICS 9
SOFTWARE LAB VII (CS-319)

line(x1,y1,x2,y2);
getch();
}

Output:

SATWINDER SINGH

|| 100210390828

COMPUTER GRAPHICS 10
SOFTWARE LAB VII (CS-319)

Practical No. 3
Write a program to generate a line using DDA Approach.

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<ctype.h>
#include<math.h>
#include<stdlib.h>
void draw(int x1,int y1,int x2,int y2);
void main()
{
int x1,y1,x2,y2;
int gdriver=DETECT,gmode,gerror;
cout<<"Enter Value of X1: ";
cin>>x1;
cout<<"Enter Value of Y1: ";
cin>>y1;
cout<<"Enter Value of X2: ";
cin>>x2;
SATWINDER SINGH

|| 100210390828

COMPUTER GRAPHICS 11
SOFTWARE LAB VII (CS-319)

cout<<"Enter Value of Y2: ";


cin>>y2;
getch();
}

void draw(int x1,int y1,int x2,int y2)


{
float x,y,xinc,yinc,dx,dy;
int k;
int step;
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
step=abs(dx);
else
step=abs(dy);
xinc=dx/step;
yinc=dy/step;
x=x1;
y=y1;
putpixel(x,y,10);
for(k=1;k<=step;k++)
{
x=x+xinc;
y=y+yinc;
SATWINDER SINGH

|| 100210390828

COMPUTER GRAPHICS 12
SOFTWARE LAB VII (CS-319)

putpixel(x,y,10);
}
}

Output:

SATWINDER SINGH

|| 100210390828

COMPUTER GRAPHICS 13
SOFTWARE LAB VII (CS-319)

Practical No. 4
Write a program to generate a line using Bresenhams
Approach.

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd = DETECT, gm;
int dx, dy, p, end;
float x1, x2, y1, y2, x, y;
initgraph(&gd, &gm, "c:\\tc\\bgi\\");
cout<<"Enter Value of X1: ";
cin>>x1;
cout<<"Enter Value of Y1: ";
cin>>y1;
cout<<"Enter Value of X2: ";
cin>>x2;
cout<<"Enter Value of Y2: ";
SATWINDER SINGH

|| 100210390828

COMPUTER GRAPHICS 14
SOFTWARE LAB VII (CS-319)

cin>>y2;
dx = abs(x1 - x2);
dy = abs(y1 - y2);
p = 2 * dy - dx;
if(x1 > x2)
{
x = x2;
y = y2;
end = x1;
}
else
{
x = x1;
y = y1;
end = x2;
}
putpixel(x, y, 15);
while(x < end)
{
x = x + 1;
if(p < 0)
{
p = p + 2 * dy;
}
else
{
y = y + 1;
SATWINDER SINGH

|| 100210390828

COMPUTER GRAPHICS 15
SOFTWARE LAB VII (CS-319)

p = p + 2 * (dy - dx);
}
putpixel(x, y, 15);
}
getch();
closegraph();
}

Output:

SATWINDER SINGH

|| 100210390828

COMPUTER GRAPHICS 16
SOFTWARE LAB VII (CS-319)

Practical No. 5
Write a program to generate a rectangle using rectangle
function.

#include <graphics.h>
#include <iostream.h>
#include <conio.h>
void main()
{
int gdriver = DETECT, gmode, errorcode;
int left, top, right, bottom;
initgraph(&gdriver, &gmode, "c:\\tc\\bgi\\");
cout<<"ENTER CO-ORDINATES OF RECTANGLE \n";
cout<<"LEFT:";
cin>>left;
cout<<"TOP:";
cin>>top;
cout<<"RIGHT:";
cin>>right;
cout<<"BOTTOM:";
SATWINDER SINGH

|| 100210390828

COMPUTER GRAPHICS 17
SOFTWARE LAB VII (CS-319)

cin>>bottom;
rectangle(left,top,right,bottom);
getch();
}

Output:

SATWINDER SINGH

|| 100210390828

COMPUTER GRAPHICS 18
SOFTWARE LAB VII (CS-319)

Practical No. 6
Write a program to generate a circle using circle function .

#include <graphics.h>
#include <iostream.h>
#include <conio.h>
void main()
{
int gdriver = DETECT, gmode, errorcode;
int x,y,radius;
initgraph(&gdriver, &gmode, "e:\\tc\\bgi\\");
cout<<"ENTER CO-ORDINATES OF CIRCLE \n";
cout<<"X-AXIS:";
cin>>x;
cout<<"Y-AXIS:";
cin>>y;
cout<<"RADIUS:";
cin>>radius;
circle(x,y,radius);
getch();

SATWINDER SINGH

|| 100210390828

COMPUTER GRAPHICS 19
SOFTWARE LAB VII (CS-319)

Output:

SATWINDER SINGH

|| 100210390828

COMPUTER GRAPHICS 20
SOFTWARE LAB VII (CS-319)

Practical No. 7
Write a program to generate a ellipse using ellipse
function.

#include <graphics.h>
#include <iostream.h>
#include <conio.h>
void main()
{
int gdriver = DETECT, gmode, errorcode;
int midx,midy,sangle,eangle,xradius,yradius;
initgraph(&gdriver, &gmode, "c:\\tc\\bgi\\");
cout<<"ENTER CO-ORDINATES OF LI \n";
cout<<"MID X:";
cin>>midx;
cout<<"MID-Y:";
cin>>midy;
cout<<"STAR ANGLE:";
cin>>sangle;
cout<<"END ANGLE:";
SATWINDER SINGH

|| 100210390828

COMPUTER GRAPHICS 21
SOFTWARE LAB VII (CS-319)

cin>>eangle;
cout<<"X-RADIUS:";
cin>>xradius;
cout<<"Y-RADIUS:";
cin>>yradius;
ellipse(midx,midy,sangle,eangle,xradius,yradius);
getch();
}
Output:

SATWINDER SINGH

|| 100210390828

COMPUTER GRAPHICS 22
SOFTWARE LAB VII (CS-319)

Practical No. 8
Write a program to show the concept of 2D scaling.

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
clrscr();
int gd=0,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi\\");
cout<<"object before scaling is :-";
int a=100,b=100,c=200,d=200;
rectangle(a,b,c,d);
getch();
cleardevice();
int x2,y2;
int ch;
cout<<"\nEnter 1 for x-axis ,2 for y-axis and 3 for both :-\n" ;
SATWINDER SINGH

|| 100210390828

COMPUTER GRAPHICS 23
SOFTWARE LAB VII (CS-319)

cin>>ch;
switch(ch)
{
case 1:
cout<<"\nEnter value:-\n";
cin>>x2;
if(x2>a)
{
cout<<"\nobject after

scaling is :-";

rectangle(a,b,c+x2,d);
}
else if(x2==a)
{
cout<<"\nno change";
rectangle(a,b,c,d);
}
else
{
cout<<"\nobject after

scaling is :-";

rectangle(a,b,c-x2,d);
}
break;
case 2:
cout<<"\nEnter value:-\n";
cin>>y2;
SATWINDER SINGH

|| 100210390828

COMPUTER GRAPHICS 24
SOFTWARE LAB VII (CS-319)

if(y2>b)
{
cout<<"\nobject after

scaling is :-";

rectangle(a,b,c,d+y2);
}
else if(y2==b)
{
cout<<"\nno change";
rectangle(a,b,c,d);
}
else
{
cout<<"\nobject after

scaling is :-";

rectangle(a,b,c,d-y2);
}
break;
case 3:
cout<<"Enter values:-";
cin>>x2>>y2;
rectangle(a,b,c+x2,d+y2);
break;
default:
cout<<"\nEnter right choice.";
}
getch();
SATWINDER SINGH

|| 100210390828

COMPUTER GRAPHICS 25
SOFTWARE LAB VII (CS-319)

Output:

SATWINDER SINGH

|| 100210390828

COMPUTER GRAPHICS 26
SOFTWARE LAB VII (CS-319)

Practical No. 9
Write a program to show the concept of boundary fill
using 4 connected approach .
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
void fill (int x,int y,int color,int color1)
{
int curr;
curr=getpixel(x,y);
if((curr!=color)&&(curr!=color1))

SATWINDER SINGH

|| 100210390828

COMPUTER GRAPHICS 27
SOFTWARE LAB VII (CS-319)

{
delay(20);
putpixel(x,y,color1);
fill(x+1,y,color,color1);
fill(x,y+1,color,color1);
fill(x-1,y,color,color1);
fill(x,y-1,color,color1);
}
}
void main()
{
int gd=0,gm,x1,y1,x2,y2,color=2,color1=4;
initgraph(&gd,&gm,"c:\\tc\\bgi");
cout<<"Enter the end points of rectangle"<<endl;
cout<<"Enter coordinates";
cin>>x1>>y1;
cout<<"Enter coordinates";
cin>>x2>>y2;
setcolor(color);
rectangle(x1+150,y1+100,x2+150,y2+100);
fill(x1+151,y1+101,color,color1);
getch();
}

Output:
SATWINDER SINGH

|| 100210390828

COMPUTER GRAPHICS 28
SOFTWARE LAB VII (CS-319)

Practical No. 10
Write a program to show the concept of flood fill using 4
connected approach.

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

void main()
{
int gdriver = DETECT, gmode, errorcode;
int maxx, maxy;
SATWINDER SINGH

|| 100210390828

COMPUTER GRAPHICS 29
SOFTWARE LAB VII (CS-319)

initgraph(&gdriver, &gmode, "e:\\tc\\bgi\\");


setfillstyle(SOLID_FILL,4);
rectangle(100,100,400,400);
getch();
cout<<"After Flood Fill:";
floodfill(200,200,getmaxcolor());
getch();
closegraph();
}

Output:

SATWINDER SINGH

|| 100210390828

COMPUTER GRAPHICS 30
SOFTWARE LAB VII (CS-319)

SATWINDER SINGH

|| 100210390828

You might also like