You are on page 1of 24

GRAPHICS.H: To run Graphics program, you need graphics.h header file, graphics.

lib library file and Graphics driver (BGI file) in the program folder. These files are part of Turbo C package. In all our programs we used 640x480 VGA monitor. So all the programs are according to that specification. You need to make necessary changes to your programs according to your screen resolution. For VGA monitor, graphics driver used is EGAVGA.BGI. In graphics mode, all the screen co-ordinates are mentioned in terms of pixels. Number of pixels in the screen decides resolution of the screen. For an example, circle is drawn with xcoordinate of the center 200, y-coordinate 100 and radius 150 pixels. All the coordinates are mentioned with respect to top-left corner of the screen. INITGRAPH: Initializes the graphics mode and clears the screen. Declaration: void far initgraph(int far *graphdriver, int far *graphmode, char far *pathtodriver); Remarks: y y y To start the graphics system, you must first call initgraph. initgraph initializes the graphics system by loading a graphics driver from disk (or validating a registered driver) then putting the system into graphics mode. initgraph also resets all graphics settings (color, palette, current position, viewport, etc.) to their defaults, then resets graphresult to 0.

Arguments: *graphdriver: Integer that specifies the graphics driver to be used. You can give graphdriver a value using a constant of the graphics drivers enumeration type. *graphmode : Integer that specifies the initial graphics mode (unless *graphdriver = DETECT). If *graphdriver = DETECT, initgraph sets *graphmode to the highest resolution available for the detected driver. You can give *graphmode a value using a constant of the graphics_modes enumeration type. pathtodriver : Specifies the directory path where initgraph looks for graphics drivers (*.BGI) first. 1.If they're not there, initgraph looks in the current directory. 2. If pathtodriver is null, the driver files must be in the current directory.

This is also the path settextstyle searches for the stroked character font files (*.CHR). CLOSEGRAPH: closegraph() function switches back the screen from graphcs mode to text mode. It clears the screen also. Shutdown the graphic system. Declaration: Void far closegraph(void); Remarks: y y Closegraph deallocates all memory allocated by the graphic system. It then restores the screen to the mode it was in before you called initgraph.

A graphics program should have a closegraph function at the end of graphics. Otherwise DOS screen will not go to text mode after running the program. Here, closegraph() is called after getch() since screen should not clear until user hits a key. ARC, CIRCLE, PIESLICE: y y y Arc draws a circular arc. Circle draws a circle. Pieslics draws and fills a circular pieslics.

Declaration: void far arc(int x, int y, int stangle, int endangle, int radius); void far circle(int x, int y, int radius); void far pieslice(int x, int y, int stangle, int endangle, int radius); Remarks: arc draws a circular arc in the current drawing color. circle draws a circle in the current drawing color.

pieslice draws a pie slice in the current drawing color, then fills it using the current fill pattern and fill color.

Arguments: (x,y): Center point of arc, circlew, or pie slice stangle: Start angle in degrees endangle: End angle in degrees radius: Radius of arc, circle, and pieslice

Example c program of arc, circle, pieslice #include<graphics.h> #include<conio.h> int main() { int gd = DETECT, gm; initgraph(&gd,&gm,"C:\\TC\\BGI"); arc(100, 100, 0, 135, 50);/* (100, 100) are coordinates of center of the circle and 50 is the radius of circle.*/ circle(100, 100, 50); pieslice(200,200,0,135,100); getch(); closegraph(); return 0; }

Output of program:

ELLIPSE, FILLELLIPSE, SECTOR: y y y Ellips draws an elliptical arc. Fillellipse draws and fills ellipse. Sector draws and fills an elliptical pieslice

Declarations : void ellipse(int x, int y, int stangle, int endangle, int xradius, int yradius); void fillellipse(int x, int y, int xradius, int yradius); void sector( int x, int y, int stangle, int endangle, int xradius, int yradius); Remarks: y y y Ellipse draws an elliptical arc in the current drawing color. Fillellipse draws an elliptical arc in the current drawing color and than fills it with fillcolor and fillpattern. Sector draws an elliptical pie slics in the current drawing color and then fills it using the pattern and color defined by setfillstyle or setfillpattern.

Ellipse is used to draw an ellipse (x,y) are coordinates of center of the ellipse, stangle is the starting angle, end angle is the ending angle, and fifth and sixth parameters specifies the X and Y radius of the ellipse. To draw a complete ellipse strangles and end angle should be 0 and 360 respectively. Argument: y y y y y (x,y)-coordinates of center of the ellipse, xradius x radius of ellipse(Horizontal Axis) yradius -y radius of ellipse (vertical Axis) Stangle-Startinge Angle Endangle-Ending Angle

Example c program of ellipse #include<graphics.h> #include<conio.h> int main() { int gd = DETECT, gm; initgraph(&gd, &gm, "C:\\TC\\BIN"); ellipse(100, 100, 0, 360, 50, 25); fillellipse(100, 100, 50, 25); getch(); closegraph(); return 0; } Output of program:

Example c program of sector: #include<graphics.h> #include<conio.h> int main() { int gd = DETECT, gm; initgraph(&gd, &gm, "C:\\TC\\BIN"); sector(100, 100, 0, 135, 25, 35); getch(); closegraph(); return 0; }

Output of program:

FLOODFILL : Flood-fills a bounded region Declaration : void floodfill(int x, int y, int border); Remarks: y y floodfill an enclosed areaon bitmap device. The area bounded by the color border is flooded with the Current fill pattern and fill color.

y y Arguments: y y

Use fillpoly instead of floodfill wherever possible so you can maintain code compatability with future versions. Floodfill does not work with the IBM-8514 driver.

(x, y) is any point on the screen if (x,y) lies inside the area then inside will be filled otherwise outside will be filled, border specifies the color of boundary of area.

To change fill pattern and fill color use setfillstyle. Code given below draws a circle and then fills it. Example c program of floodfill: #include<graphics.h> #include<conio.h> int main() { int gd = DETECT, gm; initgraph(&gd,&gm,"C:\\TC\\BGI"); setcolor(RED); circle(100,100,50); floodfill(100,100,RED); getch(); closegraph(); return 0; } Output of program:

In the above program a circle is drawn in RED color. Point (100,100) lies inside the circle as it is the center of circle, third argument to floodfill is RED which is color of boundary of circle. So the output of above program will be a circle filled with WHITE color as it is the default fill color.

GETCOLOR , SETCOLOR: SETCOLOR-PARAMETER y y Getcolor returns the current drawing color. Setcolor returns the current drawing color. COLOR BLACK BLUE GREEN CYAN RED MAGENTA BROWN LIGHTGRAY DARKGRAY LIGHTBLUE LIGHTGREEN LIGHTCYAN LIGHTRED LIGHTMAGENTA YELLOW WHITE VALUE 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Declaration: int getcolor(void); void far setcolor(int color); Remarks: y Getcolor function returns the current drawing color. e.g. a = getcolor(); // a is an int variable if current drawing color is WHITE then a will be 15. y y setcolor sets the current drawing color to color, which can range from 0 to getmaxcolor. To set a drawing color with setcolor,you can pass either the the color number or the equivalent color name.

Example c program of getcolor #include<graphics.h> #include<conio.h> int main() { int gd = DETECT, gm; int dwcolor; char a[100]; initgraph(&gd,&gm,"C:\\TC\\BGI"); dwcolor = getcolor(); sprintf(a,"Current drawing color = %d ",dwcolor); outtextxy(0, 0, a); getch(); closegraph(); return 0; }

Example c program of setcolor #include<graphics.h> #include<conio.h> int main() { int gd = DETECT, gm; initgraph(&gd,&gm,"C:\\TC\\BGI"); circle(100,100,50); white color */ setcolor(RED); circle(200,200,50); red color */ getch(); closegraph(); return 0; } /* drawn in

/* drawn in

Output of setcolor:

In Turbo Graphics each color is assigned a number. Total 16 colors are available. Strictly speaking number of available colors depends on current graphics mode and driver.For Example :- BLACK is assigned 0, RED is assigned 4 etc. setcolor function is used to change the current drawing color.e.g. setcolor(RED) or setcolor(4) changes the current drawing color to RED. Remember that default drawing color is WHITE. LINE ,LINEREL,LINETO: y y y Line draws a line between two specified points. Linerel draws a line relative distance from current position(cp). Lineto draws a line from the current position(cp ) to (x,y).

Declaration : void line(int x1, int y1, int x2, int y2); void far lineto(int x,int y); Remarks: y line draws a line from a point(x1,y1) to point(x2,y2) using current color,line style and thickness i.e. (x1,y1) and (x2,y2) are end points of the line.It does not update the current position(cp). linerel function draws a line from the current position(CP) to a point that is a relative distance (x, y) from the CP, then advances the CP by (x, y). You can use getx and gety to find the current position. lineto function draws a line from current position(CP) to the point(x,y),then moves the cp to(x,y). you can get current position using getx and gety function.

Return Value: y None

Example c program of line: #include<graphics.h> #include<conio.h> int main() { int gd = DETECT, gm; initgraph(&gd,&gm,"C:\\TC\\BGI"); line(100, 100, 200, 200); /* draws a line from (100, 100) to
(200, 200).*/

Example c program of lineto, linerel: #include<graphics.h> #include<conio.h> int main() { int gd = DETECT, gm; initgraph(&gd,&gm,"C:\\TC\\BGI"); moveto(100, 100); lineto(200, 200); moveto(250, 250); linerel(100, -100); getch(); closegraph(); return 0; }

getch(); closegraph(); return 0; } Output of program:

RECTANGLE, DRAWPOLY, FILLPOLY: Draws a rectangle in graphics mode. Declaration: void far rectangle(int left, int top, int right, int bottom); void far drawpoly(int numpoints, int far *polypoints); void far fillpoly(int numpoints, int far *polypoints); Remarks: rectangle draws a rectangle in the current line style, thickness, and drawing color. To draw a square use rectangle with same height and width. drawpoly draws a polygon using the current line style and color.

fillpoly draws the outline of a polygon using the current line style and color, then fills the polygon using the current fill pattern and fill color.

Arguments: (left,top) is the upper left corner of the rectangle, and (right,bottom) is its lower right corner. numpoints: Specifies number of points *polypoints: Points to a sequence of (numpoints x 2) integers. Each pair of integers gives the x and y coordinates of a point on the polygon. Numpoints indicates (n+1) number of points where n is the number of vertices in a polygon, polypoints points to a sequence of (n*2) integers . Each pair of integers gives x and y coordinates of a point on the polygon. We specify (n+1) points as first point coordinates should be equal to (n+1)th to draw a complete figure. Return Value: y None Example c program of drawpoly, fillpoly, rectangle: #include<graphics.h> #include<conio.h> int main() { int gd = DETECT, gm; int points[] = { 320, 150, 420, 300, 250, 300, 320, 150}; /* points array contains coordinates of triangle which are (320, 150), (420, 300) and (250, 300). Note that last point(320, 150) in array is same as first. */ initgraph(&gd, &gm, "C:\\TC\\BIN"); drawpoly(4, points); fillpoly(4, points); rectangle(100,100,200,200); getch(); closegraph(); return 0; } Output of the program:

SETLINESTYLE Sets the current line style and width or pattern

Declaration: void far setlinestyle(int linestyle, unsigned upattern, int thickness); Remarks: setlinestyle sets the style for all lines drawn by line, lineto, rectangle,drawpoly, etc. Enum: Line styles for getlinesettings and setlinestyle. Name SOLID_LINE DOTTED_LINE CENTER_LINE DASHED_LINE USERBIT_LINE Value 0 1 2 3 4 Meaning Solid line Dotted line Centered line Dashed line User-defined line style

Enum: Line widths for getlinesettings and setlinestyle. Name NORM_WIDTH THICK_WIDTH Value 1:1 3:3 Meaning pixel wide pixels wide

SETFILLSTYLE , SETBKCOLOR: Declaration: void far setfillstyle(int pattern, int color); void far setbkcolor(int color); Remarks: setfillstyle sets the current fill pattern and fill color. setbkcolor sets the background to the color specified by color.

Example c program of setfillstyle #include<graphics.h> #include<conio.h> int main() { int gd = DETECT, gm; initgraph(&gd, &gm, "C:\\TC\\BGI"); setfillstyle(XHATCH_FILL, RED); circle(100, 100, 50); floodfill(100, 100, WHITE); getch(); closegraph(); return 0; } Output of program: Setfillstyle- parameter (pattern) Names Value Means Fill With... EMPTY_FILL 0 Background color SOLID_FILL 1 Solid fill LINE_FILL 2 --LTSLASH_FILL 3 /// SLASH_FILL 4 ///, thick lines BKSLASH_FILL 5 \\\, thick lines LTBKSLASH_FILL 6 \\\ HATCH_FILL 7 Light hatch XHATCH_FILL 8 Heavy crosshatch INTERLEAVE_FILL 9 Interleaving lines WIDE_DOT_FILL 10 Widely spaced dots CLOSE_DOT_FILL 11 Closely spaced dots USER_FILL 12 User-defined fill pattern

BAR AND BAR3D : Declaration :void bar(int left, int top, int right, int bottom); void bar3d(int left, int top, int right, int bottom, int depth, int topflag); Remarks: Bar function is used to draw a 2-dimensional, rectangular filled in bar . Coordinates of left top and right bottom corner are required to draw the bar.

Arguments: y y left specifies the X-coordinate of top left corner, top specifies the Y-coordinate of top left corner,

y y y y

right specifies the X-coordinate of right bottom corner, bottom specifies the Y-coordinate of right bottom corner. depth specifies the depth of bar in pixels, topflag determines whether a 3 dimensional top is put on the bar or not ( if it is non-zero then it is put otherwise not ).

Current fill pattern and fill color is used to fill the bar. To change fill pattern and fill color use setfillstyle. Example c program of bar #include<graphics.h> #include<conio.h> main() { int gd = DETECT, gm; initgraph(&gd,&gm,"C:\\TC\\BGI"); bar(100, 100, 200, 200); bar3d(100, 100, 200, 200, 20, 1); getch(); closegraph(); return 0; } Output of program: and bar3d

CLEARDEVICE : Declaration : void cleardevice(); Remarks: cleardevice function clears the screen in graphics mode and sets the current position to (0,0). Clearing the screen consists of filling the screen with current background color.

Example c program of cleardevice #include<graphics.h> #include<conio.h> int main() { int gd = DETECT, gm; initgraph(&gd,&gm,"C:\\TC\\BGI"); outtext("Press any key to clear the screen."); getch(); cleardevice(); outtext("Press any key to exit..."); getch(); closegraph(); return 0; } Note : Don't use clrscr in graphics mode. GETBKCOLOR : Declaration : int getbkcolor(); Remarks: getbkcolor function returns the current background color e.g. color = getbkcolor(); // color is an int variable if current background color is GREEN then color will be 2. Example c program of getbkcolor #include<graphics.h> #include<conio.h> int main() { int gd = DETECT, gm; int bkcolor; char a[100]; initgraph(&gd,&gm,"C:\\TC\\BGI");

bkcolor = getbkcolor(); sprintf(a,"Current background color = %d",bkcolor); outtextxy(0, 0, a); getch(); closegraph(); return 0; } GETPIXEL, PUTPIXEL : y y Getpixel gets the color of a specified pixel. Putpixel places a pixel at a specified point.

Declaration : int getpixel(int x, int y); void putpixel(int x, int y, int color); Remarks: y y Getpixel gets the color of pixel located at (x, y). Putpixel plot a point in the color defined at location(x, y).

Return: y getpixel returns the color of the given pixel present at location(x, y). y putpixel does not return. Example c program of getpixel, Putpixel #include<graphics.h> #include<conio.h> int main() { int gd = DETECT, gm; int color; char array[50]; initgraph(&gd,&gm,"C:\\TC\\BGI"); color = getpixel(0, 0); sprintf(array,"color of pixel at (0,0) = %d",color); outtext(array);

/* As we haven't drawn anything on screen and by default screen is BLACK, therefore color of pixel at (0,0) is BLACK. So output of program will be color of pixel at (0,0) is 0, as 0 indicates BLACK color.*/ putpixel(25, 25, RED); // Output of this program will be a RED pixel on screen at (25, 25). /* For example if we want to draw a GREEN color pixel at (35, 45) then we will write putpixel(35, 35, GREEN); in our c program, putpixel function can be used to draw circles, lines and ellipses using various algorithms.*/ getch(); closegraph(); return 0; } GETX , GETY: Declaration : int getx(); int gety(); Remarks: getx function returns the X coordinate of current position. gety function returns the y coordinate of current position. Example c program of getx #include<graphics.h> #include<conio.h> int main() { int gd = DETECT, gm; char array[100]; initgraph(&gd, &gm, "C:\\TC\\BIN"); sprintf(array, "Current position of x = %d",getx()); outtext(array); getch(); closegraph(); return 0; } } Example c program of gety #include<graphics.h> #include<conio.h> int main() { int gd = DETECT, gm; char message[100]; initgraph(&gd, &gm, "C:\\TC\\BIN"); sprintf(message, "Current position of y = %d",gety()); outtext(message); getch(); closegraph(); return 0;

MOVETO, MOVEREL: Declaration :void moveto(int x, int y); void moverel(int x, int y); Remarks: moveto function changes the current position (CP) to (x, y) moverel function moves the current position to a relative distance. Example c program of moverel: #include<graphics.h> #include<conio.h> int main() { int gd = DETECT, gm; int x, y; char message[100]; initgraph(&gd, &gm, "C:\\TC\\BIN"); moveto(100, 100); moverel(100, -100); x = getx(); y = gety(); sprintf(message, "Current x position = %d and y position = %d", x, y); outtextxy(10, 10, message); getch(); closegraph(); return 0; }

Example c program of moveto: #include<graphics.h> #include<conio.h> int main() { int gd = DETECT, gm; char msg[100]; initgraph(&gd, &gm, "C:\\TC\\BIN"); sprintf(msg, "X = %d, Y = %d",getx(),gety()); outtext(msg); moveto(50, 50); sprintf(msg, "X = %d, Y = %d", getx(), gety()); outtext(msg); getch(); closegraph(); return 0; }

OUTTEXT AND OUTTEXTXY FUNCTION: Declaration :

void outtext(char *string); void outtextxy(int x, int y, char *string); Remarks: y y outtext function displays text at current position. outtextxy function display text or string at a specified point(x,y) on the screen.

Arguments: y y x, y are coordinates of the point and third argument contains the address of string to be displayed.

Example c program of outtext: #include<graphics.h> #include<conio.h> int main() { int gd = DETECT, gm; initgraph(&gd,&gm,"C:\\TC\\BGI"); outtext("To display text at a particular position on the screen use outtextxy"); outtextxy(100, 100, "Outtextxy function");

getch(); closegraph(); return 0; } Do not use text mode functions like printf, gotoxy etc while working in graphics mode. Also note '\n' or other escape sequences do not work in graphics mode. You have to ensure that the text doesn't go beyond the screen while using outtext.

PUTIMAGE : Declaration: void putimage(int left, int top, void *ptr, int op);

Remarks: putimage function outputs a bit image onto the screen. putimage puts the bit image previously saved with getimage back onto the screen,

Arguments: y y y with the upper left corner of the image placed at (left, top). ptr points to the area in memory where the source image is stored. The op argument specifies a operator that controls how the color for each destination pixel on screen is computed, based on pixel already on screen and the corresponding source pixel in memory.

SETTEXTSTYLE: Declaration :void settextstyle( int font, int direction, int charsize); Remarks: settextstyle function is used to change the way in which text appears, using it we can modify the size of text, change direction of text and change the font of text. Argument: y y font argument specifies the font of text, Direction can be HORIZ_DIR (Left to top) or VERT_DIR (Bottom to top).

Example c program of settextstyle: #include<graphics.h> #include<conio.h> int main() { int gd = DETECT, gm; int x = 25, y = 25; int font = 0; initgraph(&gd,&gm,"C:\\TC\\BIN"); for ( font = 0 ; font < 9 ; font++) { settextstyle(font, HORIZ_DIR, 1); outtextxy(x, y, "Text with different fonts"); y = y + 25; }

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

TEXTHEIGHT, TEXTWIDTH : Declaration : int textheight(char *string); int textwidth(char *string); Remarks: Textheight function returns the height of a string in pixels. Textwidth function returns the width of a string in pixels. Example c program of textwidth #include<graphics.h> #include<conio.h> int main()

Example c program of textheight #include<graphics.h> #include<conio.h> int main()

{ int gd = DETECT, gm; int height; char arr[100]; initgraph(&gd, &gm, "C:\\TC\\BGI"); height = textheight("C programming"); sprintf(arr,"Textheight = %d",height); outtext(arr); getch(); closegraph(); return 0; }

{ int gd = DETECT, gm; int width; char arr[100]; initgraph(&gd, &gm, "C:\\TC\\BGI"); width = textwidth("programming"); sprintf(arr,"Textwidth = %d",width); outtext(arr); getch(); closegraph(); return 0; }

GRAPHDEFAULTS : Graphdefaults function resets all graphics settings to their defaults. Declaration : void graphdefaults(); Remarks: It resets the following graphics settings :y y y y y

Sets the viewport to the entire screen. moves the current position to (0,0). sets the default palette colors, background color, and drawing color. sets the default fill style and pattern. sets the default text font and justification.

C program of graphdefaults #include<graphics.h> #include<conio.h>

int main() { int gd = DETECT, gm; initgraph(&gd, &gm, "C:\\TC\\BIN"); setcolor(RED); setbkcolor(YELLOW); circle(250, 250, 50); /* we have first changed the drawing color to RED and background color to YELLOW and then drawn a circle with (250, 250) as center and 50 as radius. */

getch(); graphdefaults(); /* When the user will press a key graphdefaults is called and both drawing and background color will be reset to their default values */ getch(); closegraph(); return 0; } Basic Shapes and Colors: /*shapes.c*/ #include<graphics.h> #include<conio.h> void main() { int gd=DETECT, gm; int poly[12]={350,450, 350,410, 430,400, 350,350, 300,430, 350,450 }; initgraph(&gd, &gm, "");

circle(100,100,50); outtextxy(75,170, "Circle"); rectangle(200,50,350,150); outtextxy(240, 170, "Rectangle"); ellipse(500, 100,0,360, 100,50);

outtextxy(480, 170, "Ellipse"); line(100,250,540,250); outtextxy(300,260,"Line");

sector(150, 400, 30, 300, 100,50); outtextxy(120, 460, "Sector"); drawpoly(6, poly); outtextxy(340, 460, "Polygon"); getch(); closegraph(); }

Sample Code to explain Moveto and Lineto: #include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> main( ) { int gd = DETECT, gm ; char msg[80] ; initgraph ( &gd, &gm, "c:\\tc\\bgi" ) ; outtextxy ( 100, 0, "Demonstration of Moveto, Lineto, Moverel, Linerel" ) ; rectangle ( 0, 10, 639, 479 ) ; line ( 100, 50, 100, 350 ) ; /* draws a line */ moveto ( 300, 50 ) ; /* moves the C.P. */ lineto ( 300, 350 ) ; /* draws a line up to the point */ moverel ( 200, -300 ) ; /* moves the C.P. by relative distance from its current position */ linerel ( 0, 300 ) ; /* draws a line from the C.P. to a point a relative distance away from the current value of C.P. */ outtextxy ( 104, 50, "( 100, 50 )" ) ;

outtextxy ( 104, 350, "( 100, 350 )" ) ; outtextxy ( 90, 375, "Line" ) ; outtextxy ( 304, 50, "( 300, 50 )" ) ; outtextxy ( 304, 350, "( 300, 350 )" ) ; outtextxy ( 280, 375, "Moveto, Lineto" ) ; outtextxy ( 504, 50, "( 500, 50 )" ) ; outtextxy ( 504, 350, "( 500, 350 )" ) ; outtextxy ( 480, 375, "Moverel, Linerel" ) ; getch( ) ; closegraph( ) ; restorecrtmode( ) ; }

You might also like