graphics functions in c

9
1 | Page Graphics Functions in C by Kedar (9890495337) 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 co- ordinates 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 3 rd row and 4 th column.

Upload: kedar-kulkarni

Post on 28-Oct-2014

70 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: Graphics Functions in c

1 | P a g e G r a p h i c s F u n c t i o n s i n C b y K e d a r ( 9 8 9 0 4 9 5 3 3 7 )

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 co-

ordinates 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.

Page 2: Graphics Functions in c

2 | P a g e G r a p h i c s F u n c t i o n s i n C b y K e d a r ( 9 8 9 0 4 9 5 3 3 7 )

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 Color Name

0 BLACK 1 BLUE 2 GREEN 3 CYAN 4 RED 5 MAGENTA 6 BROWN 7 LIGHT GRAY 8 DARK GRAY 9 LIGHT BLUE

10 LIGHT GREEN 11 LIGHT CYAN 12 LIGHT RED 13 LIGHT MAGENTA 14 YELLOW 15 WHITE

128 BLINK

Syntax:textcolor(color); Example:

textcolor("GREEN");

Is equivalent to

int col=2; textcolor(col);

Above both codes displays subsequent texts in green color.

Page 3: Graphics Functions in c

3 | P a g e G r a p h i c s F u n c t i o n s i n C b y K e d a r ( 9 8 9 0 4 9 5 3 3 7 )

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 Color Name

0 BLACK 1 BLUE 2 GREEN 3 CYAN 4 RED 5 MAGENTA 6 BROWN

Syntax:textbackground(color); Example:

int col=4; textbackground(col);

It sets text’s 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");

Page 4: Graphics Functions in c

4 | P a g e G r a p h i c s F u n c t i o n s i n C b y K e d a r ( 9 8 9 0 4 9 5 3 3 7 )

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: -

i. 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 Mode Constant Display Mode

CGA CGAC0 CGAC1 CGAC2 CGAC3 CGSHI

320x200, 4 color, palette 0 320x200, 4 color, palette 1 320x200, 4 color, palette 2 320x200, 4 color, palette 3 640x200, 2 color

EGA EGALO EGAHI

640x200, 16 color 640x350, 16 color

VGA VGALO VGAMED VGAHI

640x200, 16 color 640x350, 16 color 640x480, 16 color

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.

Page 5: Graphics Functions in c

5 | P a g e G r a p h i c s F u n c t i o n s i n C b y K e d a r ( 9 8 9 0 4 9 5 3 3 7 )

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 co-

ordinates 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.

Page 6: Graphics Functions in c

6 | P a g e G r a p h i c s F u n c t i o n s i n C b y K e d a r ( 9 8 9 0 4 9 5 3 3 7 )

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.

Page 7: Graphics Functions in c

7 | P a g e G r a p h i c s F u n c t i o n s i n C b y K e d a r ( 9 8 9 0 4 9 5 3 3 7 )

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:

Page 8: Graphics Functions in c

8 | P a g e G r a p h i c s F u n c t i o n s i n C b y K e d a r ( 9 8 9 0 4 9 5 3 3 7 )

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

Output:

III. Colors: -

i. 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

Page 9: Graphics Functions in c

9 | P a g e G r a p h i c s F u n c t i o n s i n C b y K e d a r ( 9 8 9 0 4 9 5 3 3 7 )

6 LTBKSLASH_FILL 7 HATCH_FILL 8 XHATCH_FILL 9 INTELEAVE_FILL

10 WIDE_DOT_FILL 11 CLOSE_DOT_FILL 12 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 Line Name

0 SOLID_NAME 1 DOTTED_LINE 2 CENTRE_LINE 3 DASHED_LINE 4 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.