You are on page 1of 9

Terna Engineering College

Computer Engineering Department


Program: Sem III

Course: Computer Graphics

Faculty:
LAB Manual
PART A
(PART A : TO BE REFFERED BY STUDENTS)
Experiment No.12
A.1 Aim:
Implement Mini Project

A.2 Prerequisite:
1. C Language.
2. Geometric Concepts.
3. Concept of 2D and 3D basic Transformations.
4. Projection Concepts.

A.3 Outcome:
After successful completion of this experiment students will be able to,
1. Implement various line, circle, ellipse drawing algorithms.
2. Implement various output and filled area primitive algorithms.
3. Apply the transformations and clipping algorithms on graphical objects.
4. Implement the curve and fractal generation.
5. Implement Parallel and Perspective projection of a 3D object on Projection Plane.
6. Apply Character Generation Techniques and OpenGL.

A.4 Theory and Procedure:


Animation is a dynamic medium in which images or objects are manipulated to appear
as moving images. In traditional animation the images were drawn (or painted) by hand
on cels to be photographed and exhibited on film. Nowadays most animations are made
with computer-generated imagery (CGI). Computer animation can be very detailed 3D
animation, while 2D computer animation can be used for stylistic reasons, low
bandwidth or faster real-time renderings. Other common animation methods apply
a stop motion technique to two and three-dimensional objects like paper
cutouts, puppets or clay figures. The stop motion technique where live actors are used as
a frame-by-frame subject is known as pixilation.
Commonly the effect of animation is achieved by a rapid succession of sequential images
that minimally differ from each other. The illusion—as in motion pictures in general—is
thought to rely on the phi phenomenon and beta movement, but the exact causes are still
uncertain. Analogmechanical animation media that rely on the rapid display of
sequential images include the phénakisticope, zoetrope, flip book, praxinoscopeand
film. Television and video are popular electronic animation media that originally
were analog and now operate digitally. For display on the computer, techniques
like animated GIF and Flash animation were developed.
Apart from short films, feature films, animated gifs and other media dedicated to the
display moving images, animation is also heavily used for video games, motion
graphics and special effects.
The physical movement of image parts through simple mechanics in for instance the
moving images in magic lantern shows can also be considered animation. Mechanical
animation of actual robotic devices is known as animatronics.
Write a program in C for bouncing ball animation using graphics.h header file
In this program, we will draw a red color ball move it vertically up and down like a
bouncing ball. We will use below mentioned functions in this program.

Function Description

initgraph It initializes the graphics system by loading the passed graphics driver
then changing the system into graphics mode.

getmaxx It returns the maximum X coordinate in current graphics mode and driver.

setcolor It changes the current drawing colour. Default colour is white. Each color
is assigned a number, like BLACK is 0 and RED is 4. Here we are using
colour constants defined inside graphics.h header file.

setfillstyle It sets the current fill pattern and fill color.

circle It draws a circle with radius r and centre at (x, y).

floodfill It is used to fill a closed area with current fill pattern and fill color. It takes
any point inside closed area and color of the boundary as input.
Function Description

cleardevice It clears the screen, and sets current position to (0, 0).

kbhit It is used to determine whether a key is pressed or not. It returns a non-


zero value if a key is pressed otherwise zero.

delay It is used to suspend execution of a program for a M milliseconds.

closegraph It unloads the graphics drivers and sets the screen back to text mode.

C program for bouncing ball graphics animation

#include <stdio.h>
1 #include <conio.h>
2 #include <graphics.h>
3 #include <dos.h>
4
5 int main() {
int gd = DETECT, gm;
6
int i, x, y, flag=0;
7 initgraph(&gd, &gm, "C:\\TC\\BGI");
8
9 /* get mid positions in x and y-axis */
10 x = getmaxx()/2;
11 y = 30;
12
13
while (!kbhit()) {
14 if(y >= getmaxy()-30 || y <= 30)
15 flag = !flag;
16 /* draws the gray board */
17 setcolor(RED);
18 setfillstyle(SOLID_FILL, RED);
circle(x, y, 30);
19 floodfill(x, y, RED);
20
21 /* delay for 50 milli seconds */
22 delay(50);
23
24 /* clears screen */
cleardevice();
25 if(flag){
26 y = y + 5;
27 } else {
y = y - 5;
28 }
29 }
30
31 getch();
32 closegraph();
33 return 0;
}
34
35
36
37
38
39
40
Program
Output
Here is the screenshot of bouncing ball.
PART B
(PART B : TO BE COMPLETED BY STUDENTS)

Roll No. C28 Name: Prathmesh Krishna Gaikwad


Class : SE-C Batch : B-1
Date of Experiment: 03/12/2021 Date of Submission: 03/12/2021
Grade :
B.1 Document created by the student:

Program:

#include<stdio.h>
#include<ctype.h>
#include<conio.h>
#include<stdlib.h>
#include<dos.h>
#include<graphics.h>
#include<time.h>
int x1,y1,x2,y2,x1old,x2old,ycenter,xcenter;
int xnewcenter,ynewcenter,radius,ballspeedx,ballspeedy;
char sym,pit[20];
int speed=10;
void ball()
{
time_t t;
xcenter=xnewcenter;
ycenter =ynewcenter;
xnewcenter=xcenter+ballspeedx;
ynewcenter=ycenter+ballspeedy;
setcolor(0);
setfillstyle(1,0);
fillellipse(xcenter,ycenter,radius,radius);
setfillstyle(1,11);
fillellipse(xnewcenter,ynewcenter,radius,radius);
setcolor(11);
if(xnewcenter-radius<0)
ballspeedx=-ballspeedx;
if(xnewcenter+radius>640)
ballspeedx=-ballspeedx;
if(ynewcenter-radius<0)
ballspeedy=-ballspeedy;
if(ynewcenter+radius>340)
{
if(xnewcenter>x1 && xnewcenter<x2)
{
ballspeedy=-ballspeedy;
srand((int)time(&t));
ballspeedx=random(3);
if(random(3)==1)
ballspeedx=-ballspeedx;
score++;
}
else
{
cleardevice();
outtextxy(300,150,"Game Over");
sprintf(pit,"Score %d",score);
outtextxy(300,170,"Press Q to Exit");
getch();
}
return;
}
delay(10);
}
void bat(char sym)
{
switch(sym)
{
case 'z' : if(x1>0)
{
x1old=x1;
x2old=x2;
x1=x1-5;
x2=x2-5;
delay(10);
setfillstyle(1,0);
bar(x1old,y1,x2old,y2);
setfillstyle(1,13);
bar(x1,y1,x2,y2);
}
if(x1==0)
{
delay(10);
setfillstyle(1,13);
bar(x1,y1,x2,y2);
}
break;
case 'x' : if(x2<640)
{
x1old=x1;
x2old=x2;
x1=x1+5;
x2=x2+5;
delay(10);
setfillstyle(1,0);
bar(x1old,y1,x2old,y2);
setfillstyle(1,13);
bar(x1,y1,x2,y2);
}
if(x2==640)
{
delay(10);
setfillstyle(1,13);
bar(x1,y1,x2,y2);
}
break;
}
}
void play()
{
while(inportb(0x60)!=16)
{
if(inportb(0x60)==44) bat('z');
if(inportb(0x60)==45) bat('x');
ball();
}
}
void initialize()
{
time_t t;
x1=rand();
x1=320;
y1=x1+30;
x2=x1+80;
y2=y1+40;
srand((int)time(&t));
xnewcenter=random(200);
ynewcenter=random(200);
ballspeedx=2;
srand((int)time(&t));
ballspeedy=random(3);
if(random(3)==0)
{
ballspeedx=-ballspeedx;
ballspeedy=-ballspeedy;
}
radius=20;
play();
}
void main()
{
int gd=0,gm;
initgraph(&gd,&gm,"C:\\turboc3\\bgi");
outtextxy(90,150,"Instractions:");
outtextxy(90,170,"Press Q to Exit");
outtextxy(90,190,"Press Z, X to move the bar");
outtextxy(90,210,"Press any key to continue.");
getch();
cleardevice();
initialize();
getch();
}
OUTPUT:
B.3 Observations and learning:
Ans-
1.outtextxy() function is used to display text or string at a specified point (x,y) on the
screen.
2.inport us used to get hold of specific keyboard key
3.random function is used for the random movement of the ball.

B.4 Conclusion:
Ans- Thus, we conclude that we have successfully implemented animation of Ping
Pong Game by which can play the game. We have used various functions of graphics in
this program and thus familiarized with the concepts of graphics in C.

B.5 Question of Curiosity

Q.1] write another program for animation.


Ans- Animation using increasing circles filled with different colors and patterns.
Program:
#include<graphics.h>
#include<conio.h>
void main()
{
intgd=DETECT, gm, i, x, y;
initgraph(&gd, &gm, "C:\\TC\\BGI");
x=getmaxx()/3;
y=getmaxx()/3;
setbkcolor(WHITE);
setcolor(BLUE);
for (i=1; i<=8; i++)
{
setfillstyle(i,i);
delay (20);
circle (x, y, i*20);
floodfill (x-2+i*20, y, BLUE);
}
getch();
closegraph();
}

You might also like