0% found this document useful (0 votes)
19 views

Graphics Programming: Appendix

The document discusses graphics programming in C using graphics libraries. It provides an example program that initializes graphics mode and draws a circle on screen. It explains various graphics functions like circle, rectangle, line, text output etc. and provides their prototypes and usage.

Uploaded by

ccdsfdds few
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Graphics Programming: Appendix

The document discusses graphics programming in C using graphics libraries. It provides an example program that initializes graphics mode and draws a circle on screen. It explains various graphics functions like circle, rectangle, line, text output etc. and provides their prototypes and usage.

Uploaded by

ccdsfdds few
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

518 Programming in C

Appendix

E Graphics Programming

The C programming language has a good collection of The function initgraph initializes the graphics system
graphics libraries. To start with graphics programming, by loading a graphics driver from disk (or validating a
let us write a small program that displays a circle on the registered driver) and then putting the system into graphics
screen. mode. The function resets all graphics settings (color,
palette, current position, viewport, etc.) to their defaults
#include < graphics.h>
and then resets graphresult to 0.
#include < conio.h>
*graphdriver is an integer that specifies the graphics
main()
driver to be used. graphdriver can be assigned a value
{
using a constant of the graphics drivers enumeration type.
int gd = DETECT, gm;
*graphmode is an integer that specifies the initial
initgraph(&gd, &gm, "c:\\turboc3\\bgi");
circle(250, 150, 100);
graphics mode (unless *graphdriver = DETECT). If
getch(); *graphdriver = DETECT, initgraph sets *graphmode
closegraph(); to the highest resolution available for the detected driver.
} *graphmode can be assigned a value using a constant of
the graphics_modes enumeration type.
To run this program, you must include "graphics.h" pathtodriver is used to specify the directory path
header file, graphics.lib library file, and Graphics where initgraph looks for graphics drivers (*.BGI)
driver (BGI file) in the program folder. These files are part first. If the driver is not present there, initgraph looks in
of Turbo C package. In the programs in this annexure we the current directory. If pathtodriver is null, the driver
will be using 640 × 480 VGA monitor, so the programs are files must be in the current directory.
according to that specification. If your screen resolution closegraph() function clears the screen and switches
is different then you must make the necessary changes to back the screen from graphics mode to text mode. Any
your programs. For VGA monitor, the graphics driver used graphics program must make a call to the closegraph()
is EGAVGA.BGI. function at the end of graphics. Otherwise DOS screen
Here, initgraph() function initializes the graphics, will not go to text mode after running the program. Here,
i.e., mode and clears the screen initializes the graphics closegraph() is called after getch() because the screen
system. Its prototype can be given as: must not be cleared until the user hits a key.
In case you have the BGI file in the same folder of your
void far initgraph(int far *graphdriver, int far
program, you can just leave it as "".You need not mention
*graphmode, char far *pathtodriver);
*graphmode if you give *graphdriver as DETECT.

© 2015 Oxford University Press. All rights reserved.


In graphics mode, screen coordinates are mentioned in displays a text in the current position. Current position is
terms of pixels. Number of pixels in the screen decides the place where the last drawing ends. The prototypes of
resolution of the screen. In the example, a circle is drawn these functions can be given as
with x-coordinate of 250, y-coordinate 150, and radius void far outtextxy(int x,int y,char *text);
100 pixels. All the coordinates are mentioned from the void far outtext(char *text);
top-left corner of the screen.
The arc() is used to draw a circular arc in the current
Let us look at another program. drawing colour. The prototype of arc() can be given as
#include <conio.h> void far arc(int x,int y,int stangle,int
main() endangle, int radius);
{
int gd = DETECT, gm; The pieslice() is used to draw a pie slice in the
int polypoints[10]={350,450,350,410, current drawing colour, then fills it using the current fill
430,400,350,350,300,430}; pattern and fill color.
void far pieslice(int x, int y, int stangle, int
initgraph(&gd, &gm, "c:\\turboc3\\bgi"); endangle, int radius);

circle(120,120,70); In the above prototypes of arc() and pieslice(),


outtextxy(100,200, "CIRCLE"); ∑ (x,y) denotes the centre point of arc, or the pie
slice
rectangle(200,50,300,100); ∑ stangle specifies the start angle in degrees
outtextxy(250,180,"RECTANGLE");
∑ endangle gives the end angle in degrees
ellipse(500,100,0,360,100,50); ∑ radius denotes the radius of arc and pieslice
outtextxy(480,170,"ELLIPSE"); Note that stangle and endangle are specified in
degrees starting from the +ve x-axis in the polar coordinate
line(100,200,500,200);
system in the anti-clockwise direction. If stangle is 0,
outtextxy(250,250,"LINE");
endangle is 360, it will draw a full circle.
To draw a border, use the rectangle() function with
sector(150,400,30,300,100,50);
outtextxy(120,460,"SECTOR");
the coordinates of outline. In order to draw a square,
use rectangle with same height and width. Therefore,
drawpoly(5,polypoints); the rectangle() draws a rectangle in the current line
outtextxy(340,460,"POLYGON WITH FIVE style, thickness, and drawing color. The prototype of
SIDES"); rectangle() can be given as
void far rectangle(int left,int top,int right,
getch(); int bottom);
closegraph();
} Here, left and top specifies the upper left corner of
the rectangle, right and bottom specifies the lower right
In the above program, the circle() function takes
corner of the rectangle.
the x-, y- coordinates of the circle with respect to top left
Similarly, drawpoly() and fillpoly() are two
corner of the screen and radius of the circle in terms of
functions useful to draw any polygons. To use these
pixels as arguments.
functions, we need to store the coordinates of the shape in
The outtextxy() function is used to display a string
an array and pass the address of the array as an argument
in graphical mode. You can use different fonts, text sizes,
to the function.
alignments, colours, and directions of the text to be
The function fillpoly() is used to fill in the shape
displayed. The outtextxy() accepts x and y coordinates
with current fill colour. The function drawpoly() draws
of the position on the screen where text is to be displayed
the polygon using the current line style and colour. On
as parameters. There is another function outtext() that

© 2015 Oxford University Press. All rights reserved.


Introduction to C Programming

similar grounds, fillpoly() draws the outline of a Note that instead of writing the color name, you may
polygon using the current line style and colour, then fill also specify the color code like setcolor(14);.
the polygon using the current fill pattern and fill color. The function setbkcolor() is used to set the back-
Let us now have a quick look at the prototype of these ground colour for drawing. The function setfillstyle()
functions: is used to set fill pattern and fill colors. After calling
void far drawpoly(int numpoints, int far setfillstyle(), if we use functions like floodfill(),
*polypoints); fillpoly(), bar etc., the shapes will be filled with fill
void far fillpoly(int numpoints, int far color and pattern set using setfillstyle(). The proto-
*polypoints); types of these functions can be given as
In the above prototypes, void far setfillstyle(int pattern, int color);
void far setcolor(int color);
∑ numpoints is used to specify the number of points
void far setbkcolor(int color);
∑ polypoints gives the sequence of integers which
is equal to (numpoints x 2). Each pair of integers The parameter pattern in setfillstyle is as follows:
gives the x- and y- coordinates of a point on the STYLE VALUE EFFECT
polygon. EMPTY_FILL O Back ground color
Basically there are 16 colors declared in graphics.h SOLID_FILL 1 Solid Fill
as listed below. LINE_FILL 2 ---
LTSLASH_FILL 3 ///
COLOR COLOR CODE
SLASH_FILL 4 /// (thick lines)
BLACK 0
BKSLASH_FILL 5 \\\ (thick lines)
BLUE 1
LTBACKSLASH_FILL 6 \\\
GREEN 2 HATCH_FILL 7 Light hatch
CYAN 3 XHATCH_FILL 8 Heavy cross hatch
RED 4 INTERLEAVE_FILL 9 Interleaving lines
MAGENTA 5 WIDE_DOT_FILL 10 Widely spaced dots
BROWN 6 CLOSE_DOT_FILL 11 Closely spaced dots
USER_FILL 12 User-defined fill paƩern
LIGHTGRAY 7
Look at the program given below which demon-
DARKGRAY 8
strates the use of setfillstyle(), setcolor () and
LIGHTBLUE 9
setbkcolor().
LIGHTGREEN 10
#include <graphics.h>
LIGHTCYAN 11 #include <conio.h>
LIGHTRED 12 main()
{
LIGHTMAGENTA 13 int gd = DETECT, gm;
YELLOW 14 initgraph(&gd, &gm, "c:\\turboc3\\bgi");
WHITE 15
setbkcolor(11);
These colors can be used by using functions such as setcolor(4);
setcolor(), setbkcolor(), and setfillstyle(). setfillstyle(SOLID_FILL, LIGHTRED);
bar(100, 100, 300, 250);
The setcolor() function is used to set the current
drawing color. If we use setcolor(YELLOW) then if you
getch();
draw any shape, line or text after that, the drawing will be closegraph();
in yellow color. }

© 2015 Oxford University Press. All rights reserved.

You might also like