Graphics in C - Draw a Simple Line
Computer graphics is a very
interesting and important area in any computer language. C is a powerful
language as far as graphics are concerned. There are so many ready-made graphics
functions available in the C computer language. Let us explore one by one through
programs.
Here I assume that you have
some basic knowledge of C computer language at your disposal and you have Turbo C
installed on your computer.
Here is a simple program
given below:
#include<graphics.h>
void main()
{
int gd=DETECT, gm;
initgraph(&gd, &gm,
"c:\\tc\\bgi");
line(200, 200, 400, 200);
getch();
closegraph();
restorecrtmode();
}
Let us understand the
program line by line:
The first line #include<graphics.h> tells the compiler to include the header file
“graphics.h”. To draw anything on the screen we need a header file ‘graphics.h’
and a library file ‘graphics.lib’. This header file contains the definition of
all the graphics functions and constants. The library file contains graphics
functions.
To create graphics we need
to change our computer from text mode to graphics mode. For this we have to
call the function initgraph().
Declaration:
void far initgraph(int far *graphdriver,
int far *graphmode, char far *pathtodriver);
Function initgraph()
initializes the graphics system by loading a graphics driver from disk then
putting the system into graphics mode.
Function initgraph()
also resets all graphic settings (color, palette, current position, viewport,
etc,) to their defaults, and then returns 0.
Two variables of integer
type, named 'gd' and 'gm', have been defined. 'gd 'refers to graphics driver and 'gm' refers to graphics mode. Graphics
drivers are the small programs, which communicate directly to the hardware. Turbo
C has some certain graphics drivers. These drivers are files with the extension
name ‘bgi’. On the basis of adapter, appropriate bgi file is selected. 'gd' has been assigned the value 'DETECT '. 'DETECT' is a macro whose responsibility is to
detect appropriate 'bgi' file. Because of this, initgrapgh() figures out the appropriate 'bgi' file. Then this file is loaded into memory.
Function initgraph() has been called to assign the values to 'gm' and 'gd'. This function figures out the best value i.e. best resolution and puts the
value in the variable 'gm'. This function also figures out the best 'bgi' file and
puts the value in 'gd'. 'bgi' files are generally found
in 'c' drive’s 'tc' directory. So the path is given ‘c:\tc\bgi’. If your computer
has different path, change this accordingly.
Since in the declaration of function initgraph(), arguments are of pointer type, that's why we have passed the addresses of 'gd' and 'gm'.
When switched to graphics
mode some changes occur. One is, our cursor disappears from the screen. Other
is, screen is now measured by the coordinate system, in which the top left
corner is (0, 0) and the values are increased vertically and horizontally. Every point on the screen is denoted by the
(x, y). First value is the x-axis, which goes horizontally across, second value
is y-axis, which goes vertically downward.
Function line()
draws a line between two given points.
Declaration:
void far line(int x1, int y1, int x2,
int y2);
Function line()
draws a line from (x1, y1) to (x2, y2) using the current color, line styles,
and thickness. So line(200, 200, 400, 200) draws a line from point (200, 200) to
point (400, 200).
Function getch()
gets a character from keyboard but does not echo to the screen. I have used
this function to hold the output; otherwise you won’t be able to see the
output.
Function closegraph() shuts down the graphics system, frees all memory allocated by the graphics system. It then restores the screen to the mode it was in before calling function initgraph().
Declaration:
void far closegraph(void);
Function restorecrtmode()
restores the original video mode detected by function initgraph().
Declaration:
void far restorecrtmode(void);
So this simple program is
well explained now. Although it does not
do anything significant, but it is a building block for graphics programming in
C computer language.
OUTPUT:
You would also like these programs given below:
C Program to draw a line
C Program to draw a circle and a point
C Program to draw different types of ellipses
C Program to draw arc, pie-slice, and rectangle
C Program to draw a polygon
C Program to draw filled polygon, 2-D bar, and 3-D bar
C Program to create the user-defined fill-pattern
C Program to explain different line-styles
C Progarm to explain functions moveto() and lineto()
C Program to explain moverel() and linerel()
C Program to draw an elliptical pie-slice using sector()
C Program to write text in different font styles using settextstyle()
C Program to justify text using settextjustify()
C Program to explain function setusercharsize()
C Program to define palette of colors
C Program to generate captcha code
C Program for moving a ball on screen
C Program for creating animated circles
Playing with mouse using C language
C Program to restrict mouse pointer in a window
C Program for getting the position of mouse pointer
C Program to create a clock
Interrupt Handling With C
No comments:
Post a Comment
Please give your comment