C Programming – CSIT 1st Semester
Unit-11: Introduction to Graphics
Introduction:
The representation of an object in a Pictorial view is called a graphics which is
being used in every kind of application nowadays. In a C program, first step is to
initialize the graphics drivers on the computer. This is done using the initgraph
method provided in graphics.h library.
Graphics mode Initialization
First of all, we have to call the initgraph function that will initialize the graphics
mode on the computer. initgraph method has the following prototype.
void initgraph(int &graphdriver, int &graphmode, “path of BGI”);
initgraph initializes the graphics system by loading the graphics driver from disk
(or validating a registered driver) then putting the system into graphics mode.
Initgraph also resets all graphics settings (color, palette, current position, viewport,
etc.) to their defaults, then resets graphresult to 0.
graphdriver
It is a pointer to an integer specifying the graphics driver to be used. It tells the
compiler that what graphics driver to use or to automatically detect the drive. In all
our programs we will use DETECT macro of graphics.h library that instruct
compiler for auto detection of graphics driver.
We can give it a value using a constant of the graphics_drivers enum type, which is
defined in graphics.h and listed below.
graphics_drivers constant Numeric value
DETECT 0 (requests autodetect)
CGA 1
MCGA 2
By Lec. Pratik Chand Page 1
C Programming – CSIT 1st Semester
EGA 3
EGA64 4
EGAMONO 5
IBM8514 6
HERCMONO 7
ATT400 8
VGA 9
PC3270 10
graphmode
It is a pointer to an integer that specifies the graphics mode to be used.
If *gdriver is set to DETECT, then initgraph sets *graphmode to the highest
resolution available for the detected driver.
pathtodriver
It specifies the directory path where graphics driver files (BGI files) are located. If
directory path is not provided, then it will search for driver files in current working
directory. graphdriver and graphmode must be set to valid graphics_drivers and
graphics_mode values otherwise you’ll get unpredictable results. (The exception is
graphdriver = DETECT.)
After a call to initgraph, *graphdriver is set to the current graphics driver, and
*graphmode is set to the current graphics mode. You can tell initgraph to use a
particular graphics driver and mode, or to auto detect the attached video adapter at
run time and pick the corresponding driver. If you tell initgraph to auto detect, it
calls detectgraph to select a graphics driver and mode.
Normally, initgraph loads a graphics driver by allocating memory for the driver,
then loading the appropriate BGI file from disk. As an alternative to this dynamic
loading scheme, you can link a graphics driver file (or several of them) directly
into your executable program file.
By Lec. Pratik Chand Page 2
C Programming – CSIT 1st Semester
Sample graphics code
// This program initializes graphics mode and then closes it after a key is pressed
#include<graphics.h>
#include<conio.h>
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
getch();
closegraph();
return 0;
}
Here, in this program is 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 well.
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 your Turbo C 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 the 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.
By Lec. Pratik Chand Page 3
C Programming – CSIT 1st Semester
BGI
Borland Graphics Interface (BGI) is a graphics library that is bundled with several
Borland compilers for the DOS operating systems since 1987. The library loads
graphic drivers (*.BGI) and vector fonts (*.CHR) from disk so to provide device
independent graphics support to the programmers.
closegraph()
closegraph deallocates the memory allocated by the graphics system and then
restores the screen to the mode it was in before calling initgraph.
Example: Write a C program to draw a rectangle and circle
#include<graphics.h>
#include<conio.h>
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "path of BGI");
int left=100,top=100,right=200,bottom=200,x= 300,y=150,radius=50;
rectangle(left, top, right, bottom);
circle(x, y, radius);
getch();
closegraph();
return 0;
}
Output:
By Lec. Pratik Chand Page 4
C Programming – CSIT 1st Semester
Graphical function
C graphics using graphics.h functions can be used to draw different shapes, display
text in different fonts, change colors and many more.
There are so many built in graphics functions defined in C library. The basic
graphics functions are described as below.
Graphics functions are text mode graphics functions as well as graphical mode
functions
Text mode graphics functions:
The text mode graphics functions place the text in certain area of the screen. Those
functions are included in the header file <conio.h>. Some of those text mode
graphics functions are:
window(): It sets particular area on the output screen and displays the output text
on that area. The syntax of calling this window function is
window(10,10, 80,25);
where co-ordinate (10,10) represents the upper left corner of displaying window
and (80,25) is the lower right corner of the display window.
clrscr(): It is used to clear the screen and locates the curser at the beginning of the
screen.
gotoxy(x,y): it moves the curser at specified co-ordinates (x,y) position.
cputs(string): it writes a string given to function to the user defined window
screen.
putch(char) : it writes a character given to function to the user defined area of
window.
By Lec. Pratik Chand Page 5
C Programming – CSIT 1st Semester
Graphical mode functions:
There are so many graphical functions in C graphics library. All the graphics mode
functions are in <graphics.h> file. So we must include the graphics.h file to use
those functions. To run a c program in graphical mode the most important
functions are as below.
initgraph(): it is one of the function that is used to initialize the computer in
graphics mode. Its syntax is as
initgraph(&gdriver, &gmode, "graphics driver path");
closegraph(): It is the graphical function that is used to close the graphics mode
initialized by the initgraph() function.
graphresult(): it is a graphical function that returns the value 0 if the graphics is
detected correctly and the driver is correctly initialized to correct graphics mode
other wise it returns some error code than 0.
grapherrormsg(errorcode): This function returns the message string
corresponding to the errorcode returned by the graphresult() function.
cleardevice(): This function is used to clear the screen in graphical mode as
clrscr() in text mode.
After correctly initialized in graphics mode, we can use the different graphical
functions available in the c- library in graphics.h.
Basic Graphic Functions:
crc():
The arc() function is used to create an arc. This arc function is included in
graphics.h library in C which contains methods that can draw figures on the output
screen.
By Lec. Pratik Chand Page 6
C Programming – CSIT 1st Semester
Syntax:
arc(int x, int y, int startangle, int endangle, int radius);
x: type = int, function: defines the x coordinate of the center of the arc.
y: type = int, function: defines the y coordinate of the center of the arc.
start angle: type = int, function: defines the starting angle of arc.
entangle: type = int, function: defines the ending angle of arc.
radius: type = int, function: defines the radius of the arc.
Program:
#include<conio.h>
#include<graphics.h>
void main()
{
int gd = DETECT, gm;
int x = 250, y = 250;
int start_angle = 155, end_angle = 300;
int radius = 100;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
arc(x,y,start_angle,end_angle,radius);
getch();
closegraph();
}
Output:
By Lec. Pratik Chand Page 7
C Programming – CSIT 1st Semester
line():
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.
Syntax:
line(int x1, int y1, int x2, int y2);
Program:
#include<graphics.h>
#include<conio.h>
void main()
{
int gd=DETECT, gm;
int x1 = 100, y1 = 50, x2 = 500, y2=50;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
line(x1,y1,x2,y2);
getch();
By Lec. Pratik Chand Page 8
C Programming – CSIT 1st Semester
closegraph();
}
Output:
circle():
Draw circle with center (x,y) and radius r.
Syntax:
circle(x, y, r)
Program:
#include<graphics.h>
#include<conio.h>
void main()
{
int gd=DETECT, gm;
int x=200, y=200, r=100;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
circle(x,y,r);
getch();
closegraph();
}
By Lec. Pratik Chand Page 9
C Programming – CSIT 1st Semester
Output:
rectangle():
It draw rectangle where (left, top) is upper left coordinate and (right, bottom) is
lower right co-ordinates of rectangle.
Syntax:
rectangle(left,top,right,bottom)
Program:
#include<graphics.h>
#include<conio.h>
By Lec. Pratik Chand Page 10
C Programming – CSIT 1st Semester
void main()
{
int gd=DETECT,gm;
int left = 100, top = 100, right = 200, bottom = 200;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
rectangle(left,top,right,bottom);
getch();
closegraph();
}
Output:
ellipse();
We will draw an eclipse on screen having centre at mid of the screen. We will
use ellipse functions of graphics.h header file to draw eclipse on screen.
Syntax:
ellipse(int xCenter, int yCenter, int startAngle, int endAngle, int xRadius, int
yRadius);
By Lec. Pratik Chand Page 11
C Programming – CSIT 1st Semester
where,
xCenter X coordinate of center of eclipse.
yCenter Y coordinate of center of eclipse.
startAngle Start angle of the eclipse arc.
endAngle End angle of the eclipse arc. It will draw eclipse starting
form startAngle till endAngle.
xRadius Horizontal radius of the eclipse.
yRadius Vertical radius of the eclipse.
Program:
#include<graphics.h>
#include<conio.h>
void main()
{
int gd=DETECT,gm;
int xCenter=200, yCenter=100;
int sAngle=0, eAngle=360;
int xRadius=100, yRadius=50;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
ellipse(xCenter,yCenter,sAngle,eAngle,xRadius,yRadius);
getch();
closegraph();
}
By Lec. Pratik Chand Page 12
C Programming – CSIT 1st Semester
Output:
getmaxx():
This will give the total pixels in x coordinate of screen
getmaxy():
This will give the total pixels in y coordinate of screen
Program:
#include<graphics.h>
#include<conio.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
printf("Max resolution in x cordinate: %d\n",getmaxx());
printf("Max resolution in y cordinate: %d",getmaxy());
getch();
closegraph();
}
By Lec. Pratik Chand Page 13
C Programming – CSIT 1st Semester
Output:
setfillstyle():
This will set the pattern and color to fill in the screen.
Syntax:
setfillstyle(pattern, color)
where, pattern is the style of filling the color
floodfill():
This will fill the color inside the specific shape of screen
Syntax:
floodfill(x, y, border)
where, x and y are fall inside the specific shape. Border indicates boundary of
flooding the color. Actually that is the border of shape.
Below is the table showing INT VALUES corresponding to Colors:
COLOR INT VALUES
-------------------------------
BLACK 0
BLUE 1
GREEN 2
CYAN 3
RED 4
By Lec. Pratik Chand Page 14
C Programming – CSIT 1st Semester
MAGENTA 5
BROWN 6
LIGHTGRAY 7
DARKGRAY 8
LIGHTBLUE 9
LIGHTGREEN 10
LIGHTCYAN 11
LIGHTRED 12
LIGHTMAGENTA 13
YELLOW 14
WHITE 15
Below is the table showing INT VALUES corresponding to Patterns:
PATTERN INT VALUES
-------------------------------
EMPTY_FILL 0
SOLID_FILL 1
LINE_FILL 2
LTSLASH_FILL 3
SLASH_FILL 4
BKSLASH_FILL 5
LTBKSLASH_FILL 6
HATCH_FILL 7
XHATCH_FILL 8
INTERLEAVE_FILL 9
WIDE_DOT_FILL 10
CLOSE_DOT_FILL 11
USER_FILL 12
Program:
#include<graphics.h>
#include<conio.h>
void main()
{
int gd=DETECT,gm;
int xCircle=90, yCircle=120, rCircle=50;
By Lec. Pratik Chand Page 15
C Programming – CSIT 1st Semester
int LeftRec=150, TopRec=50, RightRec=300, BottomRec=200;
int border = RED;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
setcolor(RED);
circle(xCircle,yCircle,rCircle);
setfillstyle(LINE_FILL,BLUE);
floodfill(xCircle,yCircle,border);
rectangle(LeftRec,TopRec,RightRec,BottomRec);
setfillstyle(CLOSE_DOT_FILL,WHITE);
floodfill(155,60,border);
getch();
closegraph();
}
Output:
By Lec. Pratik Chand Page 16
C Programming – CSIT 1st Semester
Print Text:
outtext("text string"): prints the text string in the current position of
screen.
outtextxy(x, y, "string"): prints the string from the co-ordinate (x,y)
position.
Program:
#include<graphics.h>
#include<conio.h>
void main()
{
int gd=DETECT,gm;
int x=20, y=30;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
outtext("this is graphics.");
outtextxy(x,y,"Hello");
getch();
closegraph();
}
Output:
By Lec. Pratik Chand Page 17
C Programming – CSIT 1st Semester
Example 1: WAP to write CSIT inside the circle using graphics
#include<graphics.h>
#include<conio.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
circle(100,100,50);
outtextxy(85,100,"CSIT");
getch();
closegraph();
}
Output:
Example 2: Write a C program to draw a rectangle, circle, line, ellipse and display
text on the bottom of these shapes
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
By Lec. Pratik Chand Page 18
C Programming – CSIT 1st Semester
main()
{
int gd = DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
int left=100,top=100,right=200,bottom=200,x= 300,y=150,radius=50;
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:
End of Unit-11
By Lec. Pratik Chand Page 19