Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 52

CSX-328 Computer Graphics and Animation Lab

COMPUTER GRAPHICS AND ANIMATION


LAB
(CSX-328)
LAB MANUAL
COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


DR. B R AMBEDKAR NATIONAL INSTITUTE OF TECHNOLOGY
JALANDHAR – 144011, PUNJAB (INDIA)

1
CSX-328 Computer Graphics and Animation Lab

TABLE OF CONTENTS
S.No. Program Page no
1 To draw a line using DDA algorithm 10-11
2 To draw a line using Bresenham’s algorithm 12-13
3 To draw a circle using Mid point circle algorithm 14-16
4 To draw an ellipse using Midpoint Algorithm 17-19
5 Transalte an object 2D 20-21
6 Scale an object 2D 22-23
7 Rotate an object with given angle 24-25
8 Perform Reflection and Shearing 26-29
9 3D Translation 30-31
10 3D Scaling 32-33
11 3D Rotation 34-35
12 Implement Cohen Sutherland Algorithm 36-40
13 Implement Liang Barsky Line clipping algorithm 41-43
14 Implement Polygon Clipping algorithm 44-46
15 Graphic Representation of man walking in rain 47-49
16 Program to show concentric circles 50-51

2
CSX-328 Computer Graphics and Animation Lab

Hardware and Software Requirements

1. Software Requirement:

Turbo C / C++ compiler that supports graphics.h package with DOSBOX installer for Turbo
C++ compiler
CODE BLOCKS and CODE BLOCKS-EP

2. Minimum hardware requirements:

Intel Pentium III800 MHz Processor or higher version

Intel chipset 810 mother board or higher version

512 MB RAM

14’’ color monitor or greater than that

Mouse

Keyboard

2GB HDD or greater

Steps to configure CodeBlocks for executing graphics programs.

Steps:
Compiling graphics codes on CodeBlocks IDE shows an error: “Cannot find graphics.h”.
This is because graphics.h runs is not available in the library folder of CodeBlocks. To
successfully compile graphics code on CodeBlocks, setup winBGIm library.

Follow below steps in sequence to include “graphics.h” in CodeBlocks to


successfully compile graphics code on Codeblocks.

Step 1 : To setup “graphics.h” in CodeBlocks, first set up winBGIm graphics library.


Download WinBGIm from http://winbgim.codecutter.org/ or use this link.

Step 2 : Extract the downloaded file. There will be three files:

3
CSX-328 Computer Graphics and Animation Lab

• graphics.h
• winbgim.h
• libbgi.a


Step 3 : Copy and paste graphics.h and winbgim.h files into the include folder of
compiler directory. (If you have Code::Blocks installed in C drive of your computer, go
through: Disk C >> Program Files >> CodeBlocks >> MinGW >> include. Paste these two
files there.)

4
CSX-328 Computer Graphics and Animation Lab

Step 4 : Copy and paste libbgi.a to the lib folder of compiler directory.

Step 5 : Open Code::Blocks. Go to Settings >> Compiler >> Linker settings.

5
CSX-328 Computer Graphics and Animation Lab

Step 6 : In that window, click the Add button under the “Link libraries” part, and
browse.

select the libbgi.a file copied to the lib folder in step 4.

6
CSX-328 Computer Graphics and Animation Lab

Step 7 : In right part (ie. other linker options) paste commands -


lbgi -lgdi32 -lcomdlg32 -luuid -loleaut32 -lole32

7
CSX-328 Computer Graphics and Animation Lab

Step 8 : Click Ok

Step 9 : Try compiling a graphics.h program in C or C++, still there will be an error. To
solve it, open graphics.h file (pasted in include folder in step 3) with Notepad++. Go to line
number 302, and replace that line with this line : int left=0, int top=0, int
right=INT_MAX, int bottom=INT_MAX,

8
CSX-328 Computer Graphics and Animation Lab

Step 10 : Save the file. Done !


Note : Now, you can compile any C or C++ program containing graphics.h header file. If
you compile C codes, you’ll still get an error saying: “fatal error: sstream : no such file
directory”.

9
CSX-328 Computer Graphics and Animation Lab

Practical 1
Aim: To draw a line using DDA algorithm.
Description: The digital differential analyzer (DDA) is a scan-conversion line algorithm
based on calculating either dy or dx. We sample the line at unit intervals in one coordinate
and determine corresponding integer values nearest the line path for the other coordinate.
Algorithm:
1. Calculate dx, dy
dx = X1 - X0;
dy = Y1 - Y0;
2. Depending upon absolute value of dx & dy,choose number of steps to put pixel as
steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy);
3. Calculate increment in x & y for each steps
Xinc = dx / (float) steps;
Yinc = dy / (float) steps;
4. Put pixel for each step
Code:
#include<bits/stdc++.h>
#include<graphics.h>
using namespace std;
int main()
{
int gd=0,gm;
gd=DETECT;
initgraph(&gd,&gm,"C:\Program Files (x86)\CodeBlocks\MinGW\lib");
float x1,y1,x2,y2;
x1=getmaxx()/2;
y1=getmaxy()/2;
x2=x1+50;
y2=y1+75;
float m=(float)(y2-y1)/(x2-x1);
if(m>=0)
{
if(m<=1)
{
putpixel(x1,y1,4);
while(x1!=x2||y1!=y2)
{
y1+=m;
x1+=1;
putpixel(x1,y1,4);
}
}
else
{
putpixel(x1,y1,4);
while(x1!=x2||y1!=y2)
{
10
CSX-328 Computer Graphics and Animation Lab

y1+=1;
x1+=(1/m);
putpixel(x1,y1,4);
}}}
else
{
if(abs(m)<=1)
{
putpixel(x1,y1,4);
while(x1!=x2||y1!=y2)
{
y1-=m;
x1-=1;
putpixel(x1,y1,4);
}
}
else
{
putpixel(x1,y1,4);
while(x1!=x2||y1!=y2)
{
y1-=1;
x1-=(1/m);
putpixel(x1,y1,4); }
}}
getch();
closegraph();
return 0;
}
Output:

11
CSX-328 Computer Graphics and Animation Lab

Pratical-2
Aim:To draw a line using Bresenham’s Algorithm.

Description:
The Bresenham algorithm is another incremental scan conversion algorithm. The big
advantage of this algorithm is that, it uses only integer calculations. Moving across the x axis
in unit intervals and at each step choose between two different y coordinates.
Algorithm:
1. Input the two end-points of line, storing the left end-point in (x0,y0)(x0,y0).
2. Plot the point (x0,y0)(x0,y0).
3. Calculate the constants dx, dy, 2dy, and (2dy – 2dx) and get the first value for the
decision parameter as −
p0=2dy−dx
4. At each Xk along the line, starting at k = 0, perform the following test −

If pk < 0, the next point to plot is (xk+1,yk) and


pk+1=pk+2dy
Otherwise,
(xk,yk+1)
pk+1=pk+2dy−2dx

5. Repeat step 4 (dx – 1) times.

For m > 1, find out whether you need to increment x while incrementing y each time.
After solving, the equation for decision parameter PkPk will be very similar, just the x and y
in the equation gets interchanged.

Code:
#include<bits/stdc++.h>
#include<graphics.h>
using namespace std;
int main()
{
int gd=0,gm;
gd=DETECT;
initgraph(&gd,&gm,"C:\Program Files (x86)\CodeBlocks\MinGW\lib");
float x1,y1,x2,y2;
x1=getmaxx()/2;
y1=getmaxy()/2;
x2=x1+50;
y2=y1+60;
float m=(float)(y2-y1)/(x2-x1);
float p=2*(y2-y1)-(x2-x1);
putpixel(x1,y1,4);
while(x1!=x2||y1!=y2)
{
12
CSX-328 Computer Graphics and Animation Lab

if(p<0)
{
y1=y1;
x1+=1;
p+=2*(y2-y1);
putpixel(x1,y1,4);
}
else
{
y1+=1;
x1+=1;
p+=2*(y2-y1)-2*(x2-x1);
putpixel(x1,y1,4);
}
}
getch();
closegraph();
return 0;
}

Output:

13
CSX-328 Computer Graphics and Animation Lab

Practical 3
Aim:To draw a circle using Midpoint Algorithm.

Description:
As in the raster line algorithm, we sample at unit intervals and determine the closest pixel
position to the specified circle path at each step. For a given radius r and screen center
position (x , y,), we can first set up our algorithm to calculate pixel positions around a circle
path centered at the coordinate origin (0,O)Then each calculated position (x, y) is moved to
its proper screen position by adding x, to x and y, toy. Along the circle section from x = 0 to x
= y in the first quadrant, the slope of the curve varies from 0 to -1. Therefore, we can take
unit steps in the positive x direction over this octant and use a decision parameter to
determine which of the two possible y positions is closer to the circle path at each step.
Positions in the other seven octants are then obtained by symmetry.

Algorithm:
1. Input radius r and circle center (xc,yc)(xc,yc) and obtain the first point on the
circumference of the circle centered on the origin as
(x0, y0) = (0, r)
2. Calculate the initial value of decision parameter as
P0 = 5/4 – r
3. At each Xk position starting at K=0, perform the following test −
If PK < 0 then next point on circle (0,0) is (XK+1,YK) and
PK+1 = PK + 2XK+1 + 1
Else
PK+1 = PK + 2XK+1 + 1 – 2YK+1

Where, 2XK+1 = 2XK+2 and 2YK+1 = 2YK-2.


4. Determine the symmetry points in other seven octants.
5. Move each calculate pixel position (X, Y) onto the circular path centered
on (XC,YC) and plot the coordinate values.
X = X + XC, Y = Y + YC
6. Repeat step-3 through 5 until X >= Y.

Code:

#include<bits/stdc++.h>
#include<graphics.h>
using namespace std;
int main()
{
int gd=0,gm;
gd=DETECT;
initgraph(&gd,&gm,"C:\Program Files (x86)\CodeBlocks\MinGW\lib");
float x1,y1,r,x,y,xc,yc,d;
r=100;
xc=getmaxx()/2;
yc=getmaxy()/2;
x1=0;
y1=r;
14
CSX-328 Computer Graphics and Animation Lab

putpixel(xc+x1,yc+y1,4);
putpixel(xc+y1,yc+x1,4);
putpixel(xc-y1,yc+x1,4);
putpixel(xc-x1,yc+y1,4);
putpixel(xc-x1,yc-y1,4);
putpixel(xc-y1,yc-x1,4);
putpixel(xc+y1,yc-x1,4);
putpixel(xc+x1,yc-y1,4);
d=1-r;
while(x1<=y1)
{
if(d<0)
{
x1+=1;
y1=y1;
putpixel(xc+x1,yc+y1,4);
putpixel(xc+y1,yc+x1,4);
putpixel(xc-y1,yc+x1,4);
putpixel(xc-x1,yc+y1,4);
putpixel(xc-x1,yc-y1,4);
putpixel(xc-y1,yc-x1,4);
putpixel(xc+y1,yc-x1,4);
putpixel(xc+x1,yc-y1,4);
d+=2*(x1+1)+1;
}
else
{
x1+=1;
y1-=1;
putpixel(xc+x1,yc+y1,4);
putpixel(xc+y1,yc+x1,4);
putpixel(xc-y1,yc+x1,4);
putpixel(xc-x1,yc+y1,4);
putpixel(xc-x1,yc-y1,4);
putpixel(xc-y1,yc-x1,4);
putpixel(xc+y1,yc-x1,4);
putpixel(xc+x1,yc-y1,4);
d+=2*(x1+1)+1-2*y1;
}
}
getch();
closegraph();
return 0;
}

15
CSX-328 Computer Graphics and Animation Lab

Output:

16
CSX-328 Computer Graphics and Animation Lab

Practical 4
Aim:To draw an ellipse using Midpoint Algorithm.

Description:
Our approach hrrr is similar to that used in displaying d raster circle. Given parameters
rx,ryd(xc , y,), we determine points ( x , y) for an ellipse in standard position centered on the
origin, and then we shift the points so the ellipse is centered at ( x , y,). 1t we wish also tu
display the ellipse in nonstandard position, we could then rotate the ellipse about its center
coordinates to reorient the major and minor axes. For the present, we consider only the
display of ellipses in standard position
Algorithm:
1.Input rx, ry, and ellipse center (xc, yc,), and obtain the first point on an
ellipse centered on the origin as
(x0,y0)=(0,ry);
2. Calculate the initial value of the decision parameter in region 1 as
p10=ry2-rx2ry+1/4(rx2)
3. At each x, position in region 1, starting at k = 3, perform the following
test: If plk < 0, the next point along the ellipse centered on (0, 0)
is (xk+1, yk) and
p1k+1=p1k+2ry2xk+1+ry2
Otherwise, the next point along the circle is (xk + 1, yk-1)
p1k+1=p1k+2ry2xk+1+ry2-2rx2yk+1

and continue until 2ry2>=2rx2y


4. Calculate the initial value of the decision parameter in region 2 using
the last point (xo, yo) calculated in region 1 as
p20=ry2(x0+1/2)2+rx2(y0-1)2-rx2ry2
5. At each yk position in region 2, starting at k = 0, perform the following
test: If p2k> 0, the next point along the ellipse centered on (0, 0) is
(xk , yk .- 1) and
p2k+1=p2k+2ry2yk+1+rx2
Otherwise, the next point along the circle is (.rk + 1, yt - 1) and
p2k+1=p2k+2rx2yk+1+rx2-2ry2xk+1
using the same incremental calculations for .I and y as in region 1.
6. Determine symmetry points in the other three quadrants.
7. Move each calculated pixel position (x, y) onto the elliptical path centered on (xc,yc) and
plot the coordinate values:
x=x+xc ,y=y+yc
8. Repeat the steps for region 1 until 2ry2>=2rx2y

Code:
#include<bits/stdc++.h>
#include<graphics.h>
using namespace std;
int main()
{
int gd=0,gm;
gd=DETECT;
initgraph(&gd,&gm,"C:\Program Files (x86)\CodeBlocks\MinGW\lib");
float x1,y1,rx,ry,p1,p2,m,xc,yc,p10,p20;
17
CSX-328 Computer Graphics and Animation Lab

xc=getmaxx()/2;
yc=getmaxy()/2;
rx=100;
ry=50;
x1=0;
y1=ry;
putpixel(xc+0,yc+ry,4);
putpixel(xc+rx,yc+0,4);
putpixel(xc-rx,yc+0,4);
putpixel(xc+0,yc-ry,4);

p1=ry*ry+rx*rx/4-rx*ry*ry;
while((2*x1*ry*ry)<=(2*y1*rx*rx))
{
if(p1<0)
{
x1++;
y1=y1;
putpixel(xc+x1,yc+y1,4);
putpixel(xc+x1,yc-y1,4);
putpixel(xc-x1,yc+y1,4);
putpixel(xc-x1,yc-y1,4);
p1=p1+ry*ry+2*(x1)*ry*ry;
}
else
{
x1++;
y1--;
putpixel(xc+x1,yc+y1,4);
putpixel(xc+x1,yc-y1,4);
putpixel(xc-x1,yc+y1,4);
putpixel(xc-x1,yc-y1,4);
p1=p1+ry*ry+2*(x1)*ry*ry-2*rx*rx*y1;
}
}
p2=rx*rx+rx*ry*ry+ry*ry/4;
while(((2*x1*ry*ry)>(2*y1*rx*rx))&&(y1!=0))
{
if(p2<0)
{
x1++;
y1--;
putpixel(xc+x1,yc+y1,4);
putpixel(xc+x1,yc-y1,4);
putpixel(xc-x1,yc+y1,4);
putpixel(xc-x1,yc-y1,4);
p2=p2+rx*rx-2*rx*rx*y1+2*ry*ry*x1;}
else{
x1;
y1--;
putpixel(xc+x1,yc+y1,4);
18
CSX-328 Computer Graphics and Animation Lab

putpixel(xc+x1,yc-y1,4);
putpixel(xc-x1,yc+y1,4);
putpixel(xc-x1,yc-y1,4);
p2=p2+rx*rx-2*rx*rx*y1;
}}
getch();
closegraph();
return 0;
}
Output:

19
CSX-328 Computer Graphics and Animation Lab

Practical 5
Aim:To translate an object with translation parameters in X and Y directions.

Description:
A translation is applied to an object by repositioning it along a straight-line path from one
coordinate location to another. We translate a two-dimensional point by addlng translation
distances, f, and t,, to the original coordinate position (x, y) to move the point to a new
position ( x ' , y').
x' = x + t,, y' = y + t,
Code:
/*#include<bits/stdc++.h>
#include<graphics.h>
using namespace std;
int main()
{
int gd=0,gm;
gd=DETECT;
initgraph(&gd,&gm,"C:\Program Files (x86)\CodeBlocks\MinGW\lib");
float xc,yc,tx,ty;
tx=10;
ty=10;
xc=getmaxx()/2;
yc=getmaxy()/2;
int pt[3][3]={{250,100,1},{100,150,1},{70,225,1}};
int tran[3][3]={{1,0,0},{0,1,0},{tx,ty,1}};
int tr_pt[3][3];
memset(tr_pt,0,sizeof(tr_pt[0][0])*3*3);
int i,j,k;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
{
tr_pt[i][k]+=pt[i][j]*tran[j][k];
}
}
}
setcolor(4);
line(pt[0][0]+xc,pt[0][1]+yc,pt[1][0]+xc,pt[1][1]+yc);
line(pt[1][0]+xc,pt[1][1]+yc,pt[2][0]+xc,pt[2][1]+yc);
line(pt[2][0]+xc,pt[2][1]+yc,pt[0][0]+xc,pt[0][1]+yc);

setcolor(5);
line(tr_pt[0][0]+xc,tr_pt[0][1]+yc,tr_pt[1][0]+xc,tr_pt[1][1]+yc);
line(tr_pt[1][0]+xc,tr_pt[1][1]+yc,tr_pt[2][0]+xc,tr_pt[2][1]+yc);
line(tr_pt[2][0]+xc,tr_pt[2][1]+yc,tr_pt[0][0]+xc,tr_pt[0][1]+yc);

getch();
closegraph();
20
CSX-328 Computer Graphics and Animation Lab

return 0;
}
Output:

21
CSX-328 Computer Graphics and Animation Lab

Practical 6
Aim:To scale an object with scaling factors along X and Y directions.

Description:
A scaling transformation alters the size of an object. This operation can be carried out for
polygons by multiplying the coordinate values (x, y) of each vertex by scaling factors s, and
s, to produce the transformed coordinates (x', y'):
x’=x.sx y’=y.sy
Code:
#include<bits/stdc++.h>
#include<graphics.h>
using namespace std;
int main()
{
int gd=0,gm;
gd=DETECT;
initgraph(&gd,&gm,"C:\Program Files (x86)\CodeBlocks\MinGW\lib");
float xc,yc,sx,sy;
sx=2;
sy=3;
xc=getmaxx()/2;
yc=getmaxy()/2;
int pt[3][3]={{150,10,1},{100,50,1},{150,40,1}};
int sc[3][3]={{sx,0,0},{0,sy,0},{0,0,1}};
int sc_pt[3][3];
memset(sc_pt,0,sizeof(sc_pt[0][0])*3*3);
int i,j,k;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
{
sc_pt[i][k]+=pt[i][j]*sc[j][k];
}
}
}
setcolor(4);
line(pt[0][0]+xc,pt[0][1]+yc,pt[1][0]+xc,pt[1][1]+yc);
line(pt[1][0]+xc,pt[1][1]+yc,pt[2][0]+xc,pt[2][1]+yc);
line(pt[2][0]+xc,pt[2][1]+yc,pt[0][0]+xc,pt[0][1]+yc);

setcolor(5);
line(sc_pt[0][0]+xc,sc_pt[0][1]+yc,sc_pt[1][0]+xc,sc_pt[1][1]+yc);
line(sc_pt[1][0]+xc,sc_pt[1][1]+yc,sc_pt[2][0]+xc,sc_pt[2][1]+yc);
line(sc_pt[2][0]+xc,sc_pt[2][1]+yc,sc_pt[0][0]+xc,sc_pt[0][1]+yc);

getch();
closegraph();
return 0;
22
CSX-328 Computer Graphics and Animation Lab

Output:

23
CSX-328 Computer Graphics and Animation Lab

Practical 7
Aim:To rotate an object with a certain angle

Description:
A two-dimensional rotation is applied to an object by repositioning it along a circular path in
the xy plane. To generate a rotation, we specify a rotation angle and the position (x, y,) of the
rotation point (or pivot point) about which the object is to be rotated.
The new coordinates after rotation are (x’,y’).
x’=xcosθ-ysinθ;
y’=xsinθ+ycosθ

Code:
#include<bits/stdc++.h>
#include<graphics.h>
using namespace std;
int main()
{
int gd=0,gm;
gd=DETECT;
initgraph(&gd,&gm,"C:\Program Files (x86)\CodeBlocks\MinGW\lib");
float xc,yc,theta;
theta=45;
xc=getmaxx()/2;
yc=getmaxy()/2;
int pt[3][3]={{150,20,1},{100,80,1},{150,200,1}};
float rot[3][3]={{cos(theta),-sin(theta),0},{sin(theta),cos(theta),0},{0,0,1}};
int rot_pt[3][3];
memset(rot_pt,0,sizeof(rot_pt[0][0])*3*3);
int i,j,k;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
{
rot_pt[i][k]+=pt[i][j]*rot[j][k];
}
}
}
setcolor(4);
line(pt[0][0]+xc,pt[0][1]+yc,pt[1][0]+xc,pt[1][1]+yc);
line(pt[1][0]+xc,pt[1][1]+yc,pt[2][0]+xc,pt[2][1]+yc);
line(pt[2][0]+xc,pt[2][1]+yc,pt[0][0]+xc,pt[0][1]+yc);

setcolor(5);
line(rot_pt[0][0]+xc,rot_pt[0][1]+yc,rot_pt[1][0]+xc,rot_pt[1][1]+yc);
line(rot_pt[1][0]+xc,rot_pt[1][1]+yc,rot_pt[2][0]+xc,rot_pt[2][1]+yc);
line(rot_pt[2][0]+xc,rot_pt[2][1]+yc,rot_pt[0][0]+xc,rot_pt[0][1]+yc);

getch();
24
CSX-328 Computer Graphics and Animation Lab

closegraph();
return 0;
}

Output:

25
CSX-328 Computer Graphics and Animation Lab

Practical 8

Aim:To perform composite transformations of an object.


a)Reflection
b)Shearing

Description:
a) Reflection:
A reflection is a transformation that produces a mirror image of an object. The
mirror image for a two-dimensional reflection is generated relative to an axis
of reflection by rotating the object 180" about the reflection axis. We can
choose an axis of reflection in the xy plane or perpendicular to the xy plane.
When the reflection axis is a line in the xy plane, the rotation path about this
axis is in a plane perpendicular to the xy plane. For reflection axes that are
perpendicular to the xy plane, the rotation path is in the xy plane. Following
are examples of some common reflections.
b) Shearing
A transformation that distorts the shape of an object such that the transformed
shape appears as if the object were composed of internal layers that had been
caused to slide over each other is called a shear. Two common shearing
transformations are those that shift coordinate w values and those that shift y
values.
Code:

a) Reflection

#include<bits/stdc++.h>
#include<graphics.h>
using namespace std;
int main()
{
int gd=0,gm;
gd=DETECT;
initgraph(&gd,&gm,"C:\Program Files (x86)\CodeBlocks\MinGW\lib");
float xc,yc;
xc=getmaxx()/2;
yc=getmaxy()/2;
int pt[3][3]={{150,20,1},{100,80,1},{150,200,1}};
int ref[3][3]={{-1,0,0},{0,1,0},{0,0,1}};
int ref_pt[3][3];
memset(ref_pt,0,sizeof(ref_pt[0][0])*3*3);
int i,j,k;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
{
ref_pt[i][k]+=pt[i][j]*ref[j][k];
26
CSX-328 Computer Graphics and Animation Lab

}
}
}
setcolor(4);
line(pt[0][0]+xc,pt[0][1]+yc,pt[1][0]+xc,pt[1][1]+yc);
line(pt[1][0]+xc,pt[1][1]+yc,pt[2][0]+xc,pt[2][1]+yc);
line(pt[2][0]+xc,pt[2][1]+yc,pt[0][0]+xc,pt[0][1]+yc);

setcolor(6);
line(ref_pt[0][0]+xc,ref_pt[0][1]+yc,ref_pt[1][0]+xc,ref_pt[1][1]+yc);
line(ref_pt[1][0]+xc,ref_pt[1][1]+yc,ref_pt[2][0]+xc,ref_pt[2][1]+yc);
line(ref_pt[2][0]+xc,ref_pt[2][1]+yc,ref_pt[0][0]+xc,ref_pt[0][1]+yc);

getch();
closegraph();
return 0;
}
Output:

b) Shearing:

#include<graphics.h>
using namespace std;
int main()
{
int gd=0,gm;
gd=DETECT;
initgraph(&gd,&gm,"C:\Program Files (x86)\CodeBlocks\MinGW\lib");
27
CSX-328 Computer Graphics and Animation Lab

float xc,yc,shx,shy;
shx=3;
shy=2;
xc=getmaxx()/2;
yc=getmaxy()/2;
int pt[3][3]={{50,10,1},{100,20,1},{70,40,1}};
int sh[3][3]={{1,shy,0},{shx,1,0},{0,0,1}};
int sh_pt[3][3];
memset(sh_pt,0,sizeof(sh_pt[0][0])*3*3);
int i,j,k;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
{
sh_pt[i][k]+=pt[i][j]*sh[j][k];
}
}
}
setcolor(4);
line(pt[0][0]+xc,pt[0][1]+yc,pt[1][0]+xc,pt[1][1]+yc);
line(pt[1][0]+xc,pt[1][1]+yc,pt[2][0]+xc,pt[2][1]+yc);
line(pt[2][0]+xc,pt[2][1]+yc,pt[0][0]+xc,pt[0][1]+yc);

setcolor(5);
line(sh_pt[0][0]+xc,sh_pt[0][1]+yc,sh_pt[1][0]+xc,sh_pt[1][1]+yc);
line(sh_pt[1][0]+xc,sh_pt[1][1]+yc,sh_pt[2][0]+xc,sh_pt[2][1]+yc);
line(sh_pt[2][0]+xc,sh_pt[2][1]+yc,sh_pt[0][0]+xc,sh_pt[0][1]+yc);

getch();
closegraph();
return 0;
}

28
CSX-328 Computer Graphics and Animation Lab

Output:

29
CSX-328 Computer Graphics and Animation Lab

Practical 9
Aim: 3 D Translation
#include<graphics.h>
#include<iostream>
#define DEPTH 100
#define TOP_FLAG 1
using namespace std;

struct point3d
{
int x,y;
};

point3d translate(point3d p,int tx=0,int ty=0)


{
p.x += tx;
p.y += ty;
return p;
}

int main()
{
int gd = DETECT,gm;
initgraph(&gd,&gm,"");
point3d p1,p2;
cout<<"enter top left points x ,y respectivly"<<endl; cin>>p1.x>>p1.y;
cout<<"enter bottom right points x ,y respectivly"<<endl; cin>>p2.x>>p2.y;
cout<<"object before translation"<<endl; bar3d(p1.x,p1.y,p2.x,p2.y,DEPTH,TOP_FLAG);
int tx,ty;
cout<<"enter translational parameter tx, ty"<<endl; cin>>tx>>ty;
p1 = translate(p1,tx,ty);
p2 = translate(p2,tx,ty);
cout<<"object after translation"<<endl;
bar3d(p1.x,p1.y,p2.x,p2.y,DEPTH,TOP_FLAG);

getch();

Output:

30
CSX-328 Computer Graphics and Animation Lab

31
CSX-328 Computer Graphics and Animation Lab

Practical 10
Aim: 3D-Scaling.

#include<graphics.h>
#include<iostream>
#define DEPTH 100
#define TOP_FLAG 1
using namespace std;

struct point3d
{
double x,y;
};

struct object3d
{
point3d p1,p2;
};

object3d scale(object3d o,double s)


{
o.p1.x *= s;
o.p1.y *= s;
o.p2.x *= s;
o.p2.y *= s;
return o;
}
int main()
{
int gd = DETECT,gm;
initgraph(&gd,&gm,"");
object3d o;
cout<<"enter value of top ,left ,bottom ,right respectively"<<endl;
cin>>o.p1.x>>o.p1.y>>o.p2.x>>o.p2.y;

cout<<"object before rotation"<<endl;


bar3d(o.p1.x,o.p1.y,o.p2.x,o.p2.y,DEPTH,TOP_FLAG);
cout<<"enter scaling factor"<<endl;
double s;
cin>>s;
delay(2000);
cleardevice();
cout<<"object after scaling"<<endl;
o = scale(o,s);
bar3d(o.p1.x,o.p1.y,o.p2.x,o.p2.y,DEPTH,TOP_FLAG);

getch();

}
32
CSX-328 Computer Graphics and Animation Lab

OUTPUT:

33
CSX-328 Computer Graphics and Animation Lab

Practical 11

Aim: 3D Rotation

#include<graphics.h>
#include<math.h>
#include<iostream>
using namespace std;
#define DEPTH 100
#define TOP_FLAG 1

struct point3d
{
double x,y;
};

point3d rotate(point3d p,double angle=0)


{
angle = (angle * 3.14)/180.0;
p.x = p.x*cos(angle)-p.y*sin(angle);
p.y = p.x*sin(angle)+p.y*cos(angle);
return p;
}

int main()
{
int gd = DETECT,gm;
initgraph(&gd,&gm,"");

point3d p1,p2;
cout<<"enter top left points x ,y respectivly"<<endl; cin>>p1.x>>p1.y;
cout<<"enter bottom right points x ,y respectivly"<<endl; cin>>p2.x>>p2.y;
cout<<"object before translation"<<endl; bar3d(p1.x,p1.y,p2.x,p2.y,DEPTH,TOP_FLAG);
double angle;
cout<<"enter angle of rotation in degree"<<endl; cin>>angle;
cout<<"points after rotation"<<endl;
p1 = rotate(p1,angle);
cout<<p1.x<<" "<<p1.y<<endl;
p2 = rotate(p2,angle);
cout<<p2.x<<" "<<p2.y<<endl;
delay(2000);
cleardevice();
cout<<"object after rotation"<<endl;
bar3d(abs(p1.x),abs(p1.y),abs(p2.x),abs(p2.y),DEPTH,TOP_FLAG);
getch();

34
CSX-328 Computer Graphics and Animation Lab

Output:

35
CSX-328 Computer Graphics and Animation Lab

Practical 12
Aim:To Implement cohen sutherland line clipping algorithm
#include<iostream>
#include<stdlib.h>
#include<math.h>
#include<graphics.h>
#include<dos.h>

using namespace std;


typedef struct coordinate
{
int x,y;
char code[4];

}PT;

void drawwindow();
void drawline(PT p1,PT p2);
PT setcode(PT p);
int visibility(PT p1,PT p2);
PT resetendpt(PT p1,PT p2);
int main()

{
int gd=DETECT,v,gm;
initgraph(&gd,&gm,"null");
initwindow(600,500, "null");
setfillstyle(1,7);
bar(0,0,600,500);
PT p1,p2,p3,p4,ptemp;
cout<<"\nEnter x1 and y1\n";
cin>>p1.x>>p1.y;
cout<<"\nEnter x2 and y2\n";
cin>>p2.x>>p2.y;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
drawwindow();
delay(500);
drawline(p1,p2);
delay(1000);
cleardevice();
delay(500);
p1=setcode(p1);
p2=setcode(p2);
v=visibility(p1,p2);
delay(500);
switch(v)
{
case 0: drawwindow();
delay(500);
36
CSX-328 Computer Graphics and Animation Lab

drawline(p1,p2);
break;
case 1: drawwindow();
delay(500);
break;

case 2: p3=resetendpt(p1,p2);
p4=resetendpt(p2,p1);
drawwindow();
delay(500);
drawline(p3,p4);
break;
}
delay(5000);
closegraph();
}
void drawwindow()
{
line(150,100,450,100);
line(450,100,450,350);
line(450,350,150,350);
line(150,350,150,100);
}

void drawline(PT p1,PT p2)


{
line(p1.x,p1.y,p2.x,p2.y);
}
PT setcode(PT p)
{
PT ptemp;
if(p.y<100)
ptemp.code[0]='1';
else
ptemp.code[0]='0';
if(p.y>350)

ptemp.code[1]='1';
else
ptemp.code[1]='0';
if(p.x>450)
ptemp.code[2]='1';
else
ptemp.code[2]='0';
if(p.x<150)
ptemp.code[3]='1';
else
ptemp.code[3]='0';
ptemp.x=p.x;
ptemp.y=p.y;
return(ptemp);
37
CSX-328 Computer Graphics and Animation Lab

}
int visibility(PT p1,PT p2)
{
int i,flag=0;
for(i=0;i<4;i++)
{if((p1.code[i]!='0') || (p2.code[i]!='0'))
flag=1;
}
if(flag==0)
return(0);
for(i=0;i<4;i++)

{
if((p1.code[i]==p2.code[i]) && (p1.code[i]=='1'))
flag='0';
}

if(flag==0)
return(1);
return(2);

PT resetendpt(PT p1,PT p2)


{
PT temp;
int x,y,i;
float m,k;

if(p1.code[3]=='1')
x=150;
if(p1.code[2]=='1')
x=450;
if((p1.code[3]=='1') || (p1.code[2]=='1'))

{
m=(float)(p2.y-p1.y)/(p2.x-p1.x);
k=(p1.y+(m*(x-p1.x)));
temp.y=k;
temp.x=x;
for(i=0;i<4;i++)
temp.code[i]=p1.code[i];
if(temp.y<=350 && temp.y>=100)
return (temp);
}

if(p1.code[0]=='1')
y=100;
if(p1.code[1]=='1')
y=350;
if((p1.code[0]=='1') || (p1.code[1]=='1'))
38
CSX-328 Computer Graphics and Animation Lab

{
m=(float)(p2.y-p1.y)/(p2.x-p1.x);
k=(float)p1.x+(float)(y-p1.y)/m;
temp.x=k;
temp.y=y;
for(i=0;i<4;i++)
temp.code[i]=p1.code[i];
return(temp);
}
else
return(p1);
}

39
CSX-328 Computer Graphics and Animation Lab

Output:

40
CSX-328 Computer Graphics and Animation Lab

Practical 13
Aim: Implement Liang Barsky Line clipping Algorithm

#include<iostream>
#include<graphics.h>
#include<math.h>
#include<dos.h>
using namespace std;
int main()
{
int i,gd=DETECT,gm;
initgraph(&gd,&gm,"null");
initwindow(600,500, "null");
setfillstyle(1,7);
bar(0,0,600,500);
int x1,y1,x2,y2,xmin,xmax,ymin,ymax,xx1,xx2,yy1,yy2,dx,dy; float t1,t2,p[4],q[4],temp;
cout<<"The Window port has Coordinates as (100,100) and (250,250) as lower left and upper
right corners!!\n";
cout<<"Enter the coordinates of line joining two points:\n";
cin>>x1;
cin>>y1;
cin>>x2;
cin>>y2;
xmin=100;
ymin=100;
xmax=250;
ymax=250;
initgraph(&gd,&gm,"null");
rectangle(xmin,ymin,xmax,ymax);
dx=x2-x1;
dy=y2-y1;
p[0]=-dx;
p[1]=dx;
p[2]=-dy;
p[3]=dy;
q[0]=x1-xmin;
q[1]=xmax-x1;
q[2]=y1-ymin;
q[3]=ymax-y1;
for(i=0;i<4;i++)
{
if(p[i]==0)
{
cout<<"line is parallel to one of the clipping boundary";
if(q[i]>=0)
{
if(i<2)
{
if(y1<ymin)
{
41
CSX-328 Computer Graphics and Animation Lab

y1=ymin;
}
if(y2>ymax)
{
y2=ymax;

}
line(x1,y1,x2,y2);
}
if(i>1)
{
if(x1<xmin)
{
x1=xmin;
}
if(x2>xmax)
{
x2=xmax;
}
line(x1,y1,x2,y2);

}}}}
t1=0;
t2=1;

for(i=0;i<4;i++)
{temp=q[i]/p[i];
if(p[i]<0)
{
if(t1<=temp)
t1=temp;
}
else
{
if(t2>temp)
t2=temp;
}
}
if(t1<t2)
{
xx1 = x1 + t1 * p[1];
xx2 = x1 + t2 * p[1];
yy1 = y1 + t1 * p[3];
yy2 = y1 + t2 * p[3];
line(xx1,yy1,xx2,yy2);
}
delay(5000);
closegraph();
}

42
CSX-328 Computer Graphics and Animation Lab

Output:

43
CSX-328 Computer Graphics and Animation Lab

Practical 14

Aim: To Implement Polygon clipping Algorithm

#include<graphics.h>
#include<iostream>
#include<vector>
using namespace std;
struct coordinate{
double x;
double y;

};

int checkleft(coordinate first,coordinate second,int x_min)


{
if(first.x > x_min && second.x > x_min)
{
return 0;
}

if(first.x < x_min && second.x > x_min)


{ return 1;}
if(first.x > x_min && second.x < x_min)
{return 2; }
return 3;
}
vector <coordinate> sutharlandPolygonClipping(coordinate window[],coordinate
polygon[],int
vertices)
{
int x_min = window[0].x;
int x_max = window[1].x;
int y_min = window[0].y;
int y_max = window[3].y;
vector<coordinate> verticesAfterClippingOperation; //left clipping
for(int i=0;i<vertices;i++)
{
int id = checkleft(polygon[i],polygon[(i+1)%vertices],x_min);
switch(id)
{
case 0://both vertices are inside
verticesAfterClippingOperation.push_back(polygon[(i+1)%vertices]);

break;
case 1:// edge coming from outside to inside
{
coordinate intersection;

44
CSX-328 Computer Graphics and Animation Lab

intersection.x = x_min;

double slope = (polygon[(i+1)%vertices].y - polygon[i].y)/(polygon[(i+1)%vertices].x -


polygon[i].x);

intersection.y = slope * x_min + polygon[i].y - slope * polygon[i].x;

verticesAfterClippingOperation.push_back(intersection);

verticesAfterClippingOperation.push_back(polygon[(i+1)%vertices]);

break;

case 2://edge going from inside to outside

{
coordinate intersection;
intersection.x = x_min;

double slope = (polygon[(i+1)%vertices].y - polygon[i].y)/(polygon[(i+1)%vertices].x -


polygon[i].x);

intersection.y = slope * x_min + polygon[i].y - slope * polygon[i].x;


verticesAfterClippingOperation.push_back(intersection); break;

}}}

//top clipping

for(int i=0;i<verticesAfterClippingOperation.size();i++)

{
}
return verticesAfterClippingOperation;
}
int main()

{
int gd = DETECT,gm;
initgraph(&gd,&gm,"");
cout<<"enter no. of vertices in polygon"<<endl;
int vertices;
cin>>vertices;
coordinate polygon[vertices];
cout<<"enter x, y coordinates of each vertex respectively"<<endl;
for(int i=0;i<vertices;i++)
{
cin>>polygon[i].x>>polygon[i].y;
}
45
CSX-328 Computer Graphics and Animation Lab

coordinate window[4];
cout<<"enter the coordinates of window"<<endl;
for(int i=0;i<4;i++)
{
cin>>window[i].x>>window[i].y;
}
vector<coordinate> v = sutharlandPolygonClipping(window,polygon,vertices); for(int
i=0;i<v.size();i++)
{cout<<v[i].x<<" "<<v[i].y<<endl;
}
for(int i=0;i<4;i++)

line(window[i].x,window[i].y,window[(i+1)%4].x,window[(i+1)%4].y);
}
setcolor(4);
for(int i=0;i<vertices;i++)
{
line(polygon[i].x,polygon[i].y,polygon[(i+1)%4].x,polygon[(i+1)%4].y);
}
setcolor(2);
for(int i=0;i<v.size();i++)
{
line(v[i].x,v[i].y,v[(i+1)%4].x,v[(i+1)%4].y);
}
getch();
}

Output:

46
CSX-328 Computer Graphics and Animation Lab

Practical 15
Aim: To show man walking in rain
SOURCE CODE:
#include<graphics.h>

int main()
{
intgd = DETECT, gm;
initgraph(&gd, &gm, NULL);

int x = 100, y = 200;


int radius = 20;
intlenOfBody = 30;
intlenOfArm = 30;
int neck = 10;
intlenOfLeg = 30;
bool flag = true;

for(inti = 0; i< 100; i+=2, x += 10, flag = !flag)


{
outtextxy(450, y+radius+lenOfBody+lenOfLeg +10, "A Man Walking in the Rain");

//RAIN
for(int j = 0; j < 50; j++)
{
int X = rand()%1000;
int Y = rand()%(y+radius+lenOfBody+lenOfLeg-10);
outtextxy(X,Y,"'");

}
//HEAD
circle(x,y,radius);

//BODY
line(x,y+radius, x, y+radius+lenOfBody);

//ARMS
line(x, y+radius+neck, x+lenOfArm/2, y+radius+neck);
line(x+lenOfArm/2, y+radius+neck, x+lenOfArm, y+radius);

47
CSX-328 Computer Graphics and Animation Lab

//UMBRELLA
line(x+lenOfArm, y+radius, x+lenOfArm, y-radius-10);
line(x+lenOfArm-2*radius, y-radius-10, x+lenOfArm+2*radius, y-radius-10);
arc(x+lenOfArm, y-radius-10, 0, 180,2* radius);
floodfill(x+lenOfArm, y-radius-20, WHITE);
//LEGS
if(i%3 == 0)
{
line(x,y+radius+lenOfBody, x-lenOfArm+10, y+radius+lenOfBody+lenOfLeg);
line(x,y+radius+lenOfBody, x+lenOfArm-10, y+radius+lenOfBody+lenOfLeg);
}
else if(i%3 == 1)
{
line(x,y+radius+lenOfBody, x-lenOfArm/2, y+radius+lenOfBody+lenOfLeg);
line(x,y+radius+lenOfBody, x+lenOfArm/2, y+radius+lenOfBody+lenOfLeg);
}
else
{
line(x,y+radius+lenOfBody, x, y+radius+lenOfBody+lenOfLeg);
line(x,y+radius+lenOfBody, x, y+radius+lenOfBody+lenOfLeg);
}
//SURFACE
line(0,y+radius+lenOfBody+lenOfLeg ,1000,y+radius+lenOfBody+lenOfLeg);
delay(50);
cleardevice();
}

getch();
closegraph();

48
CSX-328 Computer Graphics and Animation Lab

49
CSX-328 Computer Graphics and Animation Lab

Practical 16

Aim:Program to show concentric circles.

SOURCE CODE:
#include<graphics.h>
int main()
{
intgd = DETECT, gm;
initgraph(&gd, &gm, NULL);
int x = 250, y = 250, color = 1;
for(int radius = 25; radius <= 125; radius+=20)
{
circle(x,y,radius);
}

getch();
closegraph();
}

50
CSX-328 Computer Graphics and Animation Lab

51
CSX-328 Computer Graphics and Animation Lab

52

You might also like