You are on page 1of 64

COMPUTER GRAPHICS &

MULTIMEDIA LAB (ETCS-257)

Name: NIKHIL MATHUR


Roll No.: 05214802719
GROUP : 3C2

EXPERIMENT 1
Aim-:Study and prepare list of graphic functions.

Theory-: Different Types of Graphics Functions:

• Line Function

Line Function is used to draw a line from a point(x1,y1) to


point(x2,y2) i.e. (x1,y1) and (x2,y2) are end points of the line. The
code given below draws a Line.

Syntax: line(int x1, int y1, int x2, int y2);

• Rectangle Function

Rectangle Function is used to draw a rectangle. Coordinates of


left top and right bottom corner are required to draw the
rectangle. The code given below draws a Rectangle.

Syntax: rectangle(int left, int top, int right, int bottom);

• Circle Function

Circle Function is used to draw a circle with center (x,y) and third
parameter specifies the radius of the circle. The code given
below draws a Circle.

Syntax: circle(int x, int y, int radius);

• Ellipse Function

Ellipse Function is used to draw an ellipse (x,y) are coordinates


of center of the ellipse, stangle is the starting angle, endangle is
the ending angle, and fifth and sixth parameters specifies the X
and Y radius of the ellipse. To draw a complete ellipse strangles
and end angle should be 0 and 360 respectively. The code
given below draws an Ellipse.

Syntax:
void ellipse(int x, int y, int stangle, int endangle, int xradius, int
yradius);

• Initgraph Function

Initgraph Function initializes the graphics system by loading a


graphics driver from disk (or validating a registered driver), and
putting the system into graphics mode.

Syntax: void initgraph(int *graphdriver, int *graphmode,


char *pathtodriver);

• Closegraph Function

Closegraph Function closes the graphics mode, deallocates all


memory allocated by graphics system and restores the screen
to the mode it was in before you called initgraph.

Syntax: void closegraph();

• Getpixel Function

Getpixel Function returns the color of the pixel present at the


location (x,y).

Syntax: int getpixel(int x, int y);


• Setcolor Function

In Turbo Graphics each color is assigned a number. Total 16


colors are available. Strictly speaking number of available colors
depends on current graphics mode and driver.

Syntax: void setcolor(int color);

• Getcolor Function

Getcolor Function returns the current drawing color.

Syntax: int getcolor();

• Getbkcolor Function

Getbkcolor Function returns the current background-color.

Syntax: int getbkcolor();

EXPERIMENT 1 (B)

1.
Aim : To draw a hut using graphics header file in C/C++.

Algorithm : (To draw a hut)

• Open Turbo 3.2 C/C++.

• “ #include<graphics.h>, #include<conio.h>,
Declare header files ex-
#include<stdio.h>

• Declare int main.

• “ int gd =DETECT, gm;


Declare variables ex- ” .

• “ (position of BGI folder)


Use initgraph (&gd, &gm, ” .

• Draw three rectangles, these rectangles will represent the door, front
and side of the house.

• Draw two parallel but slanting lines at the top ends of rectangle
representing the side wall.

• Now join the tops of these slanting lines by a straight line.

• Complete the side by making another slanting line.

• Hut is ready.Close the program by using the closegraph () function.

Program-:

#include<graphics.h>

#include<conio.h>
int main(){

int gd = DETECT, gm;

initgraph(&gd, &gm, "X:\\TC\\BGI");

/* Draw Hut */

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

getch();

closegraph();

return 0;

Output-:
2.

Aim : To draw a car using graphics header file in C/C++.

Algorithm : (To draw a car)

• Open Turbo 3.2 C/C++.

• “ #include<graphics.h>, #include<conio.h>,
Declare header files ex-
#include<stdio.h>

• Declare int main.

• “ int gd =DETECT, gm;


Declare variables ex- ” .

• “ (position of BGI folder)


Use initgraph (&gd, &gm, ” .

• To build body draw to parallel lines at a distance of 50 units from each


other top line must be shorter than the bottom line.

• Draw another two slanting lines at the end of these line


• Now draw to perpendicular lines to the bigger parallel line.

• At the bottom make the wheels by providing equal radius.

• Connect the bottom line and the wheels to complete the car.

• End the program by using the closegraph () function.

Program-:

#include <graphics.h>

#include <stdio.h>

int main ()

int gd = DETECT, gm;

initgraph (&gd, &gm, "C:\\TURBOC3\\BGI");

// body of car

Line (0 , 300, 210 , 300);

Line (50 , 300, 75 , 270);

Line (75 , 270, 150 , 270);

Line (150, 270, 165 , 300);

Line (0 , 300, 0 , 330);

Line (210 , 300, 210 , 330);


// For left wheel of car

circle(65 , 330, 15);

circle(65 , 330, 2);

// For right wheel of car

circle(145 , 330, 15);

circle(145 , 330, 2);

getch();

closegraph();

return 0;

Output-:

< VIVA QUESTIONS> :


Q1) What is the use of initgraph() and closegraph() functions ?

Ans1)Initgraph(): initializes the graphics system by loading a graphics driver


from disk, and putting the system into graphics mode.

Closegraph(): function is used to reset back to text mode screen or in other


words it used to exit from the graphics screen.

Q2) Why do we need to use closegraph() function after getch() ?

Ans2) Closegraph() is called after getch() since screen should not clear until


user hits a key.

Q3) Which parameters are used to find the resolution of the screen?

Ans3) We can find out desktop resolution of a computer from command


prompt using wmic command. The below command prints desktop
resolution for all the available monitors on the system.

wmic desktopmonitor get screenheight, screenwidth

Q4) How is putpixel() different than getpixel() ?


Ans4) Function getpixel() returns the color of pixel present at point(x, y).
Function putpixel() plots a pixel at a point(x, y) of the specified color.

Q5) Explain various other graphic functions?

Ans5)

Line Function

Line Function is used to draw a line from a point(x1,y1) to point(x2,y2) i.e.


(x1,y1) and (x2,y2) are end points of the line. The code given below draws a
Line.

Syntax: line(int x1, int y1, int x2, int y2);

Rectangle Function

Rectangle Function is used to draw a rectangle. Coordinates of left top and


right bottom corner are required to draw the rectangle. The code given
below draws a Rectangle.

Syntax: rectangle(int left, int top, int right, int bottom);


Circle Function

Circle Function is used to draw a circle with center (x,y) and third parameter
specifies the radius of the circle. The code given below draws a Circle.

Syntax: circle(int x, int y, int radius);


EXPERIMENT - 2

AIM: Program for DDA Line Drawing Algorithm in C

ALGORITHM:

#include<graphics.h>
#include<stdio.h>
#include<math.h>
int main()
{
int gd,gm,x,y,pixel,x1,x2,y1,y2,dx,dy;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C://TurboC3//BGI");

printf("Enter the value of x1 : ");


scanf("%d",&x1);
printf("Enter the value of y1 : ");
scanf("%d",&y1);
printf("Enter the value of x2 : ");
scanf("%d",&x2);
printf("Enter the value of y2 : ");
scanf("%d",&y2);

dx=abs(x1-x2);
dy=abs(y1-y2);

if(dx>=dy)
pixel=dx;
else
pixel=dy;
dx=dx/pixel;
dy=dy/pixel;

x=x1;
y=y1;

while(pixel--)
{
putpixel(x,y,WHITE);
x=x+dx;
y=y+dy;
delay(100);
}

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

OUTPUT:
EXPERIMENT 3.

Aim-: Write a C program to draw a line using Bresenham’s algorithm.

ALGORITHM AND CODE :

#include<stdio.h>
#include<graphics.h>
void drawline(int x0, int y0, int x1, int y1)
{
int dx, dy, p, x, y;
dx=x1-x0;
dy=y1-y0;
x=x0;
y=y0;
p=2*dy-dx;
while(x<x1)
{
if(p>=0)
{
putpixel(x,y,7);
y=y+1;
p=p+2*dy-2*dx;
}
else
{
putpixel(x,y,7);
p=p+2*dy;}
x=x+1;
}
}
int main()
{
int gdriver=DETECT, gmode, error, x0, y0, x1, y1;
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
printf("Enter co-ordinates of first point: ");
scanf("%d%d", &x0, &y0);
printf("Enter co-ordinates of second point: ");
scanf("%d%d", &x1, &y1);
drawline(x0, y0, x1, y1);
return 0;
}

OUTPUT :
EXPERIMENT 4.

Aim-: Write a C program to draw a circle using mid-point algorithm.

ALGORITHM AND CODE :

#include<stdio.h>

#include<graphics.h>

void midPointCircleDraw(int x_centre, int y_centre, int r)

int x = r, y = 0;
printf("(%d, %d) ", x + x_centre, y + y_centre);

if (r > 0)

printf("(%d, %d) ", x + x_centre, -y + y_centre);

printf("(%d, %d) ", y + x_centre, x + y_centre);

printf("(%d, %d)\n", -y + x_centre, x + y_centre);

int P = 1 - r;

while (x > y)

y++;

if (P <= 0)

P = P + 2*y + 1;

else

x--;

P = P + 2*y - 2*x + 1;

if (x < y)

break;
printf("(%d, %d) ", x + x_centre, y + y_centre);

printf("(%d, %d) ", -x + x_centre, y + y_centre);

printf("(%d, %d) ", x + x_centre, -y + y_centre);

printf("(%d, %d)\n", -x + x_centre, -y + y_centre);

if (x != y)

printf("(%d, %d) ", y + x_centre, x + y_centre);

printf("(%d, %d) ", -y + x_centre, x + y_centre);

printf("(%d, %d) ", y + x_centre, -x + y_centre);

printf("(%d, %d)\n", -y + x_centre, -x + y_centre);

int main()

midPointCircleDraw(0, 0, 3);

return 0;

OUTPUT :
EXPERIMENT 5.

Aim-: Implementation of Circle drawing algorithms using Bresenham's


Algorithm.

ALGORITHM AND CODE :

#include <stdio.h>

#include <dos.h>

#include <graphics.h>

void drawCircle(int xc, int yc, int x, int y)

putpixel(xc+x, yc+y, RED);

putpixel(xc-x, yc+y, RED);

putpixel(xc+x, yc-y, RED);

putpixel(xc-x, yc-y, RED);

putpixel(xc+y, yc+x, RED);

putpixel(xc-y, yc+x, RED);

putpixel(xc+y, yc-x, RED);

putpixel(xc-y, yc-x, RED);

}
void circleBres(int xc, int yc, int r)

int x = 0, y = r;

int d = 3 - 2 * r;

drawCircle(xc, yc, x, y);

while (y >= x)

x++;

if (d > 0)

y--;

d = d + 4 * (x - y) + 10;

else

d = d + 4 * x + 6;

drawCircle(xc, yc, x, y);

delay(50);

}
int main()

int xc = 50, yc = 50, r2 = 30;

int gd = DETECT, gm;

initgraph(&gd, &gm, "");

circleBres(xc, yc, r);

return 0;

OUTPUT :
EXPERIMENT 6.

Aim-: Write C Programs for the implementation of 2D and 3D


transformations.

ALGORITHM AND CODE :

1. 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,YX,Y to get the new coordinate X′,Y′X′,Y′.

From the above figure, you can write that −

X’ = X + tx

Y’ = Y + ty

The pair (tx, ty) is called the translation vector or shift vector. The above
equations can also be represented using the column vectors.

P=[X][Y]P=[X][Y] p' = [X′][Y′][X′][Y′]T = [tx][ty][tx][ty]

We can write it as −

P’ = P + T

CODE:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>
void RectAngle(int x, int y, int Height, int Width);void Translate(int x, int y, int
Height, int Width);
void main() {
int gd = DETECT, gm;
int x, y, Height, Width;
initgraph(&gd, &gm, " ");
printf("Enter the First point for the Rectangle:");
scanf("%d%d", &x, &y);
printf("Enter the Height&Width for the Rectangle:");
scanf("%d%d", &Height, &Width);
RectAngle(x, y, Height, Width);
getch();
cleardevice();
Translate(x, y, Height, Width);
RectAngle(x, y, Height, Width);
getch();
}
void RectAngle(int x, int y, int Height, int Width) {
line(x, y, x + Width, y);
line(x, y, x, y + Height);
line(x + Width, y, x + Width, y + Height);
line(x, y + Height, x + Width, y + Height);
}
void Translate(int x, int y, int Height, int Width) {
int Newx, Newy, a, b;
printf("Enter the Transaction coordinates");
scanf("%d%d", &Newx, &Newy);
cleardevice();
a = x + Newx;
b = y + Newy;
RectAngle(a, b, Height, Width);
}

OUTPUT:
2. ROTATION.

In order to rotate an object we need to rotate each vertex of the figure


individually.
On rotating a point P(x, y) by an angle A about the origin we get a point P'(x’,
y’). The values of x’ and y’ can be calculated as follows:-

We know that,
x = rcosB, y = rsinB

x’ = rcos(A+B) = r(cosAcosB – sinAsinB)


= rcosBcosA – rsinBsinA = xcosA – ysinA
y’ = rsin(A+B) = r(sinAcosB + cosAsinB) = rcosBsinA + rsinBcosA = xsinA +
ycosA

Rotational Matrix Equation:-

CODE:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>
void TriAngle(int x1, int y1, int x2, int y2, int x3, int y3);void Rotate(int x1, int y1,
int x2, int y2, int x3, int y3);
void main() {
int gd = DETECT, gm;
int x1, y1, x2, y2, x3, y3;
initgraph(&gd, &gm, " ");

printf("Enter the 1st point for the triangle:");


scanf("%d%d", &x1, &y1);
printf("Enter the 2nd point for the triangle:");
scanf("%d%d", &x2, &y2);
printf("Enter the 3rd point for the triangle:");
scanf("%d%d", &x3, &y3);
TriAngle(x1, y1, x2, y2, x3, y3);
getch();
cleardevice();
Rotate(x1, y1, x2, y2, x3, y3);
setcolor(1);
TriAngle(x1, y1, x2, y2, x3, y3);
getch();
}
void TriAngle(int x1, int y1, int x2, int y2, int x3, int y3) {
line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
line(x3, y3, x1, y1);
}
void Rotate(int x1, int y1, int x2, int y2, int x3, int y3) {
int x, y, a1, b1, a2, b2, a3, b3, p = x2, q = y2;
float Angle;
printf("Enter the angle for rotation:");
scanf("%f", &Angle);
cleardevice();
Angle = (Angle * 3.14) / 180;
a1 = p + (x1 - p) * cos(Angle)-(y1 - q) * sin(Angle);
b1 = q + (x1 - p) * sin(Angle)+(y1 - q) * cos(Angle);
a2 = p + (x2 - p) * cos(Angle)-(y2 - q) * sin(Angle);
b2 = q + (x2 - p) * sin(Angle)+(y2 - q) * cos(Angle);
a3 = p + (x3 - p) * cos(Angle)-(y3 - q) * sin(Angle);
b3 = q + (x3 - p) * sin(Angle)+(y3 - q) * cos(Angle);
printf("Rotate");
TriAngle(a1, b1, a2, b2, a3, b3);
}

OUTPUT:
EXPERIMENT 7.

Aim-: Write C Programs for the implementation of Bezier Curve.

ALGORITHM AND CODE :

Constructive Bezier Curve Algorithm

Consider the n+1 points P0,…,Pn and connect the points into a polyline we will
denote hereafter as the control polygon.

Given points Pi, i = 0,...,n, our goal is to determine a curve g (t), for all values t
Î [0,1]. The idea is demonstrated below:

Basic Algorithm
The objective here is to find points in the middle of two nearby points and
iterate this until we have no more iterations. The new values of points will give
us the curve. The famous Bezier equation is the exact formulation of this idea.
Here is the algorithm:

Step 1: Select a value t Î [0,1]. This value remains constant for the rest of the
steps.

Step 2: Set Pi[0] (t) = Pi, for i = 0,...,n.

Step 3: For j= 0,...,n, set for i = j,...,n.

Step 4: g (t) = Pn[n] (t)

Special & General Cases

Now, I will give formulas for common, special cases that can be helpful in
certain applications. The code of the article does not demonstrate any of
them, but it uses the generalized formula. So, let me start with the
generalized formula:

For the sake of simplicity and convention used in this article and code, it is
better to represent this formula as:

What this equation tells us is nothing but the formulation of the above
algorithm (the mid-point iterations). It is very important in the sense that a
whole algorithm could be summarized into a formula and a straightforward
implementation would yield correct results. Here, n denotes the number of
points and P denotes the points themselves. The factorial coefficients of the
points are simply called the Bernstein basis functions, because of the name of
the founder.
Here are the special cases:

Linear Bezier:

Quadratic Bezier:

Cubic Bezier:

CODE:

#include <stdio.h>

#include <graphics.h>

#include <math.h>

int x[6]={200,100,200,250,150,210};

int y[6]={200,150,75,100,250,210};

void bezier ()

int i;

double t,xt,yt;

for (t = 0.0; t < 1.0; t += 0.0007)

xt = pow(1-t,3)*x[0]+3*t*pow(1-t,2)*x[1]+3*pow(t,2)*(1-t)*x[2]+pow(t,3)*x[3];

yt = pow(1-t,3)*y[0]+3*t*pow(1-t,2)*y[1]+3*pow(t,2)*(1-t)*y[2]+pow(t,3)*y[3];
putpixel (xt, yt,WHITE);

for (i=0; i<6; i++)

putpixel (x[i], y[i], YELLOW);

getch();

closegraph();

void main()

int gd = DETECT, gm;

initgraph (&gd, &gm, "C://TURBOC3//BGI");

bezier ();

OUTPUT:
EXPERIMENT 8.

Aim-: Write a C program to demonstrate Cohen-Sutherland line clipping


algorithm.

ALGORITHM AND CODE :

Nine regions are created, eight "outside" regions and one


"inside" region.

For a given line extreme point (x, y), we can quickly


find its region's four bit code. Four bit code can
be computed by comparing x and y with four values
(x_min, x_max, y_min and y_max).

If x is less than x_min then bit number 1 is set.


If x is greater than x_max then bit number 2 is set.
If y is less than y_min then bit number 3 is set.
If y is greater than y_max then bit number 4 is set
There are three possible cases for any given line.

1. Completely inside the given rectangle : Bitwise OR of region of two end


points of line is 0 (Both points are inside the rectangle)
2. Completely outside the given rectangle : Both endpoints share at least
one outside region which implies that the line does not cross the visible
region. (bitwise AND of endpoints != 0).
3. Partially inside the window : Both endpoints are in different regions. In
this case, the algorithm finds one of the two points that is outside the
rectangular region. The intersection of the line from outside point and
rectangular window becomes new corner point and the algorithm
repeats
CODE:

#include<stdio.h>

#include<stdlib.h>

#include<math.h>

#include<graphics.h>

#include<dos.h>

typedef struct coord

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


void main()

int gd=DETECT,v,gm;

PT p1,p2,p3,p4,ptemp;

printf("\nEnter x1 and y1\n");

scanf("%d %d",&p1.x,&p1.y);

printf("\nEnter x2 and y2\n");

scanf("%d %d",&p2.x,&p2.y);

initgraph(&gd,&gm,"c:\\turboc3\\bgi");

drawwindow();

delay(500);

drawline(p1,p2);

delay(500);

cleardevice();

delay(500);

p1=setcode(p1);

p2=setcode(p2);

v=visibility(p1,p2);

delay(500);
switch(v)

case 0: drawwindow();

delay(500);

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) //for setting the 4 bit code

PT ptemp;

if(p.y<100)

ptemp.code[0]='1'; //Top

else

ptemp.code[0]='0';

if(p.y>350)

ptemp.code[1]='1'; //Bottom

else

ptemp.code[1]='0';
if(p.x>450)

ptemp.code[2]='1'; //Right

else

ptemp.code[2]='0';

if(p.x<150)

ptemp.code[3]='1'; //Left

else

ptemp.code[3]='0';

ptemp.x=p.x;

ptemp.y=p.y;

return(ptemp);

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

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

}
OUTPUT:

BEFORE CLIPPING:
AFTER CLIPPING:
EXPERIMENT 9.

AIM-: Write a C program to demonstrate sunrise using graphics.

CODE:

#include<graphics.h>

#include<conio.h>

#include<stdio.h>

void main()

int gd=DETECT,gm;

int i,j,k,t,q;

float x,y;

initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");

setcolor(2);
rectangle(0,0,getmaxx(),getmaxy());

setcolor(2);

i=0;

for(t=0;t<getmaxx();t+=120)

line(t,250,t+60,170);

line(t+60,170,t+120,250);

line(0,400,getmaxx(),350);

setfillstyle(11,CYAN);

floodfill(2,420,2);

setfillstyle(4,LIGHTGREEN);

floodfill(1,300,2);

i=0;

while(i!=150)

{
setcolor(BLACK);

setfillstyle(SOLID_FILL,BLACK);

fillellipse(k,j,30,30);

setfillstyle(SOLID_FILL,LIGHTRED);

fillellipse(170+i,235-i,30,30);

j=235-i;

k=170+i;

i++;

setcolor(2);

for(t=0;t<getmaxx();t+=120)

line(t,250,t+60,170);

line(t+60,170,t+120,250);

setfillstyle(1,GREEN);

floodfill(202,200,GREEN);

delay(25);

}
for(i=36;i<80;i++)

for(j=0;j<=360;j+=20)

x=319+i*cos(((float)j*3.14)/180);

y=86+i*sin(((float)j*3.14)/180);

putpixel(x,y,LIGHTRED);

delay(1);

getch();

}
OUTPUT:
EXPERIMENT 10.

Aim-: Write a C program to demonstrate a fish tank with moving fishes using

graphics.

CODE:

#include <stdio.h>

#include <conio.h>

#include <graphics.h>

#include <dos.h>

void bubbles(int i,int y)

if(y<100)

y=y+200;

if(y>200)
{

circle(100,y+110-i,5);

else

circle(100,y+110-i,5);

void main(){

int i=0;

int y=200;

int gd= DETECT, gm;

initgraph(&gd, &gm,"C://TURBOC3//BGI");

for(i=0;i<300;i++)

line(150+i,250,190+i,290);
line(150+i,250,90+i,310);

line(90+i,270,150+i,330);

line(190+i,290,150+i,330);

line(90+i,310,90+i,270);

circle(170+i,290,3);

arc(400-i,150,50,320,30);

line(420-i,128,450-i,160);

line(420-i,173,450-i,130);

line(450-i,160,450-i,130);

circle(380-i,150,3);

arc(380-i,150,270,90,20);

line(50,300,80,300);

line(80,300,80,370);

line(50,370,80,370);

line(80,305,90,305);

line(80,310,90,310);
line(90,305,90,310);

bubbles(i,y);

y--;

line(50,10,550,10);

line(50,10,10,100);

line(550,10,590,100);

line(10,100,590,100);

line(50,100,50,400);

line(550,100,550,400);

line(50,400,550,400);

ellipse(100,390,0,360,20,10);

ellipse(125,370,0,360,25,10);

ellipse(150,390,0,360,25,10);

ellipse(170,375,0,360,15,5);

circle(180,390,10);
ellipse(200,380,0,360,10,15);

circle(220,390,10);

ellipse(250,380,0,360,20,15);

ellipse(290,390,0,360,20,10);

ellipse(315,370,0,360,25,10);

ellipse(340,390,0,360,25,10);

ellipse(360,375,0,360,15,5);

circle(370,390,10);

ellipse(390,380,0,360,10,15);

circle(410,390,10);

ellipse(440,380,0,360,20,15);

ellipse(480,390,0,360,20,10);

ellipse(505,370,0,360,25,10);

ellipse(530,390,0,360,21,10);

delay(50);

cleardevice();

getch();
closegraph();

OUTPUT:
EXPERIMENT 11.

AIM : Create a Bouncing Ball using Key frame animation and Path animation.

STEPS TO FOLLOW :

Getting to Know Blender .

If you would like to know as much information as possible visit the Blender

documentation.

Regarding almost all of these movements: clicking the left mouse button ‘
( lmb’) will

confirm the action and right mouse button (‘rmb’) will undo the action.

If you would like to know the specific instructions for each action in a step, hover

over the text and the specific instructions will be displayed.

There are 4 main sections within the default blender project we’ll be working with.
Blue Border: This is the 3D viewport where you can see your creations come to life. It

is where you adjust visual aspects of an experiment or model.

Yellow Border: This is the Outliner. It gives a list of all the objects within the scene

with which we are working.

Red Border: This is the Timeline. As the name suggests, it is where we will control

time within animations or time sensitive demonstrations.

Purple Border: This is Properties Panel. This is where you adjust all the properties of

objects, physics world, or the project as a whole.

There are 3 actions you need to know to be able to navigate the 3D viewport

effectively.

Press Mouse Wheel: By pressing the mouse wheel you can grab and rotate the

scene.

Press Mouse Wheel and Shift: By holding these two you can grab the scene and

translate it laterally.

Scroll Mouse Wheel As you would expect this zooms the 3D view in or out

towards the center of the 3D viewport.

You select an object with a ‘Right Click’ on it in the 3D viewport. Also, you can click

on its name in the Outliner Panel.

To select multiple object you can ‘Shift’+’Right Click’ on another object. There

are other methods to select multiple objects as well.


You can press‘c’to reveal a circle that you can use with‘click’+’drag’to select

objects or ‘mousewheel click’+’drag’ to deselect. The circle can be resized with

the mousewheel.

Also you can press ‘b’ to box select objects. It works similarly to circle select

with ‘click’+’drag’ selecting and ‘mousewheel click’+’drag’ deselecting.

Lastly, you can press ‘a’ to select or deselect all of the objects within the scene.

You can move a selected object by manipulating the widget that surrounds it like I

do in the video.

Alternatively you can translate, rotate, and scale the object by pressing ‘g’, ‘r’,

and ‘s’ respectively.

If you would like to restrict this movement to a certain axis, you can press

either ‘x’, ‘y’, or ‘z’.

Creating an Object .
You can delete and add objects within Blender

You delete an object by selecting it and pressing ‘x’ or by

selecting Object/Delete in the menus at the bottom of the 3D viewport.

You can then add an object by pressing ‘Shift’+’a’ or by selecting Add in the

menus at the bottom of the 3D viewport.

Now we’ll create the objects for our scene.

First I scale up the default cube in the scene.

I then scaled it down along the z-axis to create a platform for our ball to bounce on.

I added a ball (UV Sphere) and moved it up along the z-axis so it appears above our

plane.

(Optional) While the ball was selected I clicked on the tools panel in the left of the 3D

viewport and selected Shading/Smooth so that when we render our scene it will look

nicer.

Adding Physics .

I selected the cube and navigated to the left panel within the 3D viewport.

I click on the Physics panel and then click on Add Passive.


Now our cube should be surrounded with a green border rather than the default

orange denoting that it contains physics properties.

I do something similar to the sphere.

This time instead of selecting Add passive I select Add active to tell Blender that this

object will be moving.

Playing Animation .

To play the animation we simply navigate down to the bottom of the screen and

click the play button

This will set the Timeline in motion and we can see whatever animations we’ve

created.

When we play this animation we see that the ball falls to hit the ground and then

sticks to it. This is not what we want so we need to play with some setting to get the

object to bounce.

We navigate over to the properties panel with our ball selected.

We click the physics tab and look to find the bounciness value selector. We will

adjust this to be 1 so the ball is very bouncy.


If we play the animation again, the ball still does not bounce! This is because the

platform is set to absorb all of the energy.

We adjust the settings of the platform similarly to the ball accept we set

the bounciness setting to 0.5 this time so the platform still absorbs some energy.

This still isn’t very bouncy so we adjust the setting to 0.9 to complete the simulation.

If you play the simulation now you can see the ball should bounce as you would

expect it to.

You are encouraged to play around with some of these values to see what they do.

Often it is explained with the name of the value.

Rendering .

If we rendered now the result wouldn’t look too great. So we need to adjust some

things to make the render turn out how we want.


First, we should move our light source and possibly create some new light sources to

be sure the object is properly lit. I used a Sun type light source but you could use

whatever you want.

Then, we adjust the location of the camera to be sure that it is capturing what we

want it to capture. We do this by pressing ‘0’ on the numpad.

If you don’t have a numpad, you can go to View/Cameras/Active Camera to

change your view to that of the camera.

Lastly, we open the right panel in the 3D viewport and check Lock Camera to View.

This allows the camera to move when we adjust our view to make sure it shows what

we want.

Make sure you have your rendering settings the same as what I am showing in the

picture. I will go over the key settings to change.

1. Adjust your resolution to be sure that it is 1920×1080 to achieve full HD.

(Highlighted in yellow)

2. Be sure that the percentage bar under the resulution values it at 100% to achieve a

full resolution render. (Highlighted in green)

3. Change the export type to AVI JPEG in the output panel. (Highlighted in blue)
4. Where I have a field that says /tmp\ adjust your destination folder to somewhere

you can access. This is where the renders will be saved. (Highlighted in purple)

Lastly, go to the Render menu in the top left and select Render Animation.

Alternatively you can press ‘Ctrl’+’F12’.

After Blender is done rendering, you should have a video saved in the folder you

specified!

RESULT :

You might also like