100% found this document useful (1 vote)
486 views

C Graphics Tutorial

This tutorial provides summaries of C graphics programs that demonstrate various graphics concepts: 1. The first program initializes graphics mode, draws basic shapes (circle, line, rectangle), displays text, and closes graphics mode after a key press to demonstrate basic graphics functions. 2. Other programs demonstrate drawing bar charts, pie charts, animating a moving car that changes color, and 3D bar charts using different graphics functions. 3. Additional examples include a smiling face animation that appears randomly on screen, a captcha code generator, and a program that draws circles inside circles in different colors. The document provides the C code and output for each graphics program example.

Uploaded by

BaneeIshaqueK
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
486 views

C Graphics Tutorial

This tutorial provides summaries of C graphics programs that demonstrate various graphics concepts: 1. The first program initializes graphics mode, draws basic shapes (circle, line, rectangle), displays text, and closes graphics mode after a key press to demonstrate basic graphics functions. 2. Other programs demonstrate drawing bar charts, pie charts, animating a moving car that changes color, and 3D bar charts using different graphics functions. 3. Additional examples include a smiling face animation that appears randomly on screen, a captcha code generator, and a program that draws circles inside circles in different colors. The document provides the C code and output for each graphics program example.

Uploaded by

BaneeIshaqueK
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 48

C graphics tutorial

This tutorial is for all those who wish to learn c graphics programming, no knowledge of
graphics concepts is required. C Graphics programming is very easy and interesting. You
can use graphics programming for developing your own games, in making projects, for
animation etc. It's not like traditional C programming in which you have to apply
complex logic in your program and then you end up with a lot of errors and warnings in
your program. In C graphics programming you have to use standard library functions
( need not worry if you don't know functions ) to get your task done. Just you pass
arguments to the functions and it's done. On this website you will find almost all
functions with detailed explanation and a sample program showing the usage of a
function. To make things easy you are provided with executable files which you can
download and execute. Firstly you should know the function initgraph which is used to
initialize the graphics mode . To initialize graphics mode we use initgraph function in our
program. initgraph function is present in "graphics.h" header file, so your every graphics
program should include "graphics.h" header file. We will discuss initgraph withe help of
following sample program:-

Sample graphics code


#include<graphics.h>
#include<conio.h>
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
getch();
closegraph();
return 0;
}

Firstly let me tell you what is the output of this program, this program initializes
graphics mode and then closes it after a key is pressed. To begin with we have declared
two variables of int type gd and gm for graphics driver and graphics mode respectively,
you can choose any other variable name as you wish. DETECT is a macro defined in
"graphics.h" header file, then we have passed three arguments to initgraph function first
is the address of gd, second is the address of gm and third is the path where your BGI
files are present ( you have to adjust this accordingly where you turbo compiler is
installed). Initgraph function automatically decides an appropriate graphics driver and
mode such that maximum screen resolution is set, getch helps us to wait until a key is
pressed, closegraph function closes the graphics mode and finally return statement
returns a value 0 to main indicating successful execution of your program. After you
have understood initgraph function then you can use functions to draw shapes such as

circle, line , rectangle etc, then you can learn how to change colors and fonts using
suitable functions, then you can go for functions such as getimage, putimage etc for
doing animation.

c graphics programs
All these program have been made using c graphics. Program for various type of charts
and other interesting things and patterns.

C graphics codes

Draw shapes using c graphics


This c graphics program draws basic shapes such as circle, line, rectangle, ellipse and
display text on screen using c graphics. This can be a first graphics program for a
beginner.

C programming code
#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT,gm,left=100,top=100,right=200,bottom=200,x= 300,y=150,radius=50;
initgraph(&gd, &gm, "C:\\TC\\BGI");
rectangle(left, top, right, bottom);
circle(x, y, radius);
bar(left + 300, top, right + 300, bottom);
line(left - 10, top + 150, left + 410, top + 150);
ellipse(x, y + 200, 0, 360, 100, 50);
outtextxy(left + 100, top + 325, "My First C Graphics Program");
getch();
closegraph();
return 0;
}

Output of program:

c program draw bar chart


This program draws bar chart using c graphics. Chart is drawn using bars filled with
different styles and in different colors.

C programming code
#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
setcolor(YELLOW);
rectangle(0,30,639,450);
settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
setcolor(WHITE);
outtextxy(275,0,"Bar Chart");
setlinestyle(SOLID_LINE,0,2);

line(100,420,100,60);
line(100,420,600,420);
line(90,70,100,60);
line(110,70,100,60);
line(590,410,600,420);
line(590,430,600,420);
outtextxy(95,35,"Y");
outtextxy(610,405,"X");
outtextxy(85,415,"O");
setfillstyle(LINE_FILL,BLUE);
bar(150,100,200,419);
setfillstyle(XHATCH_FILL,RED);
bar(225,150,275,419);
setfillstyle(WIDE_DOT_FILL,GREEN);
bar(300,200,350,419);
setfillstyle(INTERLEAVE_FILL,MAGENTA);
bar(375,125,425,419);
setfillstyle(HATCH_FILL,BROWN);
bar(450,175,500,419);
getch();
return 0;
}

Output of program:

c program to draw pie chart


This c program draws a pie chart showing percentage of various components drawn with
different filling styles and colors.

C programming code
/* Program to draw a pie chart */
#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm, midx, midy;
initgraph(&gd, &gm, "C:\\TC\\BGI");
setcolor(MAGENTA);
rectangle(0,40,639,450);
settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
setcolor(WHITE);
outtextxy(275,10,"Pie Chart");
midx = getmaxx()/2;
midy = getmaxy()/2;
setfillstyle(LINE_FILL,BLUE);
pieslice(midx, midy, 0, 75, 100);
outtextxy(midx+100, midy - 75, "20.83%");
setfillstyle(XHATCH_FILL,RED);
pieslice(midx, midy, 75, 225, 100);
outtextxy(midx-175, midy - 75, "41.67%");
setfillstyle(WIDE_DOT_FILL,GREEN);
pieslice(midx, midy, 225, 360, 100);
outtextxy(midx+75, midy + 75, "37.50%");
getch();
return 0;
}

Output of program:

C program to move a car


Program in c using graphics move a car. A car is made using two rectangles and two
circles which act as tyres of car. A for loop is used to move the car forward by changing
the rectangle and circle coordinates and erasing the previous contents on screen using
clearviewport, you can also use cleardevice. Speed of car can be adjusted using delay
function, more the delay lesser will be the speed or lesser the delay your car will move
fast. In this program color of the car also keeps on changing, this is accomplished by
incrementing the color value by one each time in for loop, you can also use random
function for this purpose. Before you see a car moving you will be asked to press a key.

C programming code
#include <graphics.h>
#include <dos.h>
#include <conio.h>
main()
{
int i, j = 0, gd = DETECT, gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
settextstyle(DEFAULT_FONT,HORIZ_DIR,2);
outtextxy(25,240,"Press any key to view the moving car");
getch();
setviewport(0,0,639,440,1);
for( i = 0 ; i <= 420 ; i = i + 10, j++ )
{
rectangle(50+i,275,150+i,400);
rectangle(150+i,350,200+i,400);
circle(75+i,410,10);
circle(175+i,410,10);

setcolor(j);
delay(100);
if( i == 420 )
break;
clearviewport();
}
getch();
closegraph();
return 0;
}

c program to draw a 3d bar chart


This c program draws a 3d bar chart.

C programming code
#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
setcolor(YELLOW);
rectangle(0,30,639,450);
settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
setcolor(WHITE);
outtextxy(275,0,"Bar Chart");
setlinestyle(SOLID_LINE,0,2);
line(100,420,100,60);
line(100,420,600,420);
line(90,70,100,60);
line(110,70,100,60);
line(590,410,600,420);
line(590,430,600,420);
outtextxy(95,35,"Y");
outtextxy(610,405,"X");
outtextxy(85,415,"O");
setfillstyle(LINE_FILL,BLUE);
bar(150,100,200,419);
setfillstyle(XHATCH_FILL,RED);
bar(225,150,275,419);
setfillstyle(WIDE_DOT_FILL,GREEN);
bar(300,200,350,419);
setfillstyle(INTERLEAVE_FILL,MAGENTA);
bar(375,125,425,419);
setfillstyle(HATCH_FILL,BROWN);
bar(450,175,500,419);

getch();
return 0;
}

Output of program:

c smiling face animation


This animation using c draws a smiling face which appears at random position on screen.
See output below the code, it will help you in understanding the code easily.

C programming code
#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
main()
{
int gd = DETECT, gm, area, temp1, temp2, left = 25, top = 75;
void *p;
initgraph(&gd,&gm,"C:\\TC\\BGI");
setcolor(YELLOW);

circle(50,100,25);
setfillstyle(SOLID_FILL,YELLOW);
floodfill(50,100,YELLOW);
setcolor(BLACK);
setfillstyle(SOLID_FILL,BLACK);
fillellipse(44,85,2,6);
fillellipse(56,85,2,6);
ellipse(50,100,205,335,20,9);
ellipse(50,100,205,335,20,10);
ellipse(50,100,205,335,20,11);
area = imagesize(left, top, left + 50, top + 50);
p = malloc(area);
setcolor(WHITE);
settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
outtextxy(155,451,"Smiling Face Animation");
setcolor(BLUE);
rectangle(0,0,639,449);
while(!kbhit())
{
temp1 = 1 + random ( 588 );
temp2 = 1 + random ( 380 );
getimage(left, top, left + 50, top + 50, p);
putimage(left, top, p, XOR_PUT);
putimage(temp1 , temp2, p, XOR_PUT);
delay(100);
left = temp1;
top = temp2;
}
getch();
closegraph();
return 0;
}

Output of program:

captcha program in c
This program generates captcha, a captcha is a random code generated using some
algorithm. We will use random function in our code. These are used in typing tutors and
in website to check whether a human is operating on a website.

C programming code
#include<stdlib.h>
#include<dos.h>
#include<graphics.h>
main()
{
int i = 0, key, num, midx, gd = DETECT, gm;
char a[10];
initgraph(&gd,&gm,"C:\\TC\\BGI");
midx = getmaxx()/2;
settextstyle(SCRIPT_FONT,HORIZ_DIR,5);
settextjustify(CENTER_TEXT,CENTER_TEXT);
setcolor(GREEN);
outtextxy(midx,20,"CAPTCHA");
settextstyle(SCRIPT_FONT,HORIZ_DIR,2);
outtextxy(midx,125,"Press any key to change the generated random code \"captcha\"");
outtextxy(midx,150,"Press escape key to exit...");
setcolor(WHITE);
setviewport(100,200,600,400,1);
setcolor(RED);
randomize();
while(1)
{
while(i<6)
{
num = random(3);
if ( num == 0 )
a[i] = 65 + random(26);
else if ( num == 1)
a[i] = 97 + random(26);
else
a[i] = 48 + random(10);
i++;

/* 65 is the ASCII value of A */


/* 97 is the ASCII value of a */
/* 48 is the ASCII value of 0 */

}
a[i] = '\0';
outtextxy(210,100,a);
key = getch();
if( key == 27 )
exit(0);
clearviewport();
i = 0;
}
}

/* escape key*/

Output of program:

c program to draw circles in


circles
This program draws circles in circles in two different colors.

C programming code
#include<graphics.h>
#include<conio.h>
#include<dos.h>
main()
{
int gd = DETECT, gm, x, y, color, angle = 0;
struct arccoordstype a, b;
initgraph(&gd, &gm, "C:\\TC\\BGI");
delay(2000);
while(angle<=360)
{
setcolor(BLACK);
arc(getmaxx()/2,getmaxy()/2,angle,angle+2,100);
setcolor(RED);
getarccoords(&a);
circle(a.xstart,a.ystart,25);
setcolor(BLACK);
arc(getmaxx()/2,getmaxy()/2,angle,angle+2,150);
getarccoords(&a);
setcolor(GREEN);
circle(a.xstart,a.ystart,25);

angle = angle+5;
delay(50);
}
getch();
closegraph();
return 0;
}

Output of program:

c program countdown
This c graphics program performs countdown for 30 seconds.

C program countdown code


#include<graphics.h>
#include<dos.h>
#include<conio.h>
main()
{
int gd = DETECT, gm, i;
char a[5];
initgraph( &gd, &gm, "C:\\TC\\BGI");
settextjustify( CENTER_TEXT, CENTER_TEXT );
settextstyle(DEFAULT_FONT,HORIZ_DIR,3);
setcolor(RED);
for(i = 30 ; i >=0 ; i--)

{
sprintf(a,"%d",i);
outtextxy(getmaxx()/2, getmaxy()/2, a);
delay(1000);
if ( i == 0 )
break;
cleardevice();
}
getch();
closegraph();
return 0;
}

project example

Press me button game in c


C programming code
#include
#include
#include
#include
#include

<stdio.h>
<conio.h>
<dos.h>
<graphics.h>
<stdlib.h>

union REGS i, o;
int left = 265, top = 250;
void initialize_graphics_mode()
{
int gd = DETECT, gm, error;
initgraph(&gd,&gm,"C:\\TC\\BGI");
error = graphresult();
if (error != grOk)
{
perror("Error ");
printf("Press any key to exit...\n");
getch();
exit(EXIT_FAILURE);
}
}
void showmouseptr()
{
i.x.ax = 1;
int86(0x33,&i,&o);
}
void hidemouseptr()
{
i.x.ax = 2;
int86(0x33,&i,&o);
}
void getmousepos(int *x,int *y)

{
i.x.ax = 3;
int86(0x33,&i,&o);
*x = o.x.cx;
*y = o.x.dx;
}
void draw_bar()
{
hidemouseptr();
setfillstyle(SOLID_FILL,CYAN);
bar(190,180,450,350);
showmouseptr();
}
void draw_button(int x, int y)
{
hidemouseptr();
setfillstyle(SOLID_FILL,MAGENTA);
bar(x,y,x+100,y+30);
moveto(x+5,y);
setcolor(YELLOW);
outtext("Press me");
showmouseptr();
}
void draw()
{
settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
outtextxy(155,451,"<a
href="http://www.programmingsimplified.com"">www.programmingsimplified.com"</a>);
setcolor(BLUE);
rectangle(0,0,639,450);
setcolor(RED);
outtextxy(160,25,"Try to press the \"Press me\" button");
outtextxy(210,50,"Press escape key to exit");
setfillstyle(XHATCH_FILL,GREEN);
setcolor(BLUE);
bar(1,1,75,449);
bar(565,1,638,449);
showmouseptr();
draw_bar();
draw_button(left,top);
}
void initialize()
{
initialize_graphics_mode();
if( !initmouse() )
{
closegraph();
printf("Unable to initialize the mouse");
printf("Press any key to exit...\n");
getch();
exit(EXIT_SUCCESS);
}
draw();
}

int initmouse()
{
i.x.ax = 0;
int86(0x33,&i,&o);
return ( o.x.ax );
}
void get_input()
{
int x, y;
while(1)
{
getmousepos(&x,&y);
/* mouse pointer in left of button */
if( x >= (left-3) && y >= (top-3) && y <= (top+30+3) && x < left )
{
draw_bar();
left = left + 4;
if (left > 350)
left = 190;
draw_button(left,top);
}
/* mouse pointer in right of button */
else if (x<=(left+100+3)&&y>=(top-3)&&y<=(top+30+3)&&x>(left+100))
{
draw_bar();
left = left - 4;
if (left < 190)
left = 350;
draw_button(left,top);
}
/* mouse pointer above button */
else if(x>(left-3) && y>=(top-3) && y<(top) && x<= (left+100+3))
{
draw_bar();
top = top + 4;
if (top > 320)
top = 180;
draw_button(left,top);
}
/* mouse pointer below button */
else if (x>(left-3)&&y>(top+30)&&y<=(top+30+3)&&x<=(left+100+3))
{
draw_bar();
top = top - 4;

if (top < 180)


top = 320;
draw_button(left,top);
}
if (kbhit())
{
if (getkey() == 1)
exit(EXIT_SUCCESS);
}
}
}
int getkey()
{
i.h.ah = 0;
int86(22,&i,&o);
return( o.h.ah );
}
main()
{
initialize();
get_input();
return 0;
}

c program to restrict mouse


pointer in a circle
This program restricts mouse pointer in a circle i.e you can't move mouse out of a circle.
When you try to bring mouse pointer outside the circle, mouse pointer is moved to it's
previous location which is inside the circle. Code to restrict mouse in circle is given below
:-

C programming code
#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>
#include<math.h>
union REGS i, o;
int initmouse()
{
i.x.ax = 0;
int86(0X33, &i, &o);
return ( o.x.ax );
}
void showmouseptr()

{
i.x.ax = 1;
int86(0X33, &i, &o);
}
void hidemopuseptr()
{
i.x.ax = 2;
int86(0X33,&i,&o);
}
void getmousepos(int *x, int *y)
{
i.x.ax = 3;
int86(0X33, &i, &o);
*x = o.x.cx;
*y = o.x.dx;
}
void movemouseptr(int x, int y)
{
i.x.ax = 4;
i.x.cx = x;
i.x.dx = y;
int86(0X33, &i, &o);
}
main()
{
int gd = DETECT, gm, midx, midy, radius, x, y, tempx, tempy;
radius = 100;
initgraph(&gd, &gm, "C:\\TC\\BGI");
if(!initmouse())
{
closegraph();
exit(1);
}
midx = getmaxx()/2;
midy = getmaxy()/2;
showmouseptr();
movemouseptr(midx, midy);
circle(midx, midy, radius);
x = tempx = midx;
y = tempy = midy;
while(!kbhit())
{
getmousepos(&x, &y);
if((pow(x-midx,2)+pow(y-midy,2)-pow(radius,2))>0)
{
movemouseptr(tempx, tempy);
x = tempx;
y = tempy;
}
tempx = x;
tempy = y;

}
closegraph();
return 0;
}

C graphics function

arc function in c
Declaration :- void arc(int x, int y, int stangle, int endangle, int radius);
arc function is used to draw an arc with center (x,y) and stangle specifies starting angle,
endangle specifies the end angle and last parameter specifies the radius of the arc. arc
function can also be used to draw a circle but for that starting angle and end angle should
be 0 and 360 respectively.

C programming source code for arc


#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
arc(100, 100, 0, 135, 50);
getch();
closegraph();
return 0;
}

In the above program (100,100) are coordinates of center of arc, 0 is the starting angle,
135 is the end angle and 50 specifies the radius of the arc.
arc program executable.
Output of program:

bar function in c
Declaration :- void bar(int left, int top, int right, int bottom);
Bar function is used to draw a 2-dimensional, rectangular filled in bar . Coordinates of
left top and right bottom corner are required to draw the bar. Left specifies the Xcoordinate of top left corner, top specifies the Y-coordinate of top left corner, right
specifies the X-coordinate of right bottom corner, bottom specifies the Y-coordinate of
right bottom corner. Current fill pattern and fill color is used to fill the bar. To change fill
pattern and fill color use setfillstyle.

C programming code
#include <graphics.h>
#include <conio.h>
main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
bar(100, 100, 200, 200);
getch();
closegraph();
return 0;
}

Download bar program.


Output of program:

bar3d function in c
Declaration :- void bar3d(int left, int top, int right, int bottom, int depth, int topflag);

bar3d function is used to draw a 2-dimensional, rectangular filled in bar . Coordinates of


left top and right bottom corner of bar are required to draw the bar. left specifies the Xcoordinate of top left corner, top specifies the Y-coordinate of top left corner, right
specifies the X-coordinate of right bottom corner, bottom specifies the Y-coordinate of
right bottom corner, depth specifies the depth of bar in pixels, topflag determines
whether a 3 dimensional top is put on the bar or not ( if it is non-zero then it is put
otherwise not ). Current fill pattern and fill color is used to fill the bar. To change fill
pattern and fill color use setfillstyle.

C program of bar3d
#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
bar3d(100, 100, 200, 200, 20, 1);
getch();
closegraph();
return 0;
}

Output of program:

circle function in c
Declaration :- void circle(int x, int y, int radius);
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.

C program for circle


#include<graphics.h>
#include<conio.h>

main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
circle(100, 100, 50);
getch();
closegraph();
return 0;
}

In the above program (100, 100) are coordinates of center of the circle and 50 is the
radius of circle.
circle program.
Output of program:

cleardevice function in c
Declaration :- void cleardevice();
cleardevice function clears the screen in graphics mode and sets the current position to
(0,0). Clearing the screen consists of filling the screen with current background color.

C program for cleardevice


#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
outtext("Press any key to clear the screen.");
getch();
cleardevice();
outtext("Press any key to exit...");
getch();
closegraph();

return 0;
}

Note : Don't use clrscr in graphics mode.

closegraph function in c
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.
Declaration :- void closegraph();

C code of closegraph
#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
outtext("Press any key to close the graphics mode...");
getch();
closegraph();
return 0;
}

drawpoly function in c
Drawpoly function is used to draw polygons i.e. triangle, rectangle, pentagon, hexagon
etc.
Declaration :- void drawpoly( int num, int *polypoints );
num indicates (n+1) number of points where n is the number of vertices in a polygon,
polypoints points to a sequence of (n*2) integers . Each pair of integers gives x and y
coordinates of a point on the polygon. We specify (n+1) points as first point coordinates
should be equal to (n+1)th to draw a complete figure.
To understand more clearly we will draw a triangle using drawpoly, consider for example
the array :int points[] = { 320, 150, 420, 300, 250, 300, 320, 150};
points array contains coordinates of triangle which are (320, 150), (420, 300) and (250,
300). Note that last point(320, 150) in array is same as first. See the program below and
then its output, it will further clear your understanding.

C program for drawpoly


#include <graphics.h>
#include <conio.h>
main()
{
int gd=DETECT,gm,points[]={320,150,420,300,250,300,320,150};
initgraph(&gd, &gm, "C:\\TC\\BGI");
drawpoly(4, points);
getch();
closegraph();
return 0;
}

Output of program:

ellipse function in c
Declarations of ellipse function :void ellipse(int x, int y, int stangle, 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.

C programming code for ellipse


#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
ellipse(100, 100, 0, 360, 50, 25);

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

Output of program:

fillellipse function in c
Declaration of fillellipse function :void fillellipse(int x, int y, int xradius, int yradius);
x and y are coordinates of center of the ellipse, xradius and yradius are x and y radius of
ellipse respectively.

C program for fillellipse


#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
fillellipse(100, 100, 50, 25);
getch();
closegraph();
return 0;
}

Output of program:

fillpoly function in c
Fillpoly function draws and fills a polygon. It require same arguments as drawpoly.
Declaration :- void drawpoly( int num, int *polypoints );
For details of arguments see drawpoly.
fillpoly fills using current fill pattern and color which can be changed using setfillstyle.

C programming code
#include <graphics.h>
#include <conio.h>
main()
{
int gd=DETECT,gm,points[]={320,150,440,340,230,340,320,150};
initgraph(&gd, &gm, "C:\\TC\\BGI");
fillpoly(4, points);
getch();
closegraph();
return 0;
}

Output of program:

floodfill function
Declaration :- void floodfill(int x, int y, int border);
floodfill function is used to fill an enclosed area. Current fill pattern and fill color is used
to fill the area.(x, y) is any point on the screen if (x,y) lies inside the area then inside will
be filled otherwise outside will be filled,border specifies the color of boundary of area. To
change fill pattern and fill color use setfillstyle. Code given below draws a circle and then
fills it.

C programming code
#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
setcolor(RED);
circle(100,100,50);
floodfill(100,100,RED);
getch();
closegraph();
return 0;
}

In the above program a circle is drawn in RED color. Point (100,100) lies inside the circle
as it is the center of circle, third argument to floodfill is RED which is color of boundary
of circle. So the output of above program will be a circle filled with WHITE color as it is
the default fill color.
floodfill program.
Output of program:

getarcoords function in c
Declaration :- void getarccoords(struct arccoordstype *var);
getarccoords function is used to get coordinates of arc which is drawn most recently.
arccoordstype is a predefined structure which is defined as follows:
struct arccoordstype
{
int x, y;
/* center point of arc
int xstart, ystart;
/* start position
int xend, yend;
/* end position
};

*/
*/
*/

address of a structure variable of type arccoordstype is passed to function getarccoords.

C program of getarccoords

#include<graphics.h>
#include<conio.h>
#include<stdio.h>
main()
{
int gd = DETECT, gm;
struct arccoordstype a;
char arr[100];
initgraph(&gd, &gm,"C:\\TC\\BGI");
arc(250,200,0,90,100);
getarccoords(&a);
sprintf(arr,"(%d, %d)",a.xstart,a.ystart);
outtextxy(360,195,arr);
sprintf(arr,"(%d, %d)",a.xend,a.yend);
outtextxy(245,85,arr);
getch();
closegraph();
return 0;
}

In the above program we have drawn an arc and then we get the coordinates of end
points of arc using getarccoords.Coordinates so obtained are displayed using outtextxy.

getbkcolor function in c
getbkcolor function returns the current background color
Declaration : int getbkcolor();
e.g. color = getbkcolor(); // color is an int variable
if current background color is GREEN then color will be 2.

C program for getbkcolor


#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm, bkcolor;
char a[100];
initgraph(&gd,&gm,"C:\\TC\\BGI");
bkcolor = getbkcolor();
sprintf(a,"Current background color = %d", bkcolor);
outtextxy( 10, 10, a);
getch();
closegraph();
return 0;
}

getcolor function
getcolor function returns the current drawing color.
Declaration : int getcolor();
e.g. a = getcolor(); // a is an integer variable
if current drawing color is WHITE then a will be 15.
See colors in c graphics.

C programming code for getcolor


#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm, drawing_color;
char a[100];
initgraph(&gd,&gm,"C:\\TC\\BGI");
drawing_color = getcolor();
sprintf(a,"Current drawing color = %d", drawing_color);
outtextxy( 10, 10, a );
getch();
closegraph();
return 0;
}

getdrivername function
getdrivername function returns a pointer to the current graphics driver.

C program for getdrivername


#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm;
char *drivername;
initgraph(&gd, &gm, "C:\\TC\\BGI");
drivername = getdrivername();
outtextxy(200, 200, drivername);
getch();
closegraph();
return 0;
}

getimage function in c
getimage function saves a bit image of specified region into memory, region can be any
rectangle.
Declaration:- void getimage(int left, int top, int right, int bottom, void *bitmap);
getimage copies an image from screen to memory. Left, top, right, and bottom define the
area of the screen from which the rectangle is to be copied, bitmap points to the area in
memory where the bit image is stored.
Smiling face animation program using getimage.

getmaxcolor function
getmaxcolor function returns maximum color value for current graphics mode and
driver. Total number of colors available for current graphics mode and driver are
( getmaxcolor() + 1 ) as color numbering starts from zero.
Declaration :- int getmaxcolor();

C program of getmaxcolor
#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm, max_colors;
char a[100];
initgraph(&gd,&gm,"C:\\TC\\BGI");
max_colors = getmaxcolor();
sprintf(a,"Maximum number of colors for current graphics mode and driver = %d",max_colors+1);
outtextxy(0, 40, a);
getch();
closegraph();
return 0;
}

getmaxx function in c
getmaxx function returns the maximum X coordinate for current graphics mode and
driver.
Declaration :- int getmaxx();

C program for getmaxx


#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm, max_x;
char array[100];
initgraph(&gd,&gm,"C:\\TC\\BGI");
max_x = getmaxx();
sprintf(array, "Maximum X coordinate for current graphics mode and driver = %d.",max_x);
outtext(array);
getch();
closegraph();
return 0;
}

getmaxy function in c
getmaxy function returns the maximum Y coordinate for current graphics mode and
driver.
Declaration :- int getmaxy();

C program for getmaxy


#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm, max_y;
char array[100];
initgraph(&gd,&gm,"C:\\TC\\BGI");
max_y = getmaxy();
sprintf(array, "Maximum Y coordinate for current graphics mode and driver is = %d.",max_y);
outtext(array);
getch();
closegraph();
return 0;
}

getpixel function in c
getpixel function returns the color of pixel present at location(x, y).
Declaration :- int getpixel(int x, int y);

C program for getpixel


#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm, color;
char array[50];
initgraph(&gd,&gm,"C:\\TC\\BGI");
color = getpixel(0, 0);
sprintf(array,"color of pixel at (0,0) = %d",color);
outtext(array);
getch();
closegraph();
return 0;
}

As we haven't drawn anything on screen and by default screen is BLACK, therefore color
of pixel at (0,0) is BLACK. So output of program will be
color of pixel at (0,0) is 0, as 0 indicates BLACK color.

getx function in c
getx function returns the X coordinate of current position.
Declaration :- int getx();

C program of getx
#include <graphics.h>
#include <conio.h>
main()
{
int gd = DETECT, gm;
char array[100];
initgraph(&gd, &gm, "C:\\TC\\BGI");
sprintf(array, "Current position of x = %d",getx());
outtext(array);
getch();
closegraph();
return 0;
}

gety function in c
gety function returns the y coordinate of current position.

Declaration :- int gety();

C programming source code for gety


#include <graphics.h>
#include <conio.h>
main()
{
int gd = DETECT, gm, y;
char array[100];
initgraph(&gd, &gm, "C:\\TC\\BGI");
y = gety();
sprintf(array, "Current position of y = %d", y);
outtext(array);
getch();
closegraph();
return 0;
}

graphdefaults function in c
graphdefaults function resets all graphics settings to their defaults.
Declaration :- void graphdefaults();
It resets the following graphics settings :

Sets the viewport to the entire screen.


Moves the current position to (0,0).
Sets the default palette colors, background color, and drawing color.
Sets the default fill style and pattern.
Sets the default text font and justification.

C programming source code for graphdefaults


#include <graphics.h>
#include <conio.h>
main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
setcolor(RED);
setbkcolor(YELLOW);
circle(250, 250, 50);
getch();

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

In the above program we have first changed the drawing color to RED and background
color to YELLOW and then drawn a circle with (250, 250) as center and 50 as radius.
When the user will press a key graphdefaults is called and both drawing and background
color will be reset to their default values i.e. WHITE and BLACK respectively. See colors
in c graphics.

grapherrormsg function in c
grapherrormsg function returns an error message string.
Declaration :- char *grapherrormsg( int errorcode );

C programming code for grapherrormsg


#include <graphics.h>
#include <stdlib.h>
#include <conio.h>
main()
{
int gd, gm, errorcode;
initgraph(&gd, &gm, "C:\\TC\\BGI");
errorcode = graphresult();
if(errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to exit.");
getch();
exit(1);
}
getch();
closegraph();
return 0;
}

In the above program we have not written gd = DETECT.


After running this program we get the output :Graphics error: Graphics hardware not detected
Press any key to exit.

imagesize function in c

imagesize function returns the number of bytes required to store a bitimage. This
function is used when we are using getimage.
Declaration:- unsigned int imagesize(int left, int top, int right, int bottom);

C programming code for imagesize


#include <graphics.h>
#include <conio.h>
main()
{
int gd = DETECT, gm, bytes;
char array[100];
initgraph(&gd, &gm, "C:\\TC\\BGI");
circle(200, 200, 50);
line(150, 200, 250, 200);
line(200, 150, 200, 250);
bytes = imagesize(150, 150, 250, 250);
sprintf(array, "Number of bytes required to store required area = %d", bytes);
outtextxy(10, 280, array);
getch();
closegraph();
return 0;
}

Output of program:

line function in c
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.
Declaration :- void line(int x1, int y1, int x2, int y2);

C programming code for line


#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
line(100, 100, 200, 200);
getch();
closegraph();
return 0;
}

Above program draws a line from (100, 100) to (200, 200).


line program.
Output of program:

lineto function in c
lineto function draws a line from current position(CP) to the point(x,y), you can get
current position using getx and gety function.

C programming code for lineto


#include<graphics.h>
#include<conio.h>

main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
moveto(100, 100);
lineto(200, 200);
getch();
closegraph();
return 0;
}

linerel function in c
Linerel function draws a line from the current position(CP) to a point that is a relative
distance (x, y) from the CP, then advances the CP by (x, y). You can use getx and gety to
find the current position.

C programming code for linerel


#include <graphics.h>
#include <conio.h>
main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
moveto(250, 250);
linerel(100, -100);
getch();
closegraph();
return 0;
}

moveto function in c
moveto function changes the current position (CP) to (x, y)
Declaration :- void moveto(int x, int y);

C programming code for moveto


#include <graphics.h>
#include <conio.h>
main()
{
int gd = DETECT, gm;
char msg[100];
initgraph(&gd, &gm, "C:\\TC\\BGI");
sprintf(msg, "X = %d, Y = %d",getx(),gety());

outtext(msg);
moveto(50, 50);
sprintf(msg, "X = %d, Y = %d", getx(), gety());
outtext(msg);
getch();
closegraph();
return 0;
}

moverel function in c
moverel function moves the current position to a relative distance.
Declaration :- void moverel(int x, int y);

C programming code for moverel


#include <graphics.h>
#include <conio.h>
main()
{
int gd = DETECT, gm, x, y;
char message[100];
initgraph(&gd, &gm, "C:\\TC\\BGI");
moveto(100, 100);
moverel(100, -100);
x = getx();
y = gety();
sprintf(message, "Current x position = %d and y position = %d", x, y);
outtextxy(10, 10, message);
getch();
closegraph();
return 0;
}

outtext function
outtext function displays text at current position.
Declaration :- void outtext(char *string);

C programming code for outtext


#include<graphics.h>
#include<conio.h>
main()

{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
outtext("To display text at a particular position on the screen use outtextxy");
getch();
closegraph();
return 0;
}

Do not use text mode functions like printf, gotoxy etc while working in graphics mode.
Also note '\n' or other escape sequences do not work in graphics mode. You have to
ensure that the text doesn't go beyond the screen while using outtext.

outtextxy function in c
outtextxy function display text or string at a specified point(x,y) on the screen.
Declaration :- void outtextxy(int x, int y, char *string);
x, y are coordinates of the point and third argument contains the address of string to be
displayed.

C programming code for outtextxy


#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
outtextxy(100, 100, "Outtextxy function");
getch();
closegraph();
return 0;
}

pieslice function in c
#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
pieslice(200,200,0,135,100);
getch();
closegraph();
return 0;

Output of program:

putimage function in c
putimage function outputs a bit image onto the screen.
Declaration:- void putimage(int left, int top, void *ptr, int op);
putimage puts the bit image previously saved with getimage back onto the screen, with
the upper left corner of the image placed at (left, top). ptr points to the area in memory
where the source image is stored. The op argument specifies a operator that controls how
the color for each destination pixel on screen is computed, based on pixel already on
screen and the corresponding source pixel in memory.
Smiling face animation program using putimage.

putpixel function in c
putpixel function plots a pixel at location (x, y) of specified color.
Declaration :- void putpixel(int x, int y, int color);

For example if we want to draw a GREEN color pixel at (35, 45) then we will write
putpixel(35, 35, GREEN); in our c program, putpixel function can be used to draw
circles, lines and ellipses using various algorithms.

C programming code for putpixel


#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
putpixel(25, 25, RED);
getch();
closegraph();
return 0;
}

Output of this program will be a RED pixel on screen at (25, 25) . Try to spot that pixel
with your eyes at left top portion of your computer screen.

rectangle function in c
Declaration :- void rectangle(int left, int top, int right, int bottom);
rectangle function is used to draw a rectangle. Coordinates of left top and right bottom
corner are required to draw the rectangle. left specifies the X-coordinate of top left
corner, top specifies the Y-coordinate of top left corner, right specifies the X-coordinate
of right bottom corner, bottom specifies the Y-coordinate of right bottom corner. The
code given below draws a rectangle.

c programming code for rectangle


#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
rectangle(100,100,200,200);
getch();
closegraph();
return 0;
}

sector function in c

Sector function draws and fills an elliptical pie slice.


Declaration :- void sector( int x, int y, int stangle, int endangle, int xradius, int yradius);

C programming code for sector


#include <graphics.h>
#include <conio.h>
main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
sector(100, 100, 0, 135, 25, 35);
getch();
closegraph();
return 0;
}

setbkcolor function in c
Declaration :- void setbkcolor(int color);
setbkcolor function changes current background color e.g. setbkcolor(YELLLOW)
changes the current background color to YELLOW.
Remember that default drawing color is WHITE and background color is BLACK.

C programming code for setbkcolor


#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
outtext("Press any key to change the background color to GREEN.");
getch();
setbkcolor(GREEN);
getch();
closegraph();
return 0;
}

setcolor function in c
Declaration :- void setcolor(int color);

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.For
Example :- BLACK is assigned 0, RED is assigned 4 etc. setcolor function is used to
change the current drawing color.e.g. setcolor(RED) or setcolor(4) changes the current
drawing color to RED. Remember that default drawing color is WHITE.

C programming code for setcolor


#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
circle(100,100,50);
setcolor(RED);
circle(200,200,50);

/* drawn in white color */


/* drawn in red color */

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

Download setcolor program.


Output of program:

setfillstyle function in c
setfillstyle function sets the current fill pattern and fill color.
Declaration :- void setfillstyle( int pattern, int color);

Different fill styles:


enum fill_styles
{
EMPTY_FILL,
SOLID_FILL,
LINE_FILL,
LTSLASH_FILL,
SLASH_FILL,
BKSLASH_FILL,
LTBKSLASH_FILL,
HATCH_FILL,
XHATCH_FILL,
INTERLEAVE_FILL,
WIDE_DOT_FILL,
CLOSE_DOT_FILL,
USER_FILL
};

C programming source code for setfillstyle


#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
setfillstyle(XHATCH_FILL, RED);
circle(100, 100, 50);
floodfill(100, 100, WHITE);
getch();
closegraph();
return 0;
}

Output of program:

setlinestyle in c

Declaration:
void setlinestyle( int linestyle, unsigned upattern, int thickness );
Available line styles:
enum line_styles
{
SOLID_LINE,
DOTTED_LINE,
CENTER_LINE,
DASHED_LINE,
USERBIT_LINE
};

C programming code
#include <graphics.h>
main()
{
int gd = DETECT, gm, c , x = 100, y = 50;
initgraph(&gd, &gm, "C:\\TC\\BGI");
for ( c = 0 ; c < 5 ; c++ )
{
setlinestyle(c, 0, 2);
line(x, y, x+200, y);
y = y + 25;
}
getch();
closegraph();
return 0;
}

settextstyle function in c
Settextstyle function is used to change the way in which text appears, using it we can
modify the size of text, change direction of text and change the font of text.
Declaration :- void settextstyle( int font, int direction, int charsize);
font argument specifies the font of text, Direction can be HORIZ_DIR (Left to right) or
VERT_DIR (Bottom to top).

Different fonts
enum font_names
{
DEFAULT_FONT,
TRIPLEX_FONT,
SMALL_FONT,
SANS_SERIF_FONT,
GOTHIC_FONT,
SCRIPT_FONT,
SIMPLEX_FONT,

TRIPLEX_SCR_FONT,
COMPLEX_FONT,
EUROPEAN_FONT,
BOLD_FONT
};

C programming source code for settextstyle


#include <graphics.h>
#include <conio.h>
main()
{
int gd = DETECT, gm, x = 25, y = 25, font = 0;
initgraph(&gd,&gm,"C:\\TC\\BGI");
for ( font = 0 ; font <= 10 ; font++)
{
settextstyle(font, HORIZ_DIR, 1);
outtextxy(x, y, "Text with different fonts");
y = y + 25;
}
getch();
closegraph();
return 0;
}

Output of program:

setviewport function in c
setviewport function sets the current viewport for graphics output.
Declaration :- void setviewport(int left, int top, int right, int bottom, int clip);
setviewport function is used to restrict drawing to a particular portion on the screen. For
example setviewport(100 , 100, 200, 200, 1);
will restrict our drawing activity inside the rectangle(100,100, 200, 200).

left, top, right, bottom are the coordinates of main diagonal of rectangle in which we
wish to restrict our drawing. Also note that the point (left, top) becomes the new origin.

C programming source code for setviewport


#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm, midx, midy;
initgraph(&gd, &gm, "C:\\TC\\BGI");
midx = getmaxx()/2;
midy = getmaxy()/2;
setviewport(midx - 50, midy - 50, midx + 50, midy + 50, 1);
circle(50, 50, 55);
getch();
closegraph();
return 0;
}

Output of program:

textheight function in c
textheight function returns the height of a string in pixels.
Declaration :- int textheight(char *string);

C programming source code for textheight


#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm, height;
char array[100];
initgraph(&gd, &gm, "C:\\TC\\BGI");
height = textheight("C programming");
sprintf(array,"Textheight = %d",height);
outtext(array);
getch();
closegraph();
return 0;
}

textwidth function in c
textwidth function returns the width of a string in pixels.
Declaration :- int textwidth(char *string);

C programming source code for textwidth


#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm, width;
char array[100];
initgraph(&gd, &gm, "C:\\TC\\BG I");
width = textwidth("C programming");
sprintf(array,"Textwidth = %d",width);
outtext(array);
getch();
closegraph();
return 0;
}

You might also like