Graphics Programming in C Language

Computer graphics is defined as the creation, storage and manipulation of images, pictures and drawings by means of a digital computer. It enhances the power of communication between the computer and its users. It allows representation of huge set of numbers in the form of a graph or a picture which helps in better understanding of the characteristic and pattern of data.

IMAGES ON SCREEN

The representation of images in computer graphics is made by bits.

Converting images into binary form

Any image is constructed by straight lines, curves and text matter. These are formed of set of dots. A line consists of a set of dots lying on the shortest path connecting its end points. An arc is a collection of dots lying on the trajectory of the arc. Hence an image can be represented as a set of dots at the appropriate locations. Most of CAD system use raster scan display for producing images.

Frame buffer

A viewing surface of a raster scan display is divided into an array of pixels. A CRT screen is made up of horizontal and vertical lines. Each horizontal line is made up of pixels. The memory of the computer is a collection of bits and a bit can take any value either 0 or 1. Each pixel can be represented by one bit in memory. This memory is called frame buffer.

Scan conversion

If a line has to be represented, then the pixel must be made bright to display the line

The pixel state of bit must be 1 in memory. This is known as scan conversion. The location of each pixel on the viewing surface and the corresponding memory location in the frame buffer is accessed by an (x,y) co-ordinate pair. The origin of co-ordinates system is fixed at the bottom -left corner. Fig. 1 show the rester scan conversion system.

           A display unit of 600 x 400 pixels requires 24 X 10<sup>4</sup> bits in the frame buffer, which is equivalent of 240 kB. If the image to be displayed is composed of colours, or with different brightness levels, more bits should be provided to every pixels.

            If there are 2 bits associated with each pixel then four different combinations are possible. The number of colours n that can be displayed is equal to

            c= 2<sup>n</sup>

      where

           n = number of bit planes

            c = number of colors

           A display system with 8 bit planes require 240 KB of memory (resolution 600 x 400 ) in the frame buffer and can display 28 i.e. 256 colours simultaneously.

           The current technology allows 24 bit planes with resolution up to 4096 x 4096 pixel. For engineering design applications, a resolution of 1280 x 1024 with 8 bit planes is sufficient. An enhanced graphics adapter (EGA) gives a resolution of 600 x 400 pixels with 16 colours. The display surface is given a two dimensional co-k ordinate pair. The x value start at the origin and increases from left to right, while the y value starts at the top and increases towards bottom as in a standard Cartesian co-ordinate system.

           In many CRT displays the pixels may not be square and number of pixels per unit length in horizontal and vertical direction.

           The lines are drawn on a raster scan display by turning on the pixels which lie lighter on or very close to the shortest path between the end points of the line. The lines drawn on the displays with lower resolutions will have more gaps between the pixels compared to one with high resolution display.

           There are variety of languages which can be used in computer graphic's. But C-languages is a suitable language to produce difficult pictures effectively on screen.

GRAPHIC MODE

In a graphic mode it is possible to display text and graphical figures. The graphic mode uses the pixel which depends on the resolution of the monitor. The graphic functions are available in C-language in a library file "graphics.h" in Borland C" which must be included in the program.

           The following functions should be used to indicate graphic mode operations. These are available in Borland C<sup>++</sup> library.

"initgraph" function

This function is used to initialize the graphics system and load the appropriate specified graphic driver and the video mode used by the graphic function.

The general form

           initgraph (& driver, & mode, "x");

           where, driver and mode must be defined as integer.

                        The driver is specified as 0 to 10 representing the monitor type.

mode - specifies the resolution of the video which is normally 'VGAHI".

           x - path where the graphic driver files are to be searched which

                consist of the drive directory, subdirectory

"restorecrtmode" function

This function restores the screen to the mode that it had prior to the operation of initgraph function restorecrtmode();

"detectgraph" function

This function detects the driver and mode of the system. It is written as detectgraph(&x,&y);

            where,             x            -      graphic driver

                                    y           -         mode of the system.

"closegraph" function This function deallocates all the memory allocated by the graphic system. It restores the screen to the mode in which it was before execution of the initgraph function.

  closegraph( );

"getch" function

This function is used to obtain the view of the graphic screen, when the program is being executed. It is written as :

            getch();

           This can be obtained by including the file 'conio.h' in program

GRAPHIC FUNCTIONS

"Setpalatte " function This function closes an index for palette and matches the colour with the index and this color sets the background

           setpalatte (0, GREEN);

"Setcolor" function

This function sets the drawing colour specified by the variable.

   setcolor(n);

           where,              n          -          number of colours.

"putpixel" function

This function illuminates the pixel represented by x and y co—ordinates in the colour. putpixel (10,20, RED);

where x & y locations are specified by 10 & 20.

"get pixel " function

This function returns the colour in which the pixel (x,y) is illuminated.

     i = getpixel (10,20);

                           where,

                                         i         -        integer variable to store the colour.

        The colour number of the pixel at 10,20 location is stored in i variable.

"moveto" function

This function positions the current cursor into the pixel (x,y).

     moveto (100, 150);

    This moves the control to the pixel at new location (100,150).

/This program draws growing circles on the screen./

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<dos.h>

void main()

{

int gd=DETECT, gm; int i;

initgraph (&gd,&gm,"c:\tc\bgi");

clrscr();

for(i=0;i<500;i++)

{

setcolor(i);

//coordinates of center from x axis, y axis, radius

circle(90,90,30+i);

delay(30);

//cleardevice(); //try with and without cleardevice();

}

getch();

closegraph();

}