Graphic Programing in
C
Prof. Varun Arora
Introduction
• C Graphics programming is very easy and interesting.
• We can use graphics programming for developing games, in making
projects, for animation etc.
• It's not like traditional C programming in which we have to apply
complex logic in our program and then we end up with a lot of errors
and warnings in our program.
• Graphics programming in C is used for drawing various geometrical
shapes (rectangle, circle eclipse etc.), use of mathematical function in
drawing curves, coloring an object with different colors and patterns
and simple animation programs like jumping ball and moving cars.
Set Up in Code Blocks
• In C graphics programming we use standard library functions to get
our task done.
• Just we pass arguments to the functions and it's done.
• Firstly, we use 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 our every
graphics program should include "graphics.h" header file.
Sample graphics code
#include <graphics.h>
#include<conio.h>
void main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
getch();
closegraph();
}
• 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 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 you Turbo C compiler is installed).
• Initgraph function automatically decides an appropriate graphics driver and
mode such that maximum screen resolution is set,
• closegraph function closes the graphics mode.
To draw arc
• 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.
Code to draw an arc
#include <graphics.h>
#include <conio.h>
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
arc(100, 100, 0, 135, 50);
getch();
closegraph();
return 0;
}
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.
Program to draw a bar in C
#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;
}
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.
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;
}
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.
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.
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;
}
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;
}
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.
#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;
}
getbkcolor function in C
• Function getbkcolor returns the current background-color.
• Declaration: int getbkcolor();
• e.g. color = getbkcolor(); // color is an int variable
If current background color is GREEN, color will be 2.
#include <graphics.h>
#include <conio.h>
int 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.
#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;
}
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.
• 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;
}
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.
• 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); /* drawn in white color */
setcolor(RED);
circle(200,200,50); /* drawn in red color */
getch();
closegraph();
return 0;
}
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;
}
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
};
#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;
}
Colors in C Graphics Programming
• There are 16 colors declared in graphics.h header file.
• We use colors to set the current drawing color, change the color of
background, change the color of text, to color a closed shape etc
(Foreground and Background Color).
• To specify a color, we can either use color constants like
setcolor(RED), or their corresponding integer codes like setcolor(4).
Color Codes
Constant Value Background? Foreground?
BLACK 0 Yes Yes
BLUE 1 Yes Yes
GREEN 2 Yes Yes
CYAN 3 Yes Yes
RED 4 Yes Yes
MAGENTA 5 Yes Yes
BROWN 6 Yes Yes
LIGHT GREY 7 Yes Yes
DARK GREY 8 No Yes
LIGHT BLUE 9 No Yes
LIGHT GREEN 10 No Yes
LIGHT CYAN 11 No Yes
LIGHT RED 12 No Yes
LIGHT MAGENTA 13 No Yes
YELLOW 14 No Yes
WHITE 15 No Yes
Assignment
• Write a Program in C to draw the various shapes like line, circle,
rectangle, square, arc, semi-circle, pie chart and bar chart using Switch
statement.
• Draw a Nature Scenery using C graphics functions which should contain
different objects like Sun, Mountains tress etc. as per your choice.
• Using different graphics functions available for text formatting in C
Language, write a C program for displaying text in different sizes,
different colors and different font styles.
• Different graphic Functions are available in C-Language for filling a
given object with colors. Using the graphic functions, write a C program
for filling various closed objects with different colors.