Monday, February 27, 2012

First C graphic program - Draw a line


Hello all, Lets start with simple c graphics program. first thing is to go through the basic skelton of a c graphic program. Usually c program runs in a text mode. To run graphic program, we need to change the screen mode from text mode to graphics mode. initgraph() function does this job. All graphic functions are defined in graphics.h header file. I will not be going though all the details about graphic modes, however all details about devices and modes are clearly documented in turboc help index. please refer for more details.
For all our programs, we will leave to initgraph, the job of selecting graphic device and supported mode automatically. All the driver files are located in "c:\tc\BGI\" directory by default after Installing turbo C++.


Once after changing the mode to graphic, use graphics drawing functions to perform graphics. We will discuss about each function later. After completing the graphics, use closegraph() to gracefully shut down the graphic mode and return to text mode.

in short, follow these steps for any graphic program
1. call initgraph() to change to graphic mode
2. perform graphics job
3. call closegraph() to close the graphics mode.

Lets start with a simple example, drawing a line using graphic program in C

This program is written by using line() function. as usual, to draw a line, we need starting point and end point. These points need to specified as arguments to line() function. This program draws a diagonal line from top-left of screen to bottom-right.


/* Presented by www.ncooltips.com
 * Drawing a line
 */
#include < stdio.h >
#include < conio.h >
#include < graphics.h >

void main()
{
int gdriver=DETECT,gmode=0;
/* Open graphic mode
Detect grahic driver and supporting mode
 automatically*/
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

/* draw  line */
line(0,0,getmaxx(),getmaxy());

getch();

/*Close graphic mode */
closegraph();

}



please leave comments behind. Thanks
Pin It
Related Posts Plugin for WordPress, Blogger...