You are on page 1of 51

lOMoARcPSD|35169658

Computer Graphics and Animation journal 1

Computer Graphics (University of Mumbai)

Studocu is not sponsored or endorsed by any college or university


Downloaded by AzR Gaming (templategamingff@gmail.com)
lOMoARcPSD|35169658

Computer Graphics and Animation | Roll no 18011

Name: Sahbiya.Mohd.Saleem.Khan
Rollno: 18011 Semester:- Fourth
Subject: Computer Graphics and Animation
Academic: SY-BScIT
Professor-in-Charge:- Miss Rani Mario

Downloaded by AzR Gaming (templategamingff@gmail.com)


lOMoARcPSD|35169658

Computer Graphics and Animation | Roll no 18011

Sr.no Topic Pgno Date Remark


1. Solve the following:
a Study and enlist the basic functions used for graphics in C / C++ /
Python language. Give an example for each of them
b. Draw a co-ordinate axis at the center of the screen

2. Solve the following:


a. Divide your screen into four region, draw circle, rectangle, ellipse
and half ellipse in each region with appropriate message
b. Draw a simple hut on the screen

3 Draw the following basic shapes in the center of the screen :


i. Circle ii. Rectangle iii. Square iv. Concentric Circles v. Ellipse vi.
Line

4 Solve the following:


a. Develop the program for DDA Line drawing algorithm.
b. Develop the program for Bresenham’s Line drawing algorithm.

5. Solve the following:


a. Develop the program for the mid-point circle drawing algorithm.
b. Develop the program for the mid-point ellipse drawing algorithm.

6 Solve the following:


a. Write a program to implement 2D scaling.
b. Write a program to perform 2D translation

7. Solve the following:


a. Perform 2D Rotation on a given object.
b. Program to create a house like figure and perform the following
operations.
i.Scaling about the origin followed by translation.
ii. Scaling with reference to an arbitrary point.
iii. Reflect about the line y = mx + c.

8. Solve the following:


a. Write a program to implement Cohen-Sutherland clipping.
b. Write a program to implement Liang - Barsky Line Clipping
Algorithm
9. Solve the following:
a. Write a program to fill a circle using Flood Fill Algorithm.
b. Write a program to fill a circle using Boundary Fill Algorithm.
10. Solve the following:
a. Develop a simple text screen saver using graphics functions.
b. Perform smiling face animation using graphic functions.
c. Draw the moving car on the screen.

Downloaded by AzR Gaming (templategamingff@gmail.com)


lOMoARcPSD|35169658

Computer Graphics and Animation | Roll no 18011

Downloaded by AzR Gaming (templategamingff@gmail.com)


lOMoARcPSD|35169658

Computer Graphics and Animation | Roll no 18011

Practical no 1:- Solve the following


1a)
Aim: - Study and enlist the basic functions used for graphics in C/C++/Python language. Give an
example for each of them.
Answer:-

1.drawpoly() function which is used to draw polygons i.e. triangle, rectangle, pentagon, hexagon
etc

Code:-
#include<graphics.h>
#include<conio.h>
void main()

int gd=DETECT,gm,

int arr[] = {320, 150, 400, 250,250, 350, 320, 150};

initgraph(&gd,&gm,"C:\TC\BIN");

drawpoly(4, arr);

getch();

closegraph();

return 0;

Output:-

Conclusion:-
The above program has been executed successfully.

Downloaded by AzR Gaming (templategamingff@gmail.com)


lOMoARcPSD|35169658

Computer Graphics and Animation | Roll no 18011

2)

fillpoly() function which is used to draw and fill a polygon i.e. triangle, rectangle, pentagon,
hexagon etc. It require same arguments as drawpoly().

Code:-

#include<graphics.h>

#include<conio.h>

void main()

int gd=DETECT, gm;

int arr[]={320,150,400,250,250,350,320,150};

initgraph(&gd,&gm,"C:\TC\BIN");

fillpoly(4,arr);

getch();

closegraph();

return 0;

Output:-

Conclusion:-
The above program has been executed successfully.

Downloaded by AzR Gaming (templategamingff@gmail.com)


lOMoARcPSD|35169658

Computer Graphics and Animation | Roll no 18011

3)
setfillstyle() function which sets the current fill pattern and fill color.
floodfill() function is used to fill an enclosed area. Current fill pattern and fill color is used to fill
the area.

Code:-

#include<graphics.h>

#include<conio.h>

int main()

int gd=DETECT, gm;

initgraph(&gd,&gm,"C:\TC\BIN");

int x_circle =250;

int y_circle =250;

int radius=100;

int border_color = WHITE;

setfillstyle(HATCH_FILL,RED);

circle(x_circle,y_circle,radius);

floodfill(x_circle,y_circle,border_color);

getch();

closegraph();

return 0;

Downloaded by AzR Gaming (templategamingff@gmail.com)


lOMoARcPSD|35169658

Computer Graphics and Animation | Roll no 18011

Output:-

4
getx() function which returns the X coordinate of the current position.
Code:-

Downloaded by AzR Gaming (templategamingff@gmail.com)


lOMoARcPSD|35169658

Computer Graphics and Animation | Roll no 18011

1b)
Aim:- Draw a co-ordinate axis at the centre of the screen.
Code:-
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
clrscr();
Int gdriver = DETECT, gmode;
initgraph(&gdriver,&gmode,=C:\\TC\\BGI=);
int xmax, ymax;
xmax = getmaxx();
ymax = getmaxy();
Iine(xmax/2, 0, xmax/2,ymax);
Iine(0, ymax/2, xmax, ymax/2);
getch();
closegraph();
}
Output:-

Screenshot:-

Conclusion:-
The above program has been executed successfully.

Downloaded by AzR Gaming (templategamingff@gmail.com)


lOMoARcPSD|35169658

Computer Graphics and Animation | Roll no 18011

Practical no 2:- Solve the following


2a)
Aim:- Divide your screen into four region, draw circle, rectangle, ellipse and half ellipse in each
region with appropriate message.
Code:-
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
int main(void)
{
clrscr();
int gdriver = DETECT, gmode;
initgraph(&gdriver,&gmode, <C:\\TC\\BGI=);
int xmax, ymax;
setcolor(getmaxx());
xmax = getmaxx();
ymax = getmaxy();
line(xmax/2,0,xmax/2,ymax);
line(0,ymax/2,xmax,ymax/2);
circle(170,125,100);
outtextxy(170,125,=circle=);
rectangle(58,251,304,392);
outtextxy(160,300,=Rectangle=);
arc(500,150,45,135,100);
outtextxy(500,150,=semi circle=);
ellipse(500,300,0,360,75,25);
outtextxy(500,300,=ellipse=);
getch();
closegraph();
return 0;
}
Output:-

Downloaded by AzR Gaming (templategamingff@gmail.com)


lOMoARcPSD|35169658

Computer Graphics and Animation | Roll no 18011

Screenshot:-

Conclusion:- The above program has been executed successfully.

Downloaded by AzR Gaming (templategamingff@gmail.com)


lOMoARcPSD|35169658

Computer Graphics and Animation | Roll no 18011

2b)
Aim:- Draw a simple hut on the screen.
Code:-
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main()
{
clrscr();
int gd=DETECT, gm;
initgraph(&gd,&gm,=C:\TC\BIN=);
setcolor(WHITE);
rectangle(150,180,250,300);
rectangle(250,180,420,300);
rectangle(180,250,220,300);
line(200,100,150,180);
line(200,100,250,180);
line(200,100,370,100);
line(370,100,420,180);
setfillstyle(SOLID_FILL,RED);
floodfill(152,182,WHITE);
floodfill(252,182,WHITE);
setfillstyle(SLASH_FILL,BLUE);
floodfill(182,252,WHITE);
stylefillstyle(HATCH_FILL,GREEN);
floodfill(200,105,WHITE);
floodfill(210,105,WHITE);
getch();
closegraph();
return 0;
}
Output:-

Downloaded by AzR Gaming (templategamingff@gmail.com)


lOMoARcPSD|35169658

Computer Graphics and Animation | Roll no 18011

Screenshot:-

Conclusion:- The above program has been executed successfully.

Downloaded by AzR Gaming (templategamingff@gmail.com)


lOMoARcPSD|35169658

Computer Graphics and Animation | Roll no 18011

Practical no 3:-Draw the following basic shapes in the centre of the screen:-
Aim:-
I.Circle
II.Rectangle
III.Square
IV.Concentric Circles
V.Ellipse
VI.Line

Code:-
#include<graphics.h>
#include<stdlib.h>
#include<conio.h>
#include<stdio.h>
Int main(void)
{
int gd = DETECT,gm,errorcode;
int midx,midy,eft,top,right,bottom;
int radius=100;
initgraph(&gd, &gm, <C:\TC\BIN=);
printf(<%d=,getmaxx());
midx = getmaxx() / 2;
midy = getmaxy() /2;
circle(midx,miy,radius);
getch();
left= getmaxx() / 2 –50;
top =getmaxy() /2 - 50;
right = getmaxx() / 2 + 50;
bottom = getmaxy() /2+50
rectangle(left,top,right,bottom);
getch();
rectangle(midx-20 , midy-20, midx+20, midy+20);
getch();
ellipse(midx,midy,0,360,100,50);
getch();
closegraph();
return 0;
)

Downloaded by AzR Gaming (templategamingff@gmail.com)


lOMoARcPSD|35169658

Computer Graphics and Animation | Roll no 18011

Output:-

Screenshot:-

Conclusion:- The above program has been executed successfully.

Downloaded by AzR Gaming (templategamingff@gmail.com)


lOMoARcPSD|35169658

Computer Graphics and Animation | Roll no 18011

Downloaded by AzR Gaming (templategamingff@gmail.com)


lOMoARcPSD|35169658

Computer Graphics and Animation | Roll no 18011

Practical no 4:-Solve the following:


4a)
Aim:-Develop the program for DDA Line drawing algorithm
Code:-
#include<stdio.h>
# include< graphics. h>
#include<conio.h>
#include<math.h>
void main()
{
int x,y,x1,y1,x2,y2,dx,dy,step,xinc,yinc,I,gd=DETECT,gm;
clrscr();
initgraph(&gd,&gm,=C:\TC\BIN=)9

printf(<enter the starting coordinates:=);


Scanf(%d%d=,&x1,&y1);

printf(<Enter the ending coordinates:=);


scanf(<%d%d=,&x2,&y2);

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,4);

for(i=0;i<step;i++0
{
x=x+xinc;
y=y+yinc;
putpixel(x,y,4);
}
getch();
closegraph();
}

Downloaded by AzR Gaming (templategamingff@gmail.com)


lOMoARcPSD|35169658

Computer Graphics and Animation | Roll no 18011

Output: -

Screenshot:-

Conclusion:-
The above program has been executed successfully.

Downloaded by AzR Gaming (templategamingff@gmail.com)


lOMoARcPSD|35169658

Computer Graphics and Animation | Roll no 18011

4b)
Aim:-Develop the program forBresenham’s Line drawing algorithm.
Code:-
#include<graphics.h>
#include<conio.h>
#include<dos.h>

void main()
{
int x1,y1,x2,y2,dx,dy,p;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\BIN");
cout<<"enter the values of x1,y1"<<endl;
cin>>x1>>y1;
cout>>x2>>y2;
dx=x2-x1;
dy=y2-1;
p=2*dy-dx;
putpixel(x1,y1,3);
for(int k=0;k<dx;k++)
{
if(p<0)
{
x1++;
p=p+2*dy;
}
else
{
x1++;
y1++;
p=p+2*dy-2*dx;
}
putpixel(x1,y1,3);
delay(50);
}
getch();
closegraph();
}

Downloaded by AzR Gaming (templategamingff@gmail.com)


lOMoARcPSD|35169658

Computer Graphics and Animation | Roll no 18011

Output:-

Screenshot:-

Conclusion:-
The above program has been executed successfully.

Downloaded by AzR Gaming (templategamingff@gmail.com)


lOMoARcPSD|35169658

Computer Graphics and Animation | Roll no 18011

Practical no:-5 Solve the following:


5a)
Aim:- Develop the program for the mid-point circle drawing algorithm.
Code:-
#include<iostrem.h>
#include<conio.h>
#include<grphics.h>
void main()
{
int xc,yc,x,r,gd=DETECT,gm,d;
cout<<=enter radius=<<endl;
cin>>r;
x=0;
y=r;
d=1-r;
initgraph(&gd,&gm,=C:\\TC\\BIN=);
do
{
putpixel(xc+x,yc+y,4);
putpixel(xc+x,yc-y,4);
putpixel(xc-x,yc+y,4);
putpixel(xc-x,yc-y,4);
putpixel(xc+y,yc+x,4);
putpixel(xc+y,yc-x,4);
putpixel(xc-y,yc+x,4);
putpixel(xc-y,yc-x,4);
if(d<0)
{
x=x+1;
Y=y;
d=d+2*x+2;
}
else
{
x=x+1;
y=y-1;
d=d+2*(x-y)+1;
}
}
while(x<y);
getch();
closegraph();
}

Downloaded by AzR Gaming (templategamingff@gmail.com)


lOMoARcPSD|35169658

Computer Graphics and Animation | Roll no 18011

Output:-

Screenshot:-

Conclusion:-
The above program has been executed successfully.

Downloaded by AzR Gaming (templategamingff@gmail.com)


lOMoARcPSD|35169658

Computer Graphics and Animation | Roll no 18011

5b)
Aim:-Develop the program for the mid-point ellipse drawing algorithm.
Code:-
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
void main()
{
int gd=DETECT,gm;
float xc,yc,rx,ry,x,y,pk,p1k,p1k;p2k;
initgraph(&gd,&gm,=c:\\tc\\bin=);
scanf(<%f %f= , &rx,&ry);
printf(<\nenter centre of ellipse=);
scanf(<%f %f= , &rx,&ry);
x=0; y=ry;
pk=(ry*ry)-(rx*rx*ry)+((rx*rx*)/4);
while((2*ry*ry*x)<(2*rx*rx*y))
{
If (pk<=0)
{
x=x+1;
p1k=pk+(2*ry*ry*x)-(2*rx*rx*y)+(ry*ry);
}
else
{
x=x+1;
y=y+1;
p1k=pk+(2*ry*ry*x)-(2*rx*rx*y)+(ry*ry);
}
pk=p1k;
putpixel(xc+x,yc+y,WHITE)
putpixel(xc-x,yc+y,WHITE)
putpixel(xc+x,yc-y,WHITE)
putpixel(xc-x,yc-y,WHITE)
}
pk=((x-0.5)*(x+0.5)*ry*ry)+((y-1)*(y-1)*rx*rx)-(rrx*rx*ry*ry);
while(y>0)
{
If(pk>0)
{
y+y-1;
p1k=pk-(2*rx*rx*y)+(rx*rx);
}
pk=p1k;
putpixe(xc+x,yc+y,WHITE);
putpixe(xc-x,yc+y,WHITE);
putpixe(xc+x,yc-y,WHITE);
putpixe(xc-x,yc-y,WHITE);
}
line(xc+rx,yc,xc-rx,yc);
line(xc,yc+ry,xc,yc-ry);
getch();

Downloaded by AzR Gaming (templategamingff@gmail.com)


lOMoARcPSD|35169658

Computer Graphics and Animation | Roll no 18011

Output:-

Screenshot:-

Conclusion:-
The above program has been executed successfully.

Downloaded by AzR Gaming (templategamingff@gmail.com)


lOMoARcPSD|35169658

Computer Graphics and Animation | Roll no 18011

Practical no:- 6 Solve the following


6a)
Aim:- Write a program to implement 2D scaling.
Code:-
#include<stdio.h>
#include<graphic.h>
#include<math.h>
#include<conio.h>
int gd- DETECT, gm;
int n,x[100],y[100],I;
float sfx,sfy;
void draw();
void scale();
void main()
{
clrscr();
printf(<enter number of sides:=);
scanf(<%d=,&n);
printf(<enter co-ordinates:x,y for each points=);
for(i=0;i<n;i++)
scanf(<%f %f=,&x[i],&y[i]);
printf(<enter scale factors: (sfx,sfy)=);
scanf(<%f%f=,&sfx,&sfy);
initgraph(&gd,&gm,=C:\\TC\\BIN=);
cleardevice();
setcolor(CYAN);
draw();
scale();
getch();
}
void draw()
{
for(i=0;i<n;i++)
line (x[i],y[i],x[(i+1)%n],y[(i+1)%n]);
}
void scale()
{
for(i=0;i<n;i++)
{
x[i]=x[0]+(int)((float)(x[I]-x[0]*sfx);
y[i]=y[0]+(int)9float)(y[I]-y[0]*sfy);
}
}

Downloaded by AzR Gaming (templategamingff@gmail.com)


lOMoARcPSD|35169658

Computer Graphics and Animation | Roll no 18011

Output:-

Downloaded by AzR Gaming (templategamingff@gmail.com)


lOMoARcPSD|35169658

Computer Graphics and Animation | Roll no 18011

Screenshot:-

Conclusion:-
The above program has been executed successfully.

Downloaded by AzR Gaming (templategamingff@gmail.com)


lOMoARcPSD|35169658

Computer Graphics and Animation | Roll no 18011

6b)
Aim:- Write a program to perform 2d translation.
Code:-
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
int gd=DETECT,gm;
int n,xs[100],ys[100],i,ty,tx;
void draw();
void translate();
void main()
{
printf(<Enter number of sides of polygon: <);
scanf(<%d=,&n);
printf(<Enter co-ordinates: x,y for each vertex=);
for(i=0;i<n;i++)
Scanf(8%d%d=, &xs[i],&ys[i]);
printf(<Enter distances for translation (in x and y directions <);
scanf(%d%d=,&tx,&ty);
initgraph(&gd,&gm,=C:\TC\BIN\=);
cleardevice();
setcolor(RED);
draw();
translate();
setcolor(YELLOW);
draw();
getch();
}
void draw()
{
for(i=0;i<n;i++)
line(xs[i],ys[i],xs[(i+1)%n],ys[(i+1)%n]);
}
void translate()
{
for(i=0;i<n;i++)
{
xs[i]+=tx;
ys[i]+=ty;
}

Output:-

Downloaded by AzR Gaming (templategamingff@gmail.com)


lOMoARcPSD|35169658

Computer Graphics and Animation | Roll no 18011

Screenshot:-

Conclusion:-
The above program has been executed successfully.

Downloaded by AzR Gaming (templategamingff@gmail.com)


lOMoARcPSD|35169658

Computer Graphics and Animation | Roll no 18011

Practical no7:-Solve the following:


7a)
Aim:- Perform 2d rotation on a given object
Code:-
#include<stdio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int x1,y1,x2,y2;
float b1,b2;
float t,deg;
initgraph(&gd,&gm,=C:\\TC\\BIN=);
printf(<Enter the coordinates of line\n=);
scanf(<%d%d%d%d=,&x1,&y1,&x2,&y2);
setcolor(6);
line(x1,y1,x2,y2);
getch();
printf(<Enter the angle of rotation:=);
scanf(<%f=,&deg);
t=(22*deg)/(180*7);
b1=abs((x2*cos(t))-(y2*sin(t)));
B2=abs((x2*sin(t)+(y2*cos(t)));
setcolor(7);
line(x1,y1,b1,b2);
getch();
closegraph();
}
Output:-

Downloaded by AzR Gaming (templategamingff@gmail.com)


lOMoARcPSD|35169658

Computer Graphics and Animation | Roll no 18011

Screenshot:-

Conclusion:-
The above program has been executed successfully.

Downloaded by AzR Gaming (templategamingff@gmail.com)


lOMoARcPSD|35169658

Computer Graphics and Animation | Roll no 18011

7b)
Aim:- Program to create a house like figure and perform the following operations
i.Scaling about the origin followed by translation.
ii.Scaling with reference to an arbitrary point.
iii.Reflect about the line y=mx+c.
Code:-
#include<stdio.h>
#include<graphic.h>
#include<stdlib.h>
#include<math.h>
#include<conio.h>

void reset (int h[][2])


{
int val[9][2] = {
{ 50, 50 } ,{ 75, 50 },{75, 75 },{ 100, 75}, {100, 50 }, { 125, 50} ,{125, 100 }, {87, 125 }, {50, }
};
int I;
For(i=0; i<9; i++)
{
h[i][0] = val[i][0]-50;
h[i][1] = val[i][1]-50;
}
}
void draw (int h[][2])
{
int i;
Setlinestyle (DOTTED_LINE, 0, 1);
line (320,0,320,480);
line(0,240,640,240);
Setlinestyle(SOLID_LINE, 0, 1);
for (i=0; i<8; i++)
line(320+h[i][0],240-h[i][1] , 320+h[i+1][1]), 240-h[i+1][1]);
line(320+h[0][0], 240-h[0][1], 320+h[8][0], 240-h[8][1]);
}
void rotate (int h[][2], float angle)
{
int i;
for (i=0; i<9; i++)
{
int xnew,ynew;
xnew = h[i][0] *cos (angle) - h[i][1] *sin (angle);
ynew = h[i][0]*sin (angle) + h[i][1]*cos(angle);
h[i][0] = xnew; h[i][1] = ynew;
}
}
void scale (int h[][2], int sx, int sy)
{
int i;
for (i=0; i<9; i++)
{
h[i][0] *=sx;
h[i][1]*=sy;

Downloaded by AzR Gaming (templategamingff@gmail.com)


lOMoARcPSD|35169658

Computer Graphics and Animation | Roll no 18011

}
}
void translate (int h[][2], int dx, int dy)
{
int i;
for (i=0; i<9; i++)
{
h[i][0] += dx;
h[i][1] +=dy;
}
}
void reflect (int h[][2], int m, int c)
{
int i;
float angle;
for (i=0; i<9; i++)
h[i][1]-= c;
angle = M_P1/2 - atan (m);
rotate (h,angle);
for(i=0;i<9;i++)
h[i][1] += c;
}
void ini()
{
int gd=DETECT, gm;
initgraph(&gd,&gm,=C://TC//BIN=);
}
void dini()
{
getch();
closegraph();
}
void main()
{
Int h[9][2],sx,sy,x,y,m,c,choice;
Do
{
clrscr();
printf(<1. Scaling about the origin. \n=);
printf(<2. Scaling about the origin. \n=);
printf(<3. Scaling about the origin. \n=);
printf(<4. Scaling about the origin. \n=);
printf(<Enter the choice: =);
scanf(<%d=,&choice);
switch(choice)
{

Downloaded by AzR Gaming (templategamingff@gmail.com)


lOMoARcPSD|35169658

Computer Graphics and Animation | Roll no 18011

Case 1: printf(<Enter the x- and y-scaling factors:=);


scanf (<%d%d=,&sx,&sy);
ini();
rest (h);
draw(h);getch();
scale (h, sx, sy);
cleardevice();
draw (h);
dini();
break;
Case 2: printf(<Enter the x-and y-scalling factors:=);
scanf(<%d%d,&sx,&sy);
printf(<Enter the x- and y-coordinates of the point: <)
scanf(<%d%d=,&x,&y);
ini();
reset (h);
translate(h,-x,-y);
draw(h);
cleardevice();
translate(h,-x,-y);
draw(h);
getch();
cleardevice();
scale (h, sx,sy);
draw(h);
putpixel(320*240-y, WHITE);
dini();
break;
case 3: printf( <Enter the values of m and c:=);
scanf(<%d%d,&m,&c);
ini();
rest (h);
draw (h);
getch();
reflect (h, m, c);
cleardevice();
draw (h);
dini();
break;
case 4: exit(0);
}
} while(choice!=4);
}

Downloaded by AzR Gaming (templategamingff@gmail.com)


lOMoARcPSD|35169658

Computer Graphics and Animation | Roll no 18011

Output:-

Screenshot:-

Conclusion:-
The above program has been executed successfully.

Downloaded by AzR Gaming (templategamingff@gmail.com)


lOMoARcPSD|35169658

Computer Graphics and Animation | Roll no 18011

Practical no8 Solve the following


8a)
Aim:-Write a program to implement Cohen Sutherland clipping.
Code:-
#include<graphics.h>
#include<iostream.h>
#include<conio.h>
#include<iostream.h>
static int LEFT=1,RIGHT=2, BOTTOM=4, TOP=8, xmin,ymin,xmax,ymax;
int getcode(int x,int y)
{
int code=0;
if (y>ymax) code!=TOP;
if (y>ymin) code!=BOTTOM;
if (y>ymax) code!=LEFT;
if (y>ymax) code!=RIGHT;
return code;
}
void main()
{
int gd=DETECT, gm;
initgraph(&gd,&gm,=C:\TC\BIN\=);
setcolor(WHITE);
cout<<=enter windows minimum &maximum values=;
cin>>xmin>>ymin>>xmax>>ymax;
rectangle(xmin,ymin,xmax,ymax);
int x1,y1,x2,y2;
cout<<=enter the endpoints of the line=;
cin>>x1>>y1>>x2>>y2;
line(x1,y1,x2,y2);
getch();
int outcode1=getcode(x1,y1),outcode2=getcode(x2,y2);
int accept=0;
while(1)
{
float m=(float)(y2-y1)/(x2-x1);
if(outcode1==0 && outcode2==0)
{
accept=1;
break;
}
else if((outcode1 & outcode2)!=0)
{
break;
}
else
{
int x,y;
int temp;
if(outcode1==0)
temp=outcode1;
else
temp=outcode1;

Downloaded by AzR Gaming (templategamingff@gmail.com)


lOMoARcPSD|35169658

Computer Graphics and Animation | Roll no 18011

if(temp & TOP)


{
x=x1+(ymax-y1)/m;
y=ymax;
}
else if(temp &BOTTOM)
{
x=x1+(ymin-y1)/m;
y=ymin;
}
else if (temp & LEFT)
{
x=xmin;
y=y1+m*(xmin-x1);
}
else if (temp & RIGHT)
{
x=xmax;
y=y1+m*(xmax-x1);
}
if (temp==outcode1)
{
x1=x;
y1=y;
outcode1=getcode(x1,y1);
}
else
{
x2=x;
y2=y;
outcode2=getcode(x2,y2);
}
}
}

cout<<=after clipping=;
If(accept)
cleardevice();
rectanglr(xmin,ymin,xmax,ymax);
setcolor(RED);
line(x1,y1,x2,y2);
getch();
closegraph();
}

Downloaded by AzR Gaming (templategamingff@gmail.com)


lOMoARcPSD|35169658

Computer Graphics and Animation | Roll no 18011

Output:-

Screenshot:-

Conclusion:-
The above program has been executed successfully.

Downloaded by AzR Gaming (templategamingff@gmail.com)


lOMoARcPSD|35169658

Computer Graphics and Animation | Roll no 18011

8b)
Aim:- Write a program to implement Liang Barsky line Clipping Algorithm
Code:-
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<math.h>
void main()
{
Int a[4],b[4];
float m,xnew,ynew;
float x1=100,y1=100,xh=300,yh=300,xa=10,ya=200,xb=250,yb=150;
int gd = DETECT,gm;
initgraph(&gd,&gm,=C:\TC\BIN=);
setcolor(5);
line(xa,ya,xb,yb);
setcolor(12);
rectangle(x1,y1,xh,yh);
m = (yb-ya)/(xb-xa);
If(xa < x1)
a[2] = 1;
else a[3] =0;

If (xa>xh)
a[2] = 1;
else a[2]=0;

If(ya < y1)


a[1] =1;
else a[1] =0;

If(ya > yh)


a[0] = 1;
else a[0] = 0;

If(xb <x1)
b[3] = 1;
else b[3] = 0;
if(xb>xh)
b[2] = 1;
else b[2] = 0;

if(yb < yl)


b[1] = 1;
else b[1] = 0;

if (yb > yh)


b[0] = 1;
else b[0] = 0;
printf("press a key to continue");
getch();
if(a[0] == 0 && a[1] == 0 && a[2] == 0 && a[3] == 0 && b[0] == 0 && b[1] == 0 && b[2] == 0 &&
b[3] == 0 )

Downloaded by AzR Gaming (templategamingff@gmail.com)


lOMoARcPSD|35169658

Computer Graphics and Animation | Roll no 18011

printf("no clipping");
line(xa,ya,xb,yb);
}
else if(a[0]&&b[0] || a[1]&&b[1] || a[2]&&b[2] || a[3]&&b[3])
{
clrscr();
printf("line discarded");
rectangle(xl,yl,xh,yh);
}
else
{
if(a[3] == 1 && b[3]==0)
{
ynew = (m * (xl-xa)) + ya;
setcolor(12);
rectangle(xl,yl,xh,yh);
setcolor(0);
line(xa,ya,xb,yb);
setcolor(15);
line(xl,ynew,xb,yb);
}
else if(a[2] == 1 && b[2] == 0)
{
ynew = (m * (xh-xa)) + ya;
setcolor(12);
rectangle(xl,yl,xh,yh);
setcolor(0);
line(xa,ya,xb,yb);
setcolor(15);
line(xl,ynew,xb,yb);
}
else if(a[1] == 1 && b[1] == 0)
{
xnew = xa + (yl-ya)/m;
setcolor(0);
line(xa,ya,xb,yb);
setcolor(15);
line(xnew,yh,xb,yb);
}

else if(a[0] == 1 && b[0] == 0)


{
xnew = xa + (yh-ya)/m;
setcolor(0);
line(xa,ya,xb,yb);
setcolor(15);
line(xnew,yh,xb,yb);
}
}
getch();
closegraph();

Downloaded by AzR Gaming (templategamingff@gmail.com)


lOMoARcPSD|35169658

Computer Graphics and Animation | Roll no 18011

Output:-

Screenshot:-

Conclusion:-
The above program has been executed successfully.

Downloaded by AzR Gaming (templategamingff@gmail.com)


lOMoARcPSD|35169658

Computer Graphics and Animation | Roll no 18011

Practical no 9 Solve the following:


9a)
Aim:- Write a program to fill a circle using Flood Fill Algorithm
Code:-
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void flood(int x,int y,int fillcolor, int oldcolor)
{
Int current;
current=getpixel(x,y);
If(current==oldcolor)
{
delay(5);
putpixel(x,y,fillcolor);
flood(x+1,y,fillcolor,oldcolor);
flood(x-1,y,fillcolor,oldcolor);
flood(x,y+1,fillcolor,oldcolor);
flood(x,y-1,fillcolor,oldcolor);
}
}
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,=C://TC//BIN\=);
rectangle(50,50,100,100);
flood(55,55,RED,0);
flood(55,55,BUE,RED);
getch();
closegraph();
}
Output:-

Downloaded by AzR Gaming (templategamingff@gmail.com)


lOMoARcPSD|35169658

Computer Graphics and Animation | Roll no 18011

Screenshot:-

Conclusion:-
The above program has been executed successfully.

Downloaded by AzR Gaming (templategamingff@gmail.com)


lOMoARcPSD|35169658

Computer Graphics and Animation | Roll no 18011

9b)
Aim:-Write a program to fill a circle using Boundary Fill Algorithm.
Code:-
#include<graphics.h>
#include<conio.h>
void boundaryFill(int x,int y, int newColor, int oldColor)
{
if((getpixel(x,y)!=newColor) && (getpixel(x,y)!=oldColor))
{
setcolor(newColor);
putpixel(x,y,newColor);
delay(5);
boundaryFill(x+1,y,newColor,oldColor);
boundaryFill(x-1,y,newColor,oldColor);
boundaryFill(x,y+1,newColor,oldColor);
boundaryFill(x,y-1,newColor,oldColor);
}
{
setcolor(newColor);
putpixel(x,y);

}
}
Void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,=C:\\TC\\BIN=);

setcolor(10);
circle(250,200,30);
boundaryFill(251,201,12,10);

getch();
closegraph();
}
}
Output:-

Downloaded by AzR Gaming (templategamingff@gmail.com)


lOMoARcPSD|35169658

Computer Graphics and Animation | Roll no 18011

Screenshot:-

Conclusion:-
The above program has been executed successfully.

Downloaded by AzR Gaming (templategamingff@gmail.com)


lOMoARcPSD|35169658

Computer Graphics and Animation | Roll no 18011

Practical no 10:-Solve the following:-


10a)
Aim:-Develop a simple text screen saver using graphics functions.
Code:-
#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>
#include <conio.h>

void main(){
int gdriver=DETECT,gmode,col=480,row=640,font=4,direction=2,size=8,color=15;
initgraph(&gdriver,&gmode,"C:\\TC\\BIN");
cleardevice();
while(!kbhit()){
settextstyle(random(font),random(direction),random(size));
setcolor(random(color));
outtextxy(random(col),random(row),"five finger");
delay(250);
}
closegraph();
}
Output:-

Screenshot:-

Conclusion:-
The above program has been executed successfully.

Downloaded by AzR Gaming (templategamingff@gmail.com)


lOMoARcPSD|35169658

Computer Graphics and Animation | Roll no 18011

10b)

Aim:-Perform smiling face animation using graphic functions.


Code:-
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&g,=C:\\TC\\BIN=);
circle(200,200,80);
setfillstyle(1,YELLOW);
floodfill(200,200,15);
ellipse(220,180,0,360,8,13);
setfillstyle(1,BLACK);
floodfill(220,180,15);
ellipse(220,180,0,360,8,13);
setfillstyle(1,BLACK);
Floodfill(220,180,15);
setcolor(BLACK);
ellipse(200,225,185,355,32,20);
getch();
closegraph();
}
Output:-

Screenshot:-

Conclusion:-
The above program has been executed successfully.

Downloaded by AzR Gaming (templategamingff@gmail.com)


lOMoARcPSD|35169658

Computer Graphics and Animation | Roll no 18011

Downloaded by AzR Gaming (templategamingff@gmail.com)


lOMoARcPSD|35169658

Computer Graphics and Animation | Roll no 18011

10c)
Aim:-Draw the moving car on the screen.
Code:-
#include<iostream.h>
#include<conio.h>
#include<graphic.h>
#include<dos.h.
void main()
{
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,=C://TC//BIN=)

line(0,400,639,390);
for(int i=0;i<500:i++)
{
line(50 +i,370,90 +i,370);
arc(110 +i,370,0,180 +i,20);
line(130 +i,370,220 +i,370);
arc(240 +i,370,0,180 +i,20);
line(260 +i,370,300 +i,370);
line(300 +i,370,300 +i,330);
line(300 +i,350,240 +i,300);
line(240 +i,330,220 +i,300);
line(200 +i,300,110 +i,300);
line(11 0 +i,300,80 +i,330);
line(80 +i;330,50 +i,340);
line(50+i,340,50 +i,370);

line(165 +i,305,165 +i,330);


line(165 +i,330,230 +i,330);
line(230 +i,330,195 +i,305);
line(195 +i,305,165 +i,305);

line(160 +i,305,160 +i, 330)


line(160 +i,330,100 +i,330);
line(100 +i,330,120 +i,305);
line(120 +i,305,160 +i,305);

circle(110 +i,370,17);
circle(240 +i,370,17);

delay(10);
cleardevice();
}

getch();

Downloaded by AzR Gaming (templategamingff@gmail.com)


lOMoARcPSD|35169658

Computer Graphics and Animation | Roll no 18011

Output:-

Screenshot:-

Conclusion:-
The above program has been executed successfully.

Downloaded by AzR Gaming (templategamingff@gmail.com)


lOMoARcPSD|35169658

Computer Graphics and Animation | Roll no 18011

Downloaded by AzR Gaming (templategamingff@gmail.com)

You might also like