You are on page 1of 10

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.

Page 1 of 11
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;
}

Page 2 of 11
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.

Page 3 of 11
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 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. 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;
}

Page 4 of 11
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 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, 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;
}

Page 5 of 11
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.

Page 6 of 11
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.

Page 7 of 11
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;
}

Page 8 of 11
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;
}

Page 9 of 11
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>

int main()
{
int gd = DETECT, gm;

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

fillellipse(100, 100, 50, 25);

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

Page 10 of 11

You might also like