C GRAPHICS PROGRAMS

        This chapter contains lots of fundamental graphics program like drawing of 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. This tutorial will provide you an overview of computer graphics and it's fundamentals.
 
The first step in any graphics program is to initialize the graphics drivers on the computer using initgraph method of graphics.h library.
 

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

In this program, we will draw a circle on screen having centre at mid of the screen and radius of 80 pixels. We will use outtextxy and circle functions of graphics.h header file. Below is the detailed descriptions of graphics functions used in this program.
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

In this program, we will draw a rectangle and a bar on screen. We will use rectangle and bar functions of graphics.h header file to draw rectangle and bar on screen.
 
void rectangle(int xTopLeft, int yTopLeft, int xBottomRight, int yBottomRight);
 
rectangle function draws a rectangle on screen. It takes the coordinates of top left and bottom right corners.
 
void bar(int xTopLeft, int yTopLeft, int xBottomRight, int yBottomRight);
 
bar function draws a rectangle and fill it with current fill pattern and color.

/*******************************************************************/

//******************************************************************

# 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

we will draw an ellipse on screen having centre at mid of the screen. We will use ellipse functions of graphics.h header file to draw eclipse on screen. Below is the detailed descriptions of ellipse function.
 
void ellipse(int xCenter, int yCenter, int startAngle, int endAngle, int xRadius, int yRadius);
 

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

we will draw four circle on screen having centre at mid of the screen and radius 30, 50, 70 and 90 pixels. We will use outtextxy and circle functions of graphics.h header file. Below is the detailed descriptions of graphics functions used in this program.
 
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

This C graphics program draws basic shapes such as circle, line, rectangle, ellipse and display text on screen using C graphics.
 
/**********************************************************/
//**********************************************************
#include
#include
#include
int 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;
}

C countdown program

C graphics program which performs countdown for 30 seconds.


#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

Program in C using graphics to move a car. A car is made using two rectangles and two circles which act as tires of the car. A for loop is used to move the car forward by changing the rectangle and circle coordinates and erasing the previous contents on screen using clearviewport, you can also use cleardevice. Speed of car can be adjusted using delay function, more the delay lesser will be the speed or lesser the delay your car will move fast. In this program color of the car also keeps on changing, this is accomplished by incrementing the color value by one each time in the for loop, you can also use random function for this purpose. Before you see a car moving you will be asked to press a key.
 
#include
#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);

   for (i = 0; i <= 420; i = i + 10, j++)
   {
      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;

      clearviewport();
   }

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