Graphics in Turbo C++
Graphics in Turbo C++
GRAPHICS.H:
This file contains definitions and explanation of all the graphic functions and constants
INITGRAPH( ):
Syntax:
int gd, gm;
gd= DETECT;
initgraph( &gd, &gm, “ “);
To switch from text mode to graphic mode, we have function called as” initgraph”. This function
initializes the graphic mode. It selects the best resolution and directs that value as mode in
variable gm. The two int variables gd, gm are graphic driver and graphic mode respectively. The
gm handles value that tells us which resolution and monitor we are using. The gd specifies the
graphic driver to be used. In our program we have gd=DETECT means we have passed the
highest possible value available for the detected driver. If you don’t want that value then you
have to assign the constant value for gd, gm. The ” &” symbol is used for initgraph to pass
address of the constants. It specifies the directory path where initgraph looks for graphics drivers
(*.BGI) first. If files are not there then initgraph will look for the current directory of your
program.If it is unable to find it within the current working directory then it will pass an error.
You can leave it blank ( ” ” ) if the *.BGI files are within the working directory.
Circle( ) :
Syntax:
circle(center column, center row, radius);
Circle function takes the center point in terms of colulmn and row and the third co-ordinate is
nothing but radius of the circle.
Getch( ) :
getch( ) function gets a character from console but does not show it on screen. This is used to
pause the screen till user hits any key.
Line:
Syntax:
Line(x1, y1, x2,y2);
line draws a line from (x1, y1) to (x2, y2) using the current color, line style, and thickness
Rectangle:
Syntax:
rectangle(int left, int top, int right, int bottom);
rectangle draws a rectangle in the current line style, thickness, and drawing color. (left, top) is
the upper left corner of the rectangle, and (right, bottom) is its lower right corner.
Arc:
Syntax:
arc(int x, int y, int stangle, int endangle, int radius);
arc draws a circular arc in the current drawing color. If stangle = 0 and endangle = 360, the call
to arc draws a complete circle.
Circle:
Syntax:
circle(int x, int y, int radius);
circle draws a circle in the current drawing color.
90
degrees
Ellipse:
Syntax:
ellipse(int x, int y, int stangle, int endangle, int xradius, int yradius);
ellipse draws an elliptical arc in the current drawing color. If stangle = 0 and endangle = 360, the
call to ellipse draws a complete ellipse.
Fillellipse:
Syntax:
fillellipse(int x, int y, int xradius, int yradius);
fillellipse draws and fills an ellipse with the current fill color and fill pattern.
Bar:
Syntax:
bar(left, top, right, bottom);
bar draws a filled-in, rectangular, two-dimensional bar. The bar is filled using the current fill
pattern and fill color. bar does not outline the bar. To draw an outlined two-dimensional bar,
use bar3d with depth = 0.
Parameters What they are
(left, top) the rectangle's upper left corner
(right, bottom) the rectangle's lower right corner
The coordinates are in pixels.
Bar3d:
Syntax:
bar3d(int left, int top, int right, int bottom, int depth, int topflag);
bar3d draws a three-dimensional rectangular bar, then fills it using the current fill pattern and fill
color. The three-dimensional outline of the bar is drawn in the current line style and color.
Parameter What It Is/Does
depth Bar's depth in pixels
topflag Governs whether a three-dimensional top is put on the bar
(left, top) Rectangle's upper left corner
(right, bottom) Rectangle's lower right corner
If topflag is non-zero, a top is put on the bar. If topflag is 0, no top is put on the bar: This makes
it possible to stack several bars on top of one another. To calculate a typical depth for bar3d,
take 25% of the width of the bar, like this:
bar3d(left, top, right, bottom, (right-left)/4, 1);
Setcolor:
Syntax:
setcolor(int color);
setcolor sets the current drawing color to color, which can range from 0 to 15 To select a
drawing color with setcolor you can pass either the color number or the equivalent color name.
BLACK 0
BLUE 1
GREEN 2
CYAN 3
RED 4
MAGENTA 5
BROWN 6
LIGHTGRAY 7
DARKGRAY 8
LIGHTBLUE 9
LIGHTGREEN 10
LIGHTCYAN 11
LIGHTRED 12
LIGHTMAGENTA 13
YELLOW 14
WHITE 15
Setfillstyle
Syntax:
setfillstyle(int pattern, int color);
Sets the fill pattern and color. The enumeration fill_patterns, defined in GRAPHICS.H, gives
names for the predefined fill patterns, plus an indicator for a user-defined 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
All but EMPTY_FILL fill with the current fill color. EMPTY_FILL uses the current
background color.
Floodfill:
Syntax:
floodfill(int x, int y, int border);
Flood-fills a bounded region. The area bounded by the color border is flooded with the current
fill pattern and fill color. (x,y) is a "seed point". If the seed is within an enclosed area, the inside
will be filled. If the seed is outside the enclosed area, the exterior will be filled.
Imagesize
Syntax:
imagesize(int left, int top, int right, int bottom);
imagesize determines the size of the memory area required to store a bit image.