CGMM Lab File
CGMM Lab File
DepartmentofComputerScience&FacultyofEngineering
MEDI-CAPSUNIVERSITY
INDORE-453331
ComputerGraphicsandMultimedia
CourseCode:-CS3C024
LabManual/File
BY
RanuSharma:EN19CS305036
Index-
S.No. Practical
1.
ABriefStudyofVariousTypesofInput,Output&DisplayDevices.
2.
ProgramtoImplementaLineUsingSlopeInterceptFormula.
3.
ProgramtoImplementLineUsingDDAAlgorithm
4.
Programtoimplementlineusingbresenham’salgorithm
5.
Problem statement: write a program to implement midpoint circle
algorithm
6.
ProgramtoImplementTranslationofaLineandTriangle
7.
ProgramtoImplementRotationofaLineandTriangle
8.
ProgramtoImplementScalingTransformation.
9.
Programtoimplement3drotationaboutanarbitraryaxis
10.
Programtoimplementfloodfillalgorithm.
11.
Programtoimplementfor3Dtranslation.
12.
Program to implement basics of graphics like line,
circle,
arc,
ellipse
andrectangle.
13. Experiment
14.
Writeaprogramtodrawabouncingball.
EXPERIMENT-1
ABriefStudyofVariousTypesofInput,Output&DisplayDevices.
The
devices
which
are
used
to
input
the
data
and
the
programs
in the computer
are
known
as
"Input Devices" or Input devices that can read data and convert them to a form that a
computer
can
use.
Output
Device
can
produce
the
final
product of
machine
processing
into
a
form
usable
by humans.
It
provides
man
to
machine
communication.
Some
of
the
I/O
devices
areexplainedbelow:
1. Keyboard : Keyboard is used in the input phase of a computer-based information
system. Keyboard is the most common input device used today. The data and
instructions
are
input
by
typing
on
the
keyboard.
The
message
typed on
the keyboard
reaches the memory unit of a computer. It’s connected to a computer via a cable.
Apart from alphabet and numeral keys, it has other function keys for performing
differentfunctions.
2. Mouse
: It’s
a pointing
device.
The
mouse
is
rolled
over
the
mouse pad,
which in
turn
controls
the
movement
of
the
cursor
on
the
screen.
We
can
click,
double
click
or
drag
the mouse. Most of the mouse’s have a ball beneath them, which rotates when the
mouse is moved. The ball has 2 wheels of
the
sides,
which
in
turn
mousse
with
the
movement
of
the ball. The sensor
notifies
the
speed
of
its movements
to the computer,
whichinturnmovesthecursor/pointeronthescreen.
3. Scanner : Scanners are used to enter information directly in to the computer’s
memory. This
device
works
like
a Xerox
machine.
The
scanner
converts
any
type
of
printed
or
written
information
including
photographs
into
digital
pulses,
which
can
be
manipulatedbythecomputer.
4. Track
Ball
: Track
ball
is
similar
to
the
upside-
down
design
of
the
mouse.
The
user
moves the
ball
directly,
while
the
device
itself
remains
stationary.
The
user
spins
the
ballinvariousdirectionstoaffectthescreenmovements.
5. Light Pen : This is an input device which is used to draw lines or figures on a
computer screen. It’s touched to the CRT screen where it can detect raster on the
screenasitpasses.
6. Optical Character Rader : It’s a device which detects alpha numeric characters
printed
or
written
on
a paper.
The
text
which
is
to
be
scanned
is
illuminated by
a low
frequency
light
source.
The
light
is
absorbed
by
the
dark
areas
but
reflected
from
the
brightareas.Thereflectedlightisreceivedbythephotocells.
7. Bar
Code
Reader
:
This
device
reads
barcodes
and
converts them
into
electric
pulses
to be processed by a computer. A bar
code
is
nothing
but
data
coded
in
the
form
of
lightanddarkbars.
8. Voice
Input
Systems
:
This
device
converts
spoken
words
to
M/C
language
form.
A
microphone is
used
to
convert
human
speech
into
electric
signals.
The
signal
pattern
is then transmitted to
a computer
when
it’s
compared
to
a dictionary
of
patterns
that
have
been
previously
placed
in
a storage
unit
of
the
computer.
When
a close
match is
found,thewordisrecognized.
9. Plotter : Plotter
is
an
O/P
device
that
is
used
to
produce
graphical
O/P
on
papers.
It
usessinglecolorormulticolorpenstodrawpicturesasblueprintetc.
10. Digital Camera : It converts graphics directly into digital form. It looks like an
ordinary
camera,
but
no film
is used
therein,
instead
a CCD
(changed
coupled
Divide)
Electronic
chip
is
used.
When
light
falls on
the
chip
through
the
lens,
it
converts
light
wavesintoelectricalwaves.
Displaydevices:
● CRT
● LCD
● LED
● RasterSD
● RandomSD
EXPERIMENT-2
ProgramtoImplementaLineUsingSlopeInterceptFormula.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
voidmain()
{
floatm,x1,y1,x2,y2;
intx,y;
intgdriver=DETECT,gmode,gerror;
clrscr();
printf("PROGRAMFORTHELINEINTERCEPT\n");
printf("Enterthevalueofx1");
scanf("%f",&x1);
printf("Enterthevalueofy1");
scanf("%f",&y1);
printf("Enterthevalueofx2");
scanf("%f",&x2);
printf("Enterthevalueofy2");
scanf("%f",&y2);
initgraph(&gdriver,&gmode,"c:\\turboc3\\bgi");
m=(y2-y1)/(x2-x1);
for(x=1;x<=x2;x++)
{
y=m*(x-x1)+y1;
putpixel(x,y,15);
delay(50);
}
getch();
closegraph();
}
OUTPUT
EXPERIMENT-3
ProgramtoImplementLineUsingDDAAlgorithm
#include<graphics.h>
#include<stdio.h>
#include<math.h>
#include<conio.h>
voidmain()
{
floatx,y,x1,y1,x2,y2,dx,dy,pixel;
inti,gd,gm;
printf("Enterthevalueofx1:");
scanf("%f",&x1);
printf("Enterthevalueofy1:");
scanf("%f",&y1);
printf("Enterthevalueofx2:");
scanf("%f",&x2);
printf("Enterthevalueofy2:");
scanf("%f",&y2);
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"");
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dx>=dy)
pixel=dx;
else
pixel=dy;
dx=dx/pixel;
dy=dy/pixel;
x=x1;
y=y1;
i=1;
while(i<=pixel)
{
putpixel(x,y,1);
x=x+dx;
y=y+dy;
i=i+1;
delay(100);
}
getch();
closegraph();
}
OUTPUT
EXPERIMENT-4
Programtoimplementlineusingbresenham’salgorithm
#include"stdio.h"
#include"conio.h"
#include"math.h"
#include"graphics.h"
#include"dos.h"
main()
{
intgd=DETECT,gm;
intxa,xb,ya,yb;
intdx,dy,x,y,xend,p;
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("EnterTheTwoLeftEndpoints(xa,ya):\n");
scanf("%d%d",&xa,&ya);
printf("EnterTheTwoRightEndpoints(xb,yb):\n");
scanf("%d%d",&xb,&yb);
dx=abs(xa-xb);
dy=abs(ya-yb);
p=2*dy-dx;
if(xa>xb)
{
x=xb;
y=yb;
xend=xa;
}
else
{
x=xa;
y=ya;
xend=xb;
}
putpixel(x,y,6);
while(x<xend)
{
x=x+1;
if(p<0)
{
p=p+2*dy;
}
else
{
y=y+1;
p=p+2*(dy-dx);
}
putpixel(x,y,6);
}
getch();
return(0);
}
OUTPUT
EXPERIMENT-5
Problemstatement:writeaprogramtoimplementmidpointcirclealgorithm
Code:
#include<stdlib.h>
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
intmidx,midy,I,gdriver=DETECT,
gmode=DETECT,j,x,y,radius,p,xc,yc,x1,y1;
voidplotpoints();
voidborder();
main()
{
initgraph(&gdriver,&gmode,“c:\\turboc3\\bgi”);
printf(“ENTERTHEVALUESOFX-CENTER“);
scanf(“%d”,&xc);
printf(“ENTERTHEVALUESOFY-CENTER“);
scanf(“%d”,&xc,&yc);
printf(“ENTERTHERADIUSOFTHECIRCLE“);
scanf(“%d”,&radius);
midx=getmaxx()/2;
midy=getmaxy()/2;
x=0;
y=radius;plotpoints();
p=1-radius;
while(x<y)
{
if(p<0)
{
x=x+1;
p=p+(2*x)+1;
}
else
{
x=x+1;y=y-1;
p=p+2*(x-y)+1;
plotpoints();
}
}
getch();
closegraph();
return0;
}
voidplotpoints()
{
putpixel(xc+x+midx,midy-yc+y,15);
putpixel(xc-x+midx,midy-yc+y,15);
putpixel(xc+x+midx,midy-yc-y,15);
putpixel(midx+xc-x,midy-yc-y,15);
putpixel(midx+xc+y,midy-yc+x,15);
putpixel(midx+xc-y,midy-yc+x,15);
putpixel(midx+xc+y,midy-yc-x,15);
putpixel(midx+xc-y,midy-yc-x,15);
}
OUTPUT
EXPERIMENT-6
ProgramtoImplementTranslationofaLineandTriangle
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>
#include<dos.h>
intx1,y1,x2,y2,x3,y3,mx,my;
voiddraw();
voidtri();
voidmain()
{
intgd=DETECT,gm;
intc;
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("Enterthe1stpointforthetriangle:");
scanf("%d%d",&x1,&y1);
printf("Enterthe2ndpointforthetriangle:");
scanf("%d%d",&x2,&y2);
printf("Enterthe3rdpointforthetriangle:");
scanf("%d%d",&x3,&y3);
cleardevice();
draw();
getch();
tri();
getch();
}
voiddraw()
{
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
}
voidtri()
{
intx,y,a1,a2,a3,b1,b2,b3;
printf("EntertheTransactioncoordinates");
scanf("%d%d",&x,&y);
cleardevice();
a1=x1+x;
b1=y1+y;
a2=x2+x;
b2=y2+y;
a3=x3+x;
b3=y3+y;
line(a1,b1,a2,b2);
line(a2,b2,a3,b3);
line(a3,b3,a1,b1);
}
OUTPUT
Experiment-7
ProgramtoImplementRotationofaLineandTriangle
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>
#include<dos.h>
voidTriAngle(intx1,inty1,intx2,inty2,intx3,inty3);
voidRotate(intx1,inty1,intx2,inty2,intx3,inty3);
voidmain()
{
intgd=DETECT,gm;
intx1,y1,x2,y2,x3,y3;
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("Enterthe1stpointforthetriangle:");
scanf("%d%d",&x1,&y1);
printf("Enterthe2ndpointforthetriangle:");
scanf("%d%d",&x2,&y2);
printf("Enterthe3rdpointforthetriangle:");
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();
}
voidTriAngle(intx1,inty1,intx2,inty2,intx3,inty3)
{
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
}
voidRotate(intx1,inty1,intx2,inty2,intx3,inty3)
{
intx,y,a1,b1,a2,b2,a3,b3,p=x2,q=y2; floatAngle;
printf("Entertheangleforrotation:");
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-8
ProgramtoImplementScalingTransformation.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>
#include<dos.h>
intx1,y1,x2,y2,x3,y3,mx,my;
voiddraw();
voidscale();
voidmain()
{
intgd=DETECT,gm;
intc;
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("Enterthe1stpointforthetriangle:");
scanf("%d%d",&x1,&y1);
printf("Enterthe2ndpointforthetriangle:");
scanf("%d%d",&x2,&y2);
printf("Enterthe3rdpointforthetriangle:");
scanf("%d%d",&x3,&y3);
draw();
scale();
}
voiddraw()
{
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
}
voidscale()
{
intx,y,a1,a2,a3,b1,b2,b3;
intmx,my;
printf("Enterthescallingcoordinates");
scanf("%d%d",&x,&y);
mx=(x1+x2+x3)/3;
my=(y1+y2+y3)/3;
cleardevice();
a1=mx+(x1-mx)*x;
b1=my+(y1-my)*y;
a2=mx+(x2-mx)*x;
b2=my+(y2-my)*y;
a3=mx+(x3-mx)*x;
b3=my+(y3-my)*y;
line(a1,b1,a2,b2);
line(a2,b2,a3,b3);
line(a3,b3,a1,b1);
draw();
getch();
}
OUTPUT
EXPERIMENT-9
Programtoimplement3drotationaboutanarbitraryaxis
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<process.h>
#include<graphics.h>
#include<dos.h>
intx1,x2,y1,y2,mx,my,depth;
voiddraw();
voidrotate();
voidmain()
{
intgd=DETECT,gm,c;
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("\n3DTransformationRotating\n\n");
printf("\nEnter1sttopvalue(x1,y1):");
scanf("%d%d",&x1,&y1);
printf("Enterrightbottomvalue(x2,y2):");
scanf("%d%d",&x2,&y2);
depth=(x2-x1)/4;
mx=(x1+x2)/2;
my=(y1+y2)/2;
draw();
getch();
cleardevice();
rotate();
getch();
}
voiddraw()
{
bar3d(x1,y1,x2,y2,depth,1);
}
voidrotate()
{
floatt;
inta1,b1,a2,b2,dep;
printf("Entertheangletorotate=");
scanf("%f",&t);
t=t*(3.14/180);
a1=mx+(x1-mx)*cos(t)-(y1-my)*sin(t);
a2=mx+(x2-mx)*cos(t)-(y2-my)*sin(t);
b1=my+(x1-mx)*sin(t)-(y1-my)*cos(t);
b2=my+(x2-mx)*sin(t)-(y2-my)*cos(t);
if(a2>a1)
dep=(a2-a1)/4;
else
dep=(a1-a2)/4;
bar3d(a1,b1,a2,b2,dep,1);
setcolor(5);
//draw();
}
OUTPUT
EXPERIMENT-10
Programtoimplementfloodfillalgorithm.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
voidflood(int,int,int,int);
voidmain()
{
intgd,gm=DETECT;
clrscr();
detectgraph(&gd,&gm);
initgraph(&gd,&gm,”c:\\tc\\bgi”);
rectangle(50,50,100,100);
flood(55,55,12,0);
getch();
}
voidflood(intx,inty,intfill_col,intold_col)
{
if(getpixel(x,y)==old_col)
{
delay(10);
putpixel(x,y,fill_col);
flood(x+1,y,fill_col,old_col);
flood(x-1,y,fill_col,old_col);
flood(x,y+1,fill_col,old_col);
flood(x,y-1,fill_col,old_col);
}
}
OUTPUT
EXPERIMENT-11
Programtoimplementfor3Dtranslation.
#includestdio.h
#includegraphics.h
#includemath.h
voidtrans();
intmaxx,maxy,midx,midy;
voidmain()
{
intch;
intgd=DETECT,gm;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"");
trans();
return0;
}
voidtrans()
{
intx,y,z,o,x1,x2,y1,y2;
midx=200;
midy=200;
bar3d(midx+50,midy-100,midx+100,midy-50,20,1);
delay(1000);
printf("Entertranslationfactor");
scanf("%d%d",&x,&y);
printf("Aftertranslation:");
bar3d(midx+x+50,midy-(y+100),midx+x+100,midy-(y+50),20,1);
}
OUTPUT
EXPERIMENT-12
Programtoimplementbasicsofgraphicslikeline,circle,arc,ellipseandrectangle.
#include<graphics.h>
#include<conio.h>
voidmain()
{
intgd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
setbkcolor(GREEN);
printf("\t\t\t\n\nLINE");
line(50,40,190,40);
printf("\t\t\n\n\n\nRECTANGLE");
rectangle(125,115,215,165);
printf("\t\t\t\n\n\n\n\n\n\nARC");
arc(120,200,180,0,30);
printf("\t\n\n\n\nCIRCLE");
circle(120,270,30);
printf("\t\n\n\n\nECLIPSE");
ellipse(120,350,0,360,30,20);
getch();
}
EXPERIMENT-13
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
intmain(){
intgd=DETECT,gm;
inti,maxx,midy;
/*initializegraphicmode*/
initgraph(&gd,&gm,"X:\\TC\\BGI");
/*maximumpixelinhorizontalaxis*/
maxx=getmaxx();
/*midpixelinverticalaxis*/
midy=getmaxy()/2;
for(i=0;i<maxx-150;i=i+5){
/*clearsscreen*/
cleardevice();
/*drawawhiteroad*/
setcolor(WHITE);
line(0,midy+37,maxx,midy+37);
/*DrawCar*/
setcolor(YELLOW);
setfillstyle(SOLID_FILL,RED);
line(i,midy+23,i,midy);
line(i,midy,40+i,midy-20);
line(40+i,midy-20,80+i,midy-20);
line(80+i,midy-20,100+i,midy);
line(100+i,midy,120+i,midy);
line(120+i,midy,120+i,midy+23);
line(0+i,midy+23,18+i,midy+23);
arc(30+i,midy+23,0,180,12);
line(42+i,midy+23,78+i,midy+23);
arc(90+i,midy+23,0,180,12);
line(102+i,midy+23,120+i,midy+23);
line(28+i,midy,43+i,midy-15);
line(43+i,midy-15,57+i,midy-15);
line(57+i,midy-15,57+i,midy);
line(57+i,midy,28+i,midy);
line(62+i,midy-15,77+i,midy-15);
line(77+i,midy-15,92+i,midy);
line(92+i,midy,62+i,midy);
line(62+i,midy,62+i,midy-15);
floodfill(5+i,midy+22,YELLOW);
setcolor(BLUE);
setfillstyle(SOLID_FILL,DARKGRAY);
/* DrawWheels*/
circle(30+i,midy+25,9);
circle(90+i,midy+25,9);
floodfill(30+i,midy+25,BLUE);
floodfill(90+i,midy+25,BLUE);
/*Adddelayof0.1milliseconds*/
delay(100);
}
getch();
closegraph();
return0;
}
OUTPUT
EXPERIMENT-14
Writeaprogramtodrawabouncingball.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
voidmain(){
intgd=DETECT,gm=DETECT;
intx,y=0,j,t=400,c=1;
initgraph(&gd,&gm,"c://tc//bgi");
setcolor(RED);
setfillstyle(SOLID_FILL,RED);
for(x=40;x<602;x++){
cleardevice();
circle(x,y,30);
floodfill(x,y,RED);
delay(40);
if(y>=400){
c=0;
t-=20;
}
if(y<=(400-t))
c=1;
y=y+(c?15:-15);
}
getch();
}
OUTPUT