Vous êtes sur la page 1sur 9

GRAPHICS FUNCTIONS IN C GRAPHICS MODES: There are basically two types of graphics modes: 1. Text Mode 2.

Graphic Mode There are different graphics functions (graphics primitives) supported by C available in these two modes useful for drawing different effective texts and different types of geometric shapes.

1. Text Mode Graphics Functions: In text mode, the screen is divided into characters positions. The screen is mapped as number of rows into number of columns, which generally reflects the resolution of screen. Typically, the screen is divided in 50 columns and 25 rows. There are other resolutions also available. The graphics functions of text mode are as follows: i. window(): - This function specifies a window on screen. The four integer coordinates of the window are passed as parameters to this function. Syntax:window(left,top,right,bottom); ii. putch(): - It displays a single character at cursor position. Syntax:putch(char); Example:putch('A'); Displays the character A at specified cursor position (where cursor position will be whatever current by default position is) iii. clrscr(): - It clears the entire screen and locates the cursor In top left corner of screen i.e. (1,1). Syntax:clrscr(); iv. gotoxy(): - It positions the cursor to the specified location on screen, where location is specified by the x, y co-ordinates of the point. Syntax:gotoxy(x,y); Where, x, y is co-ordinates of a point where cursor is to be positioned. Example:gotoxy(3,4); It positions the cursor to 3rd row and 4th column.
1|Page Graphics Functions in C by Kedar (9890495337)

v.

puts(): - It displays string at cursor position. Syntax:puts(s1); Where, s1 is string to be displayed at specified cursor position. For puts() and putch() functions, gotoxy() can be used. Example:
gotoxy(3,4); puts("Hello"); gotoxy(10,10); putch('A');

Here, Hello is displayed starting from the position (3,4) and A is displayed at (10,10). vi. textcolor(): - It sets the color for the text. Any text displayed after this command will be displayed in a color specified by this command. The supported colors are numbered from 0 to 15. Color Constants 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 128 Color Name BLACK BLUE GREEN CYAN RED MAGENTA BROWN LIGHT GRAY DARK GRAY LIGHT BLUE LIGHT GREEN LIGHT CYAN LIGHT RED LIGHT MAGENTA YELLOW WHITE BLINK

Syntax:textcolor(color); Example:
textcolor("GREEN");

Is equivalent to
int col=2; textcolor(col);

Above both codes displays subsequent texts in green color.

2|Page

Graphics Functions in C by Kedar (9890495337)

vii.

delline(): - It deletes a line specified by cursor position. After deletion, all subsequent lines will be pushed up by one line. Syntax:delline(); Example:
gotoxy(8,4); delline();

Above statement deletes 8th line. viii. inline(): - It inserts a blank line at current cursor position. Syntax:inline(); Example:
gotoxy(8,4); inline();

It inserts a line at 8th row. ix. textbackground(): - It changes background color of text. The valid color for CGA are from 0 to 6. They are: Constant 0 1 2 3 4 5 6 Color Name BLACK BLUE GREEN CYAN RED MAGENTA BROWN

Syntax:textbackground(color); Example:
int col=4; textbackground(col);

It sets texts background color to red. x. moveto(): - It moves cursor to the location specified by int (x,y) co-ordinates. Syntax:moveto(x,y); xi. outtextxy(): - It displays text within quotation mark at specified position and with latest setcolor style. Syntax:outtextxy("sentence"); OR outtextxy(x,y,"sentence");
3|Page Graphics Functions in C by Kedar (9890495337)

2. Graphics Functions of Graphics Mode: In graphics mode, we can display different effective text as well as we can draw different graphical figures. By default, system is in text mode, but if we want to draw some graphical figures, then we have to work in graphics mode. Once working in this mode is over, it is general practice to close this mode. Some of the basic graphics functions are as follows: I. Basic Graphics Mode Functions: initgraph(): - it is used to initialize graphics mode. Syntax:initgraph(int driver, int mode, char path); Where, driver: This argument specifies the graphics driver to be used and it interfaces with display adapter. Some of the available graphics drivers are CGA, EGA, VGE, etc. mode: Each graphic adapter can use several different possible graphics modes. The mode argument is used to select particular mode. Following table shows different possible modes for CGA, VGA, EGA, etc. Driver Selected CGA Mode Constant CGAC0 CGAC1 CGAC2 CGAC3 CGSHI EGALO EGAHI VGALO VGAMED VGAHI Display Mode 320x200, 4 color, palette 0 320x200, 4 color, palette 1 320x200, 4 color, palette 2 320x200, 4 color, palette 3 640x200, 2 color 640x200, 16 color 640x350, 16 color 640x200, 16 color 640x350, 16 color 640x480, 16 color

i.

EGA VGA

path: it specifies path to graphics driver. Graphics drivers are files with BGI file extension supplies as part of Turbo C++. The path name is string therefore it must be surrounded by quotes. Example:
initgraph(&gd,&gm,"C:\\TC\\BGI"); intgm,gd=DETECT;

where, gd specifies graphics driver. gm specifies graphics mode. C:\\TC\\BGI specifies paths of BGI files DETECT is macro which automatically selects the driver.

4|Page

Graphics Functions in C by Kedar (9890495337)

ii.

closegraph(): - It is used to close graphics mode. When you exit from graphics mode, you should restore the system to the previous display (text) mode. closegraph() function restores the previous display mode. If you do not use this function and still you exit from graphics mode, system gives some undesirable effects such as loss of cursor or off-sine characters. It is because system tries to write text in graphics mode. Syntax:closegraph(); II. Shapes: -

Computer graphics has many in-built commands, which can be used either to draw a shape and/or for filling a color in any bounded shape. Following commands are available for drawing any basic shape and which are supported by C and C++. i. lineto():- This command draws a line on screen from current cursor position to the (x,y) position mentioned in command. Syntax:lineto(x,y); Where, (x,y) are co-ordinates of end point of line. ii. line(): - This command draws a line on screen. Syntax:line(x1,y1,x2,y2); Where, (x1,y1) are co-ordinates of starting point of line and (x2,y2) are coordinates of end point of line. Example:line(10,10,100,100); It will draw a line from point (10,10) to point (100,100). Output: It draws only a line not a box on screen.

5|Page

Graphics Functions in C by Kedar (9890495337)

iii.

circle(): - This command draws a circle on screen. Syntax: circle(x,y,r); Where, (x, y) are co-ordinates of centre of circle and r is radius of circle. Example: circle(50,50,10); It draws a circle with centre (50,50) and radius 10. Output:

iv.

rectangle(): - This command draws a rectangle on screen. Syntax: rectangle(x1,y1,x2,y2); Where, (x1,y1) are co-ordinates of top-left corner point of rectangle and (x2,y2) are co-ordinates of bottom-right corner point of rectangle. Example: rectangle(10,10,100,100); It will draw a rectangle as shown in following output. Output:

v.

ellipse(): - In draws an ellipse on screen. Syntax: ellipse(x,y,start,end,xrad,yrad); Where, (x, y) are co-ordinates of centre point (start, end) starting and ending angle of ellipse (xrad, yrad) are x-axis and y-axis radius respectively.

6|Page

Graphics Functions in C by Kedar (9890495337)

For full ellipse, the start and end should be 0 and 360 else it will draw an arc on screen. Example: ellipse(100,100,0,360,20,10); Output:

Example: ellipse(100,100,0,360,10,20); Output:

vi.

drawpoly(): - It draws outline of polygon having specified number of sides. Syntax: drawpoly(n,array); Where, n is number of vertices of a polygon+1. Array is integer array name which stores co-ordinates of vertices of a polygon. Example: drawpoly(4,m); Where, m is array which stores co-ordinates of vertices of a triangle. Triangle has three vertices but to close it one vertex is added as a starting and hence number of vertices are 4. As number of vertices are 4 and each vertex has x, y co-ordinates (2 values) and hence size of array is 8. To draw a triangle command will be:

7|Page

Graphics Functions in C by Kedar (9890495337)

int m[8]={100,100,150,150,50,150,100,100}; drawpoly(4,m);

Output:

i.

III. Colors: setcolor(): - it draws any subsequent graphics in a color given in command. Syntax: setcolor(color); Where, color is either color constant or color name as specified in table above. Example:
setcolor(RED); line(10,10,100,100);

It will draw a line in red color. If it is,


line(10,10,100,100); setcolor(RED);

It will not draw line in red color. ii. setfillstyle(): - this command decides the filling pattern and the filling color but it do not actually fill. Syntax: setfillstyle(pattern,color); Where, pattern can be either pattern constant or patter name. These pattern constants are given in following table. Color is the color constant or color name. Pattern Constant Pattern Name 0 EMPTY_FILL 1 SOLID_FILL 2 LINE_FILL 3 LTSLASH_FILL 4 SLASH_FILL 5 BKSLASH_FILL
8|Page Graphics Functions in C by Kedar (9890495337)

6 7 8 9 10 11 12

LTBKSLASH_FILL HATCH_FILL XHATCH_FILL INTELEAVE_FILL WIDE_DOT_FILL CLOSE_DOT_FILL USER_FILL

iii.

setlinestyle(): - It specifies the thickness of the line to be drawn. These styles are not used for the circles. Syntax: setlinestyle(linestyle,user_defined_style,line_width); Where, linestyle gives different style of line as shown in table below: Constant 0 1 2 3 4 Line Name SOLID_NAME DOTTED_LINE CENTRE_LINE DASHED_LINE USERBIT_LINE

User_defined_style is user defined styleand if ignored set to zero. Line_width is tickness of line as given below 0 NORM_WIDTH 3 THICK_WIDTH

Example: setlinestyle(2,0,3); This will draw thick centre line as follows:

iv.

fillpoly(): - This command draws any polygon with n number of vertices and then fill it with current setfillstyle.

Note: - drawpoly and fillpoly both commands draw a polygon with latest current setcolor and setline style. Syntax: fillpoly(n,array); Where, n is number of vertices of a polygon + 1. Array is integer array name which stores co-ordinates of vertices of a polygon.

9|Page

Graphics Functions in C by Kedar (9890495337)

Vous aimerez peut-être aussi