Professional Documents
Culture Documents
CG practicals
CG practicals
CG practicals
1) The first line to look at is: GRAPHICS.H ,this file contains definitions and explanation of all the
graphic functions and constants. While GRAPHICS.LIB file contains standard graphic
functions.
4) The two int variables gdriver, gmode are graphic driver and graphic mode respectively.
5) The gmode handles value that tells us which resolution and monitor we are using.
6) The gdriver specifies the graphic driver to be used. In our program we have gd=DETECT
means we have passed the highest possible value available for the detected driver. The ” &”
symbol is used for initgraph to pass address of the constants.
7) Path ( ” C:\\Turboc3\\BGI”) : It specifies the directory path where initgraph looks for graphics
drivers (*.BGI) first.
8) Closegraph( ) : The closegraph() switches back the screen from grpahics mode to text mode.
If you don’t use this function then you may have undesirable effects.
Note:
1) Make sure you have entered the correct path for the include & library directories. You can change
the path by pointing your mouse to : Options > Directories. Enter the valid path for the include
directory and libraries,and output directories.
2) After installation of Turbo C,you have to adjust the settings of linker. Go to Options>Linker >
Libraries> and then check the ” Graphics Library“. This will help to solve the linker errors for the
graphics programs. Please do not uncheck any other option already selected by compiler.
3) Graphic initialisation depends on the path mentioned in initgraph path. Be sure to enter slash
between c,tc,bgi. The path C & TC depends on user if he installed TC in d: drive then it will be d,tc.
Read the above code’s path carefully.
1
Practical N0. 1 : To draw line ,circle and arc
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gdriver = DETECT,gmode;
initgraph(&gdriver,&gmode,"C:\\Turboc3\\BGI");
//initgraph(&gdriver,&gmode,"C:\\TC\\BGI");
setcolor(2); //green
line(150,250,250,300);
setcolor(4); //red
circle(320,320,100);//x axis , y axis and the radius
setcolor(10); //yellow
arc(250,300,0,90,50);
getch();
}
2
Practical N0. 2 : To display current position , pieslice and setfillstyle
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gdriver = DETECT,gmode;
initgraph(&gdriver,&gmode,"C:\\Turboc3\\BGI");
setcolor(2); //green
cout<<"the current position" <<getx()<<gety();
moveto(20,30);
cout<<"\n now the new position " <<getx()<<gety();
setcolor(4); //red
setfillstyle(3,12); //3=slanting lines and 12 =red color for below diagram
pieslice(150,130,35,120,55);
getch();
}
// setfillstyle(pattern , color) patern can be1: plain 2: horizontal line , 3: slanting, line , empty
Pieslice(x,y, start angle, end angle , radius)
3
Practical No3 : To understand getmodename , max and min value of screen and mode .
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gdriver = DETECT,gmode;
initgraph(&gdriver,&gmode,"C:\\Turboc3\\BGI");
cout<<"\n THE CURRENT MODE NEAME IS "<<getmodename(gmode);
cout<<"\n THE MAXIMUM VALUE OF X AND Y IS "<<getmaxx()<<"\t"<<getmaxy();
cout<<"\n THE CURRENT MODE IS :"<<gmode;
outtextxy(250,50,"HELLO HOW ARE YOU");
getch();
}
4
Practical 4: to understand outtext and settext style. To draw triangle
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gdriver = DETECT,gmode;
initgraph(&gdriver,&gmode,"C:\\Turboc3\\BGI");
int triangle[8]={150,100,200,390,250,150,150,100};
drawpoly(4,triangle);
outtextxy(30,50,"hello");
settextstyle(4,1,3);
outtextxy(240,50,"hello");
settextstyle(6,0,4); //0=horizontal
outtextxy(350,50,"hello");
getch();
}
/*
5
Practical 5: to draw rectangle and understand fillpoly
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gdriver = DETECT,gmode;
initgraph(&gdriver,&gmode,"C:\\Turboc3\\BGI");
int triangle[8]={150,100,200,390,250,150,150,100};
outtextxy(20,20,"RECTANGLE AND FILL POLY");
rectangle(50,50,100,200);
fillpoly(4,triangle);
getch();
}
//fillpoly(color, int)
6
Practical 6; to draw pentagon , hexagon and octagon
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gdriver = DETECT,gmode;
initgraph(&gdriver,&gmode,"C:\\Turboc3\\BGI");
int pentagon[12]= {340,150,320,110,360,70,400,110,380,150,340,150};
int hex[14]={360,260,340,240,360,220,400,220,420,240,400,260,360,260};
int octagon[18]={450,150,430,120,430,100,450,70,500,70,520,100,520,120,500,150,450,150};
drawpoly(6,pentagon);
drawpoly(7,hex);
drawpoly(9,octagon);
getch();
}
7
Practical 7: to draw ellipse , sector , fill ellipse
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gdriver = DETECT,gmode;
initgraph(&gdriver,&gmode,"C:\\Turboc3\\BGI");
ellipse(135,400,0,360,80,70);
sector(300,250,150,240,90,120);
fillellipse(320,130,160,80);
getch();
}
/*
void ellipse(int x, int y, int startangle, int endangle, int xradius, int yradius);
Ellipse is used to draw an ellipse (x,y) are coordinates of center of the ellipse, stangle is the starting
angle, end angle 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.
void sector( int x, int y, int start angle, int endangle, int xradius, int yradius);
8
Practical 8: TO UNDERSTAND DDA ALGORITHM
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
#include<dos.h>
void ddaline(int x1,int x2,int y1,int y2,int color);
void main()
{
int x1,y1,x2,y2,color;
int gd=DETECT,gm;
do /*loop for value of x1*/
{
cout<<"enter the staring x-coordinat";
cin>>x1;
if(x1<0||x1>639)
cout<<"please enter the value in the range 0-639";
}
while(x1<0||x1>639);
do
{
cout<<"enter staring y-coordinate";
cin>>y1;
if(y1<0||y1>479)
cout<<"please enter the value in the range 0-479";
}
while(y1<0||y1>479);
do /*loop for value of x1*/
{
cout<<"enter the staring x-coordinate";
cin>>x2;
if(x2<0||x2>639)
cout<<"please enter the value in the range 0-639";
}
while(x2<0||x2>639);
do
{
cout<<"enter staring y-coordinate";
cin>>y2;
if(y2<0||y2>479)
cout<<"please enter the value in the range 0-479";
}
while(y2<0||y2>479);
do
{
cout<<"enter the value of color";
cin>>color;
if(color<0||color>15);
cout<<"please enter the value in the range 0-15";
}
while(color<0||color>15);
initgraph(&gd,&gm,"C:\\TC\\BGI");
cleardevice();
setcolor(2);
outtextxy(50,50,"DDA.LINE ALGO");
moveto(40,50);
cout<<"x1="<<” “<<x1<<"y1="<<” “<<y1;
9
moveto(x2,y2);
cout<<"x2="<<x2<<"y2="<<y2;
ddaline(x1,y1,x2,y2,color);
getch();
closegraph();
}
void ddaline(int x1,int y1,int x2,int y2,int color);
{
float dx,dy,x,y,xinc,yinc,length;
int i;
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
length=abs(dx);
else
length=abs(dx);
xinc=dx+0.5;
yinc=dy+0.5;
for(i=1;i<=length,i++)
{
putpixel(x,y,10) ;
x=x*xinc;
y=y*yinc;
delay(0);
}
}
10
#include<conio.h>
#include<math.h>
void bline(int x1,int x2,int y1,int y2,int color);
void main()
{ int x1,y1,x2,y2,color;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
do
{ cout<<"enter the value x1:";
cin>>x1;
if(x1<0||x1>639);
cout<<"please enter the value in the range 0-639";
}
while(x1<0||x1>639);
do
{ cout<<"enter the value of x2:";
cin>>x2;
if(x2<0||x2>639);
cout<<"please enter the value in the range 0-639";
}
while(x2<0||x2>639);
do
{
if(x1==x2)
cout<<"correct value for x1 & x2";
}
while(x1==x2);
do
{
cout<<"enter value of y1:";
cin>>y1;
if(y1<0||y1>479)
cout<<"please enter the value in the range 0-479";
}
while(y1<0||y1>479);
do
{
cout<<"enter the value of y2:";
cin>>y2;
if(y2<0||y2>479);
cout<<"please enter the value in the range 0-479";
}
while(y2<0||y2>479);
do
{
if(y1==y2)
cout<<"enter the correct value of y1 & y2";
}
while(y1==y2);
do
{
cout<<"enter the value of color";
cin>>color;
if(color<0||color>15)
cout<<"please enter the value in the range 0-15";
}
while(color<0||color>15);
cleardevice();
setcolor(2);
outtextxy(50,50,"BLINE ALGO");
11
bline(x1,y1,x2,y2,color);
getch();
closegraph();
}
void bline(int x1,int y1,int x2,int y2,int color)
{
int x,y,dx,dy,i,b;
dx=x2-x1;
dy=y2-y1;
b=2*dy-dx;
x=x1;
y=y1;
putpixel((int)x,(int)y,color);
for(i=1;i<dx;i++)
{ putpixel(x,y,color);
while(b>=0)
{ y=y+1;
b=b-2*dx;
}
x=x+1;
b=b+2*dy;
}
}
12
#include<iostream.h>
#include<dos.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
void bcircle(int xc,int yc,int r,int color);
void main()
{
int xc,yc,r,color;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
do
{
cout<<"enter the value of xc:";
cin>>xc;
if(xc<0||xc>639)
cout<<"please enter the value in the range 0-639";
}
while(xc<0||xc>639);
do
{
cout<<"enter the value of yc:";
cin>>yc;
if(yc<0||yc>479)
cout<<"please enter the value in the range 0-479";
}
while(yc<0||yc>479);
do
{
cout<<"enter the value of r:";
cin>>r;
if(r<=0)
cout<<"please enter the currect value of r:";
}
while(r<=0);
do
{
cout<<"enter the value of color:";
cin>>color;
if(color<0||color>15)
cout<<"please enter the value in the range 0-15";
}
while(color<0||color>15);
cleardevice();
setcolor(2);
outtextxy(50,50,"BRESENHAM'S CIRCLE ALGO");
bcircle(xc,yc,r,color);
getch();
closegraph();
}
void bcircle(int xc,int yc,int r,int color)
{
int x,y,p;
p=3-2*r;
x=0;
y=r;
do
{
putpixel(xc+x,yc+y,color);
putpixel(xc+x,yc-y,color);
13
putpixel(xc-x,yc+y,color);
putpixel(xc-x,yc-y,color);
putpixel(xc+y,yc+x,color);
putpixel(xc+y,yc-x,color);
putpixel(xc-y,yc+x,color);
putpixel(xc-y,yc-x,color);
if(p<0)
p=p+4*x+6;
else
{
p=p+4*(x-y)+10;
y=y-1;
}
x=x+1;
delay(80);
}
while(x<y);
}
14