C GRAPHICS PROGRAMS
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.
At the end of our graphics program, we have to unloads the graphics drivers and sets the screen back to text mode by calling closegraph function.
Write a program in C to draw a circle on screen using graphics.h header file
Function | Description |
---|---|
initgraph | It initializes the graphics system by loading the passed graphics driver then changing the system into graphics mode. |
getmaxx | It returns the maximum X coordinate in current graphics mode and driver. |
getmaxy | It returns the maximum Y coordinate in current graphics mode and driver. |
outtextxy | It displays a string at a particular point (x,y) on screen. |
circle | It draws a circle with radius r and centre at (x, y). |
closegraph | It unloads the graphics drivers and sets the screen back to text mode |
C program to draw circle using c graphics
In this program we first initialize graphics mode, by passing graphics driver(DETECT), default graphics mode and specifies the directory path where initgraph looks for graphics drivers (*.BGI). It is the first step you need to do during graphics programming. Setting graphics driver as DETECT, means instructing the compiler to auto detect graphics driver. Here we are using getmaxx and getmaxy function to find the center coordinate of the screen.
/ *************************************************** /
/ / ***********************************************
# include < stdio . h >
# include < conio . h >
# include < graphics . h >
int main ( )
{
int
gd = DETECT,gm;
int
x ,y ,radius=80;
clrscr();
initgraph(&gd, &gm,
"C:\\TC\\BGI"
);
/* Initialize center of circle with center of screen */
x = getmaxx()/2;
y = getmaxy()/2;
outtextxy(x-100, 50,
"CIRCLE Using Graphics in C"
);
/* Draw circle on screen */
circle(x, y, radius);
getch();
closegraph();
return
0;
}
2.C Graphics program to draw a straight line on screen.
In this program initgraph function auto detects an appropriate graphics driver and sets graphics mode maximum possible screen resolution. Then line function draws a straight line from coordinate (100, 100) to (200, 200). Then we added a call to getch function to avoid instant termination of program as it waits for user to press any key. At last, we unloads the graphics drivers and sets the screen back to text mode by calling closegraph function.
/* C graphics program to draw a line */
//*************************************
#include
< stdio . h > #include < conio. h >
#include
< graphics. h >
int
main()
{
int
gd = DETECT, gm;
clrscr( );
/* initialization of graphic mode */
initgraph(&gd, &gm,
"C:\\TC\\BGI"
);
line(100,100,200, 200);
getch();
closegraph();
return
0;
}
3. C Program to Draw a Rectangle and Bar Using C Graphics
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
void main ( )
Read more: https://museacademy-repalle.webnode.in/tutorials/c-programs/loops/
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. |
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
void main ( )
{
Read more: https://museacademy-repalle.webnode.in/tutorials/c-programs/loops/
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
void main ( )
{
Read more: https://museacademy-repalle.webnode.in/tutorials/c-programs/loops/
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
void main ( )
{
Read more: https://museacademy-repalle.webnode.in/tutorials/c-programs/loops/
/************************************************/
//***********************************************
#include
#include
#include
int
main()
{
int
gd = DETECT,gm;
initgraph(&gd, &gm,
"C:\\TC\\BGI"
);
/* Draw rectangle on screen */
rectangle(150, 50, 400, 150);
/* Draw Bar on screen */
bar(150, 200, 400, 350);
getch();
closegraph();
return
0;
}
C Program to Draw an Ellipse Shape Using C Graphics
To draw a complete eclipse, we should pass start and end angle as 0 and 360 respectively.
Function Argument | Description |
---|---|
xCenter | X coordinate of center of ellipse. |
yCenter | Y coordinate of center of ellipse. |
startAngle | Start angle of the eclipse arc. |
endAngle | End angle of the ellipse arc. It will draw ellipse starting form startAngle till endAngle. |
xRadius | Horizontal radius of the ellipse. |
yRadius | Vertical radius of the ellipse. |
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
void main ( )
{
Read more: https://museacademy-repalle.webnode.in/tutorials/c-programs/loops/
In this program we first initialize graphics mode, by passing graphics driver(DETECT), default graphics mode and specifies the directory path where initgraph looks for graphics drivers (*.BGI). First of all we will calculate the center co-ordinates of eclipse which is the center of screen bu calling getmaxx and getmaxy function. Then we draw full eclipse by calling ellipse function.
/*********************************************/
//********************************************
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int
main()
{
int
gd = DETECT,gm ;
int
x ,y;
initgraph(&gd, &gm,
"C:\\TC\\BGI"
);
/* Initialize center of ellipse with center of screen */
x = getmaxx()/2;
y = getmaxy()/2;
outtextxy(x-100, 50,
"ELLIPSE Using Graphics in C"
);
/* Draw ellipse on screen */
ellipse(x, y, 0, 360, 120, 60);
getch();
closegraph();
return
0;
}
C Program to Draw Concentric Circles of Different Colors Using C Graphics
Function | Description |
---|---|
initgraph | It initializes the graphics system by loading the passed graphics driver then changing the system into graphics mode. |
getmaxx | It returns the maximum X coordinate in current graphics mode and driver. |
getmaxy | It returns the maximum Y coordinate in current graphics mode and driver. |
outtextxy | It displays a string at a particular point (x,y) on screen. |
circle | It draws a circle with radius r and centre at (x, y). |
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. |
closegraph | It unloads the graphics drivers and sets the screen back to text mode. |
In this program we first initialize graphics mode, by passing graphics driver(DETECT), default graphics mode and specifies the directory path where initgraph looks for graphics drivers (*.BGI). It is the first step you need to do during graphics programming. Setting graphics driver as DETECT, means instructing the compiler to auto detect graphics driver. Here we are using getmaxx and getmaxy function to find the centre coordinate of the screen and setcolor function to change he colour of drawing.
/**********************************************************/
//**********************************************************
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
int
main()
{
int
gd = DETECT,gm;
int
x ,y;
initgraph(&gd, &gm,
"C:\\TC\\BGI"
);
/* Initialize center of circle with center of screen */
x = getmaxx()/2;
y = getmaxy()/2;
outtextxy(240, 50,
"Concentric Circles"
);
/* Draw circles on screen */
setcolor(RED);
circle(x, y, 30);
setcolor(GREEN);
circle(x, y, 50);
setcolor(YELLOW);
circle(x, y, 70);
setcolor(BLUE);
circle(x, y, 90);
getch();
closegraph();
return
0;
}
Draw Shapes Using C Graphics
/**********************************************************/
//**********************************************************
#include
#include
#include
{
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;
}
C countdown program
#include
#include
#include
int 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;
}
C program to move a car
#include
#include
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);
{
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;
}
getch();
closegraph();
return 0;
}