A tutorial on graphics.h - How to use graphics.h


The graphics.h file is used in C language to get basic graphics working in C. 

Position Tracking

The positioning is done from the top left corner as shown in the image below,

Including graphics library

#include<stdio.h>
#include<graphics.h>
void main()
{
}

Loading The Graphics Driver

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode," ");
}

References Of Common Functions

Cleardevice()
Clears screen

gotoxy()
Moves the cursor to a specific location on screen

putpixel()
Puts a small pixel(a very small dot) at the location mentioned in the function.

outtextxy()
Prints a text on the screen.

rectangle()
Draws a rectangle, given its starting and ending diagonal co-ordinates.

circle()
Draws a circle, given the coordinates of centre and the radius.

line()
Draws a line, given the starting and ending co-ordinates of the line.

moveto()
Moves the cursor, from one place to the position mentioned in the function.

lineto()
Draws a line from the current position to the co-ordinates specified.

ellipse()
Draws the ellipse using the specified angles and coordinates.

drawpoly()
Draws a polygon.

settextstyle()
Sets the font style. The available fonts are,


  1. TRIPLEX_FONT
  2. SMALL_FONT
  3. SANS_SERIE_FONT
  4. GOTHIC_FONT
You may change the direction of the text to be displayed horizontally or vertically using HORIZ_DIR or VERT_DIR respectively.


You may also change the size. The size is from 1 to 10.

setfillstyle()

Sets a style to fill up a shape. Styles available are SOLID_FILL, LINE_FILL, HATCH_FILL, SLASH_FILL etc.

setcolor()
Sets the color

floodfill()
Fills up the selected area with color.


itoa()
Converts from integer to alphabet.

delay()
Causes a delay in the execution of the program. The delay is given in miliseconds.

closegraph()
To close the graphics library.



{ 10 comments... read them below or add one }

Bilal Chandio Baloch said...

Farahad brother your efforts are admirable, i found this tutorial very helpful.

Thanks.

Fahad Uddin said...

@Bilal: Brother thankyou very much. That means alot for me. I have collected this site data from various sources.

Prateek Aher said...

i want to create a table for making a sudoku game.
how should i start??

Fahad Uddin said...

@Prateek:
Try making boxes by rectangle() function, then display the text by outtextxy() function.

Anonymous said...

sweet and simple!!

Shanky Kalra said...

simple and to the point

Anonymous said...

delay() is in the dos.h header file i guess!!

Anonymous said...

sir how can i create trigonometric curves using c????
plz help me....!!!!

Fahad Uddin said...

Which trigonometric function? All of them?

Anonymous said...

really helpful thank you sir

Post a Comment

Please Comment Back If The Code Has Helped.