Computer Graphics & Multimedia Lab (Etcs-257) : Experiment 1
Computer Graphics & Multimedia Lab (Etcs-257) : Experiment 1
Computer Graphics & Multimedia Lab (Etcs-257) : Experiment 1
EXPERIMENT 1
Aim-:Study and prepare list of graphic functions.
• Line Function
• Rectangle Function
• 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.
• Ellipse Function
Syntax:
void ellipse(int x, int y, int stangle, int endangle, int xradius, int
yradius);
• Initgraph Function
• Closegraph Function
• Getpixel Function
• Getcolor Function
• Getbkcolor Function
EXPERIMENT 1 (B)
1.
Aim : To draw a hut using graphics header file in C/C++.
• “ #include<graphics.h>, #include<conio.h>,
Declare header files ex-
#include<stdio.h>
• 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.
Program-:
#include<graphics.h>
#include<conio.h>
int main(){
/* 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.
• “ #include<graphics.h>, #include<conio.h>,
Declare header files ex-
#include<stdio.h>
• Connect the bottom line and the wheels to complete the car.
Program-:
#include <graphics.h>
#include <stdio.h>
int main ()
// body of car
getch();
closegraph();
return 0;
Output-:
Q3) Which parameters are used to find the resolution of the screen?
Ans5)
Line Function
Rectangle 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.
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");
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.
#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.
#include<stdio.h>
#include<graphics.h>
int x = r, y = 0;
printf("(%d, %d) ", x + x_centre, y + y_centre);
if (r > 0)
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);
if (x != y)
int main()
midPointCircleDraw(0, 0, 3);
return 0;
OUTPUT :
EXPERIMENT 5.
#include <stdio.h>
#include <dos.h>
#include <graphics.h>
}
void circleBres(int xc, int yc, int r)
int x = 0, y = r;
int d = 3 - 2 * r;
while (y >= x)
x++;
if (d > 0)
y--;
d = d + 4 * (x - y) + 10;
else
d = d + 4 * x + 6;
delay(50);
}
int main()
return 0;
OUTPUT :
EXPERIMENT 6.
1. TRANSLATION.
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.
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.
We know that,
x = rcosB, y = rsinB
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, " ");
OUTPUT:
EXPERIMENT 7.
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.
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;
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);
getch();
closegraph();
void main()
bezier ();
OUTPUT:
EXPERIMENT 8.
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<graphics.h>
#include<dos.h>
int x,y;
char code[4];
}PT;
void drawwindow();
PT setcode(PT p);
int gd=DETECT,v,gm;
PT p1,p2,p3,p4,ptemp;
scanf("%d %d",&p1.x,&p1.y);
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);
line(p1.x,p1.y,p2.x,p2.y);
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 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++)
flag='0';
if(flag==0)
return(1);
return(2);
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];
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.
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>
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;
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 :
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
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
Yellow Border: This is the Outliner. It gives a list of all the objects within the scene
Red Border: This is the Timeline. As the name suggests, it is where we will control
Purple Border: This is Properties Panel. This is where you adjust all the properties of
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
You select an object with a ‘Right Click’ on it in the 3D viewport. Also, you can click
To select multiple object you can ‘Shift’+’Right Click’ on another object. There
the mousewheel.
Also you can press ‘b’ to box select objects. It works similarly to circle select
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’,
If you would like to restrict this movement to a certain axis, you can press
Creating an Object .
You can delete and add objects within Blender
You can then add an object by pressing ‘Shift’+’a’ or by selecting Add in the
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.
This time instead of selecting Add passive I select Add active to tell Blender that this
Playing Animation .
To play the animation we simply navigate down to the bottom of the screen and
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 click the physics tab and look to find the bounciness value selector. We will
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.
Rendering .
If we rendered now the result wouldn’t look too great. So we need to adjust some
be sure the object is properly lit. I used a Sun type light source but you could use
Then, we adjust the location of the camera to be sure that it is capturing what we
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
(Highlighted in yellow)
2. Be sure that the percentage bar under the resulution values it at 100% to achieve a
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.
After Blender is done rendering, you should have a video saved in the folder you
specified!
RESULT :