Draw a line, First graphics program in C

Program to Draw a line 

 

#include<graphics.h>

#include<stdio.h>

#include<conio.h>


void main(void) {

    int gdriver = DETECT, gmode;

    int x1 = 200, y1 = 200;

    int x2 = 300, y2 = 300;

    clrscr();

    

    initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");

    line(x1, y1, x2, y2);

    getch();

    closegraph();

}




Code Explanation-

The first step in any graphics program is to include graphics.h header file. The graphics.h header file provides access to a simple graphics library that makes it possible to draw lines, rectangles, ovals, arcs, polygons, images, and strings on a graphical window.


The second step is initialize the graphics drivers on the computer using initgraph method of graphics.h library.

void initgraph(int *graphicsDriver, int *graphicsMode, char *driverDirectoryPath);


It initializes the graphics system by loading the passed graphics driver then changing the system into graphics mode. It also resets or initializes all graphics settings like color, palette, current position etc, to their default values. Below is the description of input parameters of initgraph function.


graphicsDriver : It is a pointer to an integer specifying the graphics driver to be used. It tells the compiler that what graphics driver to use or to automatically detect the drive. In all our programs we will use DETECT macro of graphics.h library that instruct compiler for auto detection of graphics driver.


graphicsMode : It is a pointer to an integer that specifies the graphics mode to be used. If *gdriver is set to DETECT, then initgraph sets *gmode to the highest resolution available for the detected driver.


driverDirectoryPath : It specifies the directory path where graphics driver files (BGI files) are located. If directory path is not provided, then it will search for driver files in current working directory directory. In all our sample graphics programs, you have to change path of BGI directory accordingly where you Turbo C++ compiler is installed.




Comments