Users Online

· Guests Online: 146

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

#C PROGRAMMING LANGUAGE TUTORIALS

 

 

Graphics (graphics.h) - C Programming

 

 

Graphics programming in C used to 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.

1. First graphics program (Draw a line)

#include<graphics.h>
#include<stdio.h>
#include<conio.h>

void main(void) {
    int gdriver = DETECT, gmode;
    int x1 = 200, y1 = 200;
    int x2 = 300, y2 = 300;
    clrscr();
    
    initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
    line(x1, y1, x2, y2);
    getch();
    closegraph();
}

2. Explanation of Code :

The first step in any graphics program is to include graphics.h header file. The graphics.h header file provides access to a simple graphics library that makes it possible to draw lines, rectangles, ovals, arcs, polygons, images, and strings on a graphical window.

The second step is initialize the graphics drivers on the computer using initgraph method of graphics.h library.

void initgraph(int *graphicsDriver, int *graphicsMode, char *driverDirectoryPath);

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 *gdriver is set to DETECT, then initgraph sets *gmode 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 search 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.

We have declared variables so that we can keep track of starting and ending point.

int x1=200, y1=200;
int x2=300, y2=300;

No, We need to pass just 4 parameters to the line function.

line(x1,y1,x2,y2);

line Function Draws Line From (x1,y1) to (x2,y2) .

Syntax : line(x1,y1,x2,y2);

Parameter Explanation

  • x1 - X Co-ordinate of First Point
  • y1 - Y Co-ordinate of First Point
  • x2 - X Co-ordinate of Second Point
  • y2 - Y Co-ordinate of Second Point

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.

3. 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). Below is the color code in increasing order.

CONSTANTVALUEBACKGROUND?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
LIGHTGRAY 7 Yes Yes
DARKGRAY 8 NO Yes
LIGHTBLUE 9 NO Yes
LIGHTGREEN 10 NO Yes
LIGHTCYAN 11 NO Yes
LIGHTRED 12 NO Yes
LIGHTMAGENTA 13 NO Yes
YELLOW 14 NO Yes
WHITE 15 NO Yes
BLINK 128 NO *

***** To display blinking characters in text mode, add BLINK to the foreground color. (Defined in conio.h)

4. Graphics example using color

//Include the graphics header file
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
 
void main()
{
   //Initialize the variables for the graphics driver and mode
   int gd = DETECT, gm;
   clrscr();
   initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");

   //Set the color of the object you want to draw. 
   setcolor(BLUE);

   //Draw an object. For this example,drawing a rectangle using the rectangle function
   rectangle(50,50,100,100);

   getch();

   //unloads the graphics drivers
   closegraph();
}

5. Examples

EXAMPLE STATEMENT FOR GRAPHICS IN C LANGUAGE

 

 

1. Drawing Line in Graphics Mode

 

/**********************************************
 Statement - Drawing Line in Graphics Mode
 Programmer - Vineet Choudhary
 Written For - http://developerinsider.co
 **********************************************/

#include<graphics.h>
#include<stdio.h>
#include<conio.h>

void main(void) {
    int gdriver = DETECT, gmode;
    int x1 = 200, y1 = 200;
    int x2 = 300, y2 = 300;
    clrscr();
    
    initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
    line(x1, y1, x2, y2);
    closegraph();
    getch();
}

/*
 
Explanation of Code :
1. int x1=200, y1=200;
   int x2=300, y2=300;
   We have declared above variables so that we can keep track of starting and ending point.
 
2. line(x1,y1,x2,y2);
   We need to pass just 4 parameters to the line() function.
 
3. Line Function Draws Line From (x1,y1) to (x2,y2) .
    Syntax : line(x1,y1,x2,y2);
    ->Parameter	Explanation
        x1	X Co-ordinate of First Point
        y1	Y Co-ordinate of First Point
        x2	X Co-ordinate of Second Point
        y2	Y Co-ordinate of Second Point
*/

 

 

2. Make Static Countdown

 

 

/**********************************************
 Statement - Static Countdown
 Programmer - Vineet Choudhary
 Written For - http://developerinsider.co
 **********************************************/
#include <graphics.h>
#include <dos.h>
#include <conio.h>
 
int main()
{
   int gd = DETECT, gm, i;
   char a[5];

   initgraph( &gd, &gm, "C:\\TURBOC3\\BGI");

   settextjustify( CENTER_TEXT, CENTER_TEXT );
   settextstyle(DEFAULT_FONT,HORIZ_DIR,50);
   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();
   }
   cleardevice();
   settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
   outtextxy(300,300,"Project by http://www.turboc.codeplex.com");
   delay(5000);
   closegraph();
   return 0;
}

 

 

3. Draw Moving a Car

 

/**********************************************
 Statement - Move a Car
 Programmer - Vineet Choudhary
 Written For - http://developerinsider.co
 **********************************************/
#include <graphics.h>
#include <dos.h>
#include <conio.h> 
 
main()
{
   int i, j = 0, gd = DETECT, gm;
 
   initgraph(&gd,&gm,"C:\\TURBOC3\\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();
   cleardevice();
   settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
   outtextxy(100,200,"Project by http://www.turboc.codeplex.com");
   delay(5000);
   closegraph();
   return 0;
}

 

 

4. Press Me Button Game

 

 

/**********************************************
 Statement - Press Me Button Game	
 Programmer - Vineet Choudhary
 Written For - http://developerinsider.co
 **********************************************/
 
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <graphics.h>
#include <stdlib.h>
 
union REGS i, o;
int left = 265, top = 250;
 
void initialize_graphics_mode()
{
  int gd = DETECT, gm, error;
 
  initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
 
  error = graphresult();
 
  if (error != grOk)
  {
    perror("Error ");
    printf("Press any key to exit...\n");
    getch();
    exit(EXIT_FAILURE);
  }
}
 
void showmouseptr()
{
  i.x.ax = 1;
  int86(0x33,&i,&o);
}
 
void hidemouseptr()
{
  i.x.ax = 2;
  int86(0x33,&i,&o);
}
 
void getmousepos(int *x,int *y)
{
  i.x.ax = 3;
  int86(0x33,&i,&o);
 
  *x = o.x.cx;
  *y = o.x.dx;
}
 
void draw_bar()
{
  hidemouseptr();
  setfillstyle(SOLID_FILL,CYAN);
  bar(190,180,450,350);
  showmouseptr();
}
 
void draw_button(int x, int y)
{
  hidemouseptr();
  setfillstyle(SOLID_FILL,MAGENTA);
  bar(x,y,x+100,y+30);
  moveto(x+5,y);
  setcolor(YELLOW);
  outtext("Press me");
  showmouseptr();
}
 
void draw()
{
  settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
  outtextxy(100,451,"Project by http://www.turboc.codeplex.com");
  setcolor(BLUE);
  rectangle(0,0,639,450);
  setcolor(RED);
  outtextxy(160,25,"Try to press the \"Press me\" button");
  outtextxy(210,50,"Press escape key to exit");
  setfillstyle(XHATCH_FILL,GREEN);
  setcolor(BLUE);
  bar(1,1,75,449);
  bar(565,1,638,449);
  showmouseptr();
  draw_bar();
  draw_button(left,top);
}
 
void initialize()
{
  initialize_graphics_mode();
 
  if( !initmouse() )
  {
    closegraph();
    printf("Unable to initialize the mouse");
    printf("Press any key to exit...\n");
    getch();
    exit(EXIT_SUCCESS);
  }
 
  draw();
}
 
int initmouse()
{
  i.x.ax = 0;
  int86(0x33,&i,&o);
  return ( o.x.ax );
}
 
void get_input()
{
  int x, y;
 
  while(1)
  {
    getmousepos(&x,&y);
 
    /* mouse pointer in left of button */
 
    if( x >= (left-3) && y >= (top-3) && y <= (top+30+3) && x < left )
    {
      draw_bar();
      left = left + 4;
 
      if (left > 350)
        left = 190;
 
      draw_button(left,top);
    }
 
    /* mouse pointer in right of button */
 
    else if (x<=(left+100+3)&&y>=(top-3)&&y<=(top+30+3)&&x>(left+100))
    {
      draw_bar();
      left = left - 4;
 
      if (left < 190)
        left = 350;
 
      draw_button(left,top);
    }
 
    /* mouse pointer above button */
 
    else if(x>(left-3) && y>=(top-3) && y<(top) && x<= (left+100+3))
    {
      draw_bar();
      top = top + 4;
 
      if (top > 320)
        top = 180;
 
      draw_button(left,top);
    }
 
    /* mouse pointer below button */
 
    else if (x>(left-3)&&y>(top+30)&&y<=(top+30+3)&&x<=(left+100+3))
    {
      draw_bar();
      top = top - 4;
 
      if (top < 180)
        top = 320;
 
      draw_button(left,top);
    }
 
    if (kbhit())
    {
      if (getkey() == 1)
        exit(EXIT_SUCCESS);
    }
  }
}
 
int getkey()
{
  i.h.ah = 0;
  int86(22,&i,&o);
 
  return( o.h.ah );
}
 
main()
{
  initialize();
 
  get_input();
  return 0;
}

 

 

5. Draw Smiling Face Animation

 

 

/**********************************************
 Statement - Smiling Face Animation	
 Programmer - Vineet Choudhary
 Written For - http://developerinsider.co
 **********************************************/
 
#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
 
main()
{
   int gd = DETECT, gm, area, temp1, temp2, left = 25, top = 75;
   void *p;
 
   initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
 
   setcolor(YELLOW);
   circle(50,100,25);
   setfillstyle(SOLID_FILL,YELLOW);
   floodfill(50,100,YELLOW);
 
   setcolor(BLACK);
   setfillstyle(SOLID_FILL,BLACK);
   fillellipse(44,85,2,6);
   fillellipse(56,85,2,6);
 
   ellipse(50,100,205,335,20,9);
   ellipse(50,100,205,335,20,10);
   ellipse(50,100,205,335,20,11);
 
   area = imagesize(left, top, left + 50, top + 50);
   p = malloc(area);
 
   setcolor(WHITE);
   settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
   outtextxy(20,451,"Smiling Face Project by http://www.turboc.codeplex.com");
 
   setcolor(BLUE);
   rectangle(0,0,639,449);
 
   while(!kbhit())
   {
      temp1 = 1 + random ( 588 );
      temp2 = 1 + random ( 380 );
 
      getimage(left, top, left + 50, top + 50, p);
      putimage(left, top, p, XOR_PUT);
      putimage(temp1 , temp2, p, XOR_PUT);
      delay(500);
      left = temp1;
      top = temp2;
   }
 
   getch();
   closegraph();
   return 0;
}

 

 

 

6. Make Traffic Light Simulation

/**********************************************
 Statement - Traffic Light Simulation
 Programmer - Vineet Choudhary
 Written For - http://developerinsider.co
 **********************************************/

#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>

main()
{
   int gd = DETECT, gm, midx, midy;

   initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");

   midx = getmaxx()/2;
   midy = getmaxy()/2;

   setcolor(RED);
   settextstyle(SCRIPT_FONT, HORIZ_DIR, 3);
   settextjustify(CENTER_TEXT, CENTER_TEXT);
   outtextxy(midx, midy-10, "Traffic Light Simulation");
   outtextxy(midx, midy+10, "Press any key to start");
   getch();
   cleardevice();
   setcolor(WHITE);
   settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
   rectangle(midx-30,midy-80,midx+30,midy+80);
   circle(midx, midy-50, 22);
   setfillstyle(SOLID_FILL,RED);
   floodfill(midx, midy-50,WHITE);
   setcolor(BLUE);
   outtextxy(midx,midy-50,"STOP");
   delay(2000);
   graphdefaults();
   cleardevice();
   setcolor(WHITE);
   rectangle(midx-30,midy-80,midx+30,midy+80);
   circle(midx, midy, 20);
   setfillstyle(SOLID_FILL,YELLOW);
   floodfill(midx, midy,WHITE);
   setcolor(BLUE);
   outtextxy(midx-18,midy-3,"READY");

   delay(2000);
   cleardevice();
   setcolor(WHITE);
   rectangle(midx-30,midy-80,midx+30,midy+80);
   circle(midx, midy+50, 22);
   setfillstyle(SOLID_FILL,GREEN);
   floodfill(midx, midy+50,WHITE);
   setcolor(BLUE);
   outtextxy(midx-7,midy+48,"GO");
   setcolor(RED);
   settextstyle(SCRIPT_FONT, HORIZ_DIR, 4);
   outtextxy(midx-150, midy+100, "Press any key to exit...");
   getch();
   clrscr();
   cleardevice();
   setcolor(RED);
   settextstyle(SCRIPT_FONT, HORIZ_DIR, 3);
   settextjustify(CENTER_TEXT, CENTER_TEXT);
   outtextxy(midx, midy, "Project by http://www.turboc.codeplex.com");
   delay(5000);
   closegraph();
   return 0;
}




Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 0.78 seconds
10,807,774 unique visits