Graphics in C Programming
What is Graphics Program in C
Graphics program in C involves libraries such as OpenGL, GDI, or Allegro to create 2D and 3D
graphics, multimedia applications, and games. A basic understanding of programming concepts is
required, along with a development environment such as Code::Blocks or Visual Studio. Applications
of graphics programming in C include game development, computer-aided design, visualization and
data analysis, multimedia applications, user interfaces, and animation and special effects.
Libraries of Graphic Program in C
Here are some popular graphic libraries for C programming:
OpenGL: It provides a powerful set of tools for developing 3D graphics applications.
Simple Direct Media Layer: It is a multimedia library created to offer Direct3D and OpenGL low-level
access to hardware for audio, keyboard, mouse, joystick, and graphics.
Allegro: It is a popular multimedia library for C/C++ programming that provides basic functionality for
graphics, sound, and input devices.
GTK+ (GIMP Toolkit): It is a popular open-source widget toolkit for creating graphical user interfaces
(GUIs) that are often used for developing desktop applications.
Cairo: This is a vector graphics library that provides support for a wide range of output devices
including PNG, PDF, and SVG.
SFML (Simple and Fast Multimedia Library): This is a multimedia library that provides a simple
interface for creating 2D and 3D games and multimedia applications.
GLUT (OpenGL Utility Toolkit): It is a utility library for OpenGL that provides a simple API for creating
windows, handling input, and drawing 3D graphics.
Qt: It is a GUI toolkit that provides a comprehensive set of tools for creating desktop and mobile
applications.
Colors of Graphics Program in C
Here, are some of the colors of the graphics program in C
COLOR MACRO INTEGER VALUE
BLACK 0
BLUE 1
GREEN 2
CYAN 3
RED 4
MAGENTA 5
BROWN 6
LIGHT GRAY 7
DARK GRAY 8
LIGHT BLUE 9
LIGHT GREEN 10
LIGHT CYAN 11
Er.Binay Yadav Page 1
Graphics in C Programming
COLOR MACRO INTEGER VALUE
LIGHT RED 12
LIGHT MAGENTA 13
YELLOW 14
WHITE 15
Syntax of Graphics Program in C
initgraph(int *gm, int *gm, char *driverDirectoryPath);
Explanation of Syntax
It initializes the graphics system by loading the passed graphics driver then changing the system into
graphics mode. It also resets or initializes all graphics settings like color, palette, current position etc,
to their default values. Below is the description of input parameters of initgraph function.
graphicsDriver : 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.
graphicsMode : It is a pointer to an integer that specifies the graphics mode to be used. If
*graphdriver is set to DETECT, then initgraph sets *graphmode to the highest resolution available for
the detected driver.
driverDirectoryPath : It specifies the directory path where graphics driver files (BGI files) are
located. If directory path is not provided, then it will seach for driver files in current working directory
directory. In all our sample graphics programs, you have to change path of BGI directory accordingly
where you turbo C compiler is installed.
Example of Graphics Program in C
#include <graphics.h>
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
int gd = DETECT, gm;
char data[] = "C:\\MinGW\\lib\\libbgi.a";
initgraph( & gd, & gm, data);
circle(400, 400, 100);
getch();
closegraph();
return 0;
}
Note: Make sure to install the libraries before running the examples.
Output
Er.Binay Yadav Page 2
Graphics in C Programming
Applications of Graphics Program in C
Graphics programming in C has a wide range of applications. Here are some examples:
Game development: Graphics program is widely used in game development. C-based graphics
libraries such as SDL and Allegro can be used to create 2D and 3D games.
Computer-aided design (CAD): It is used extensively in CAD software to create and manipulate 2D
and 3D designs.
Visualisation and data analysis: It is used to create visualizations of data in fields such as science,
engineering, and finance. This might require producing charts, graphs, and other kinds of
visualizations.
Multimedia applications: It is used in multimedia applications such as video and audio players,
image viewers, and video editors.
User interfaces: It is used to create graphical user interfaces (GUIs) for software applications. This
can include creating buttons, menus, and other interface elements.
Animation and special effects: It is used to create animation and special effects in movies, TV
shows, and other media.
Advantages of Graphics Program in C
Efficient memory management
Cross-platform compatibility
Visually appealing applications
Availability of graphics libraries
Low-level control for advanced graphics.
Disadvantages of Graphics Program in C
Performance optimization.
Limited GUI capabilities.
Lack of built-in features.
Steep learning curve.
Platform-specific issues.
Computer Graphics Programs
Write a Program to draw basic graphics construction like line, circle, arc, ellipse and
rectangle.
#include<graphics.h>
#include<conio.h>
void main()
{
intgd=DETECT,gm;
initgraph (&gd,&gm,"c:\\tc\\bgi");
setbkcolor(GREEN);
printf("\t\t\t\n\nLINE");
line(50,40,190,40);
printf("\t\t\n\n\n\nRECTANGLE");
rectangle(125,115,215,165);
printf("\t\t\t\n\n\n\n\n\n\nARC");
arc(120,200,180,0,30);
printf("\t\n\n\n\nCIRCLE");
circle(120,270,30);
printf("\t\n\n\n\nECLIPSE");
ellipse(120,350,0,360,30,20);
getch();
}
Output
Er.Binay Yadav Page 3
Graphics in C Programming
Write a Program to draw animation using increasing circles filled with different colors
and patterns.
#include<graphics.h>
#include<conio.h>
void main()
{
Int gd=DETECT, gm, i, x, y;
initgraph(&gd, &gm, "C:\\TC\\BGI");
x=getmaxx()/3;
y=getmaxx()/3;
setbkcolor(WHITE);
setcolor(BLUE);
for(i=1;i<=8;i++)
{
setfillstyle(i,i);
delay(20);
circle(x, y, i*20);
floodfill(x-2+i*20,y,BLUE);
}
getch();
closegraph();
}
Output
Er.Binay Yadav Page 4
Graphics in C Programming
Write a Program to make a moving colored car using inbuilt functions.
#include<graphics.h>
#include<conio.h>
int main()
{
Int gd=DETECT,gm, i, maxx, cy;
initgraph(&gd, &gm, "C:\\TC\\BGI");
setbkcolor(WHITE);
setcolor(RED);
maxx = getmaxx();
cy = getmaxy()/2;
for(i=0;i<maxx-140;i++)
{
cleardevice();
line(0+i,cy-20, 0+i, cy+15);
line(0+i, cy-20, 25+i, cy-20);
line(25+i, cy-20, 40+i, cy-70);
line(40+i, cy-70, 100+i, cy-70);
line(100+i, cy-70, 115+i, cy-20);
line(115+i, cy-20, 140+i, cy-20);
line(0+i, cy+15, 18+i, cy+15);
circle(28+i, cy+15, 10);
line(38+i, cy+15, 102+i, cy+15);
circle(112+i, cy+15,10);
line(122+i, cy+15 ,140+i,cy+15);
line(140+i, cy+15, 140+i, cy-20);
rectangle(50+i, cy-62, 90+i, cy-30);
setfillstyle(1,BLUE);
floodfill(5+i, cy-15, RED);
setfillstyle(1, LIGHTBLUE);
floodfill(52+i, cy-60, RED);
delay(10);
}
getch();
closegraph();
return 0;
}
Output
Er.Binay Yadav Page 5
Graphics in C Programming
C Program to Draw Bar Graph Using C
Graphics
Here is a C graphics program to draw bar chart on screen using graphics.h header file. In this
program, we will draw a bar graph on screen. Here, we will use line, setfillstyle and bar
functions of graphics.h header file to draw horizontal and vertical axis and bars on screen.
void line(int x1, int y1, int x2, int y2);
It draws a line from (x1, y1) to (x2, y2).
void setfillstyle(int pattern, int color);
It sets the current fill pattern and fill color.
void bar(int xTopLeft, int yTopLeft, int xBottomRight, int yBottomRight);
bar function draws a rectangle and fill it with current fill pattern and color.
Function Argument Description
xTopLeft X coordinate of top left corner.
yTopLeft Y coordinate of top left corner.
xBottomRight X coordinate of bottom right corner.
yBottomRight Y coordinate of bottom right corner.
C program to draw bar graph using graphics
#include <graphics.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "X:\\TC\\BGI");
settextstyle(BOLD_FONT,HORIZ_DIR,2);
outtextxy(275,0,"BAR GRAPH");
setlinestyle(SOLID_LINE,0,2);
/* Draw X and Y Axis */
line(90,410,90,50);
line(90,410,590,410);
line(85,60,90,50);
line(95,60,90,50);
line(585,405,590,410);
line(585,415,590,410);
outtextxy(65,60,"Y");
outtextxy(570,420,"X");
outtextxy(70,415,"O");
/* Draw bars on screen */
setfillstyle(XHATCH_FILL, RED);
bar(150,80,200,410);
bar(225,100,275,410);
bar(300,120,350,410);
bar(375,170,425,410);
bar(450,135,500,410);
closegraph();
return 0;
}
Er.Binay Yadav Page 6
Graphics in C Programming
Output
C Program to Draw a Hut on Screen Using C Graphics
Here is a C graphics program to draw a hut and color it using graphics.h header file. In
this program, we will draw a hut on screen using line and rectangle function and then fill
it with different patterns and colors. We will use below mentioned graphics functions in
this program.
Function Argument Description
initgraph It initializes the graphics system by loading the passed graphics
driver then changing the system into graphics mode.
setcolor It changes the current drawing colour. Default colour is white.
Each color is assigned a number, like BLACK is 0 and RED is 4.
Here we are using colour constants defined inside graphics.h
header file.
setfillstyle It sets the current fill pattern and fill color.
rectangle It draws a rectangle on screen. It takes the coordinates of top
left and bottom right corners.
line It draws a straight line between two points on screen.
floodfill It is used to fill a closed area with current fill pattern and fill
color. It takes any point inside closed area and color of the
boundary as input.
closegraph It unloads the graphics drivers and sets the screen back to text
mode.
C program to draw a hut and color it using graphics
#include<graphics.h>
int main(){
int gd = DETECT,gm;
initgraph(&gd, &gm, "X:\\TC\\BGI");
/* Draw Hut */
setcolor(WHITE);
rectangle(150,180,250,300);
rectangle(250,180,420,300);
rectangle(180,250,220,300);
line(200,100,150,180);
line(200,100,250,180);
Er.Binay Yadav Page 7
Graphics in C Programming
line(200,100,370,100);
line(370,100,420,180);
/* Fill colours */
setfillstyle(SOLID_FILL, BROWN);
floodfill(152, 182, WHITE);
floodfill(252, 182, WHITE);
setfillstyle(SLASH_FILL, BLUE);
floodfill(182, 252, WHITE);
setfillstyle(HATCH_FILL, GREEN);
floodfill(200, 105, WHITE);
floodfill(210, 105, WHITE);
closegraph();
return 0;
}
Output
Er.Binay Yadav Page 8