-->
 Introduction to graphics libraries in C -MASTER GUIDE

Introduction to graphics libraries in C -MASTER GUIDE

 Introduction to graphics libraries in C 

If you know the basics of C, you can easily learn graphics programming. In this sections a short introduction to graphics programming environment in C is presented. Turbo C has a good collection of graphics libraries. To start programming, let us write a small program of drawing a circle on a computer screen.

/ * simple. c * / 
#include<graphics.h>
#include<conio.h>
void main ( ) 
{     int gd=DETECT, gm; 
    initgraph ( &gd, & gm , "c: \ \ turboc3\ \bgi"); 
    circle(200, 100, 150) ; 
    getch ( ) ; 
    closegraph ( ) ;
 }
To run this program, you need the graphics.h header file, graphics.lib library file and the graphics driver (BGI file) in the program folder. These files are part of the Turbo C package. All standard programs, by default use the 640 x 480 mode on a VGA monitor. You must make necessary changes to your programs according to your screen resolution. For a VGA monitor, graphics driver used is EGAVGA.BGI .
Here, the initgraph ( ) function initializes the graphics mode and clears the screen,

Declaration
 void far initgraph (int far *graphdriver, int far *graphmode, char far *pathtodriver) ;

Remarks: To start the graphics system, we must first call initgraph ( ) with appropriate parameters. The function initgraph initializes the graphics system by loading a graphics driver from disk (or validating a registered driver) then putting the system into graphics mode. initgraph also resets all graphics settings (color, palette, current position, viewport, etc.) to their defaults, then resets graphresult to 0.

Arguments

*graphdriver is an integer that specifies the graphics driver to be used. We can give graphdriver a value using a constant of the graphics drivers enumeration type.

*graphmode is an integer that specifies the initial graphics mode (unless *graphdriver = DETECT). If *graphdriver = DETECT, initgraph sets *graphmode to the highest resolution available for the detected driver. We can give *graphmode a value using a constant of the graphics_modes enumeration type.

pathtodriver specifies the directory path where initgraph looks for graphics drivers ( * . BGI) first. If they are not there, initgraph looks in the current directory. If pathtodriver is null, the driver files must be in the current directory. This is also the path settext style searches for the stroked character font files ( * . CHR).

closegraph ( ) function switches back the screen from the graphics mode to the text mode. It also clears the screen. A graphics program should have a closegraph function at the end of graphics. Otherwise, DOS screen will not go to the text mode after running the program. Here, closegraph() is called after getch ( ) since the screen should not clear until the user hits a key. 

If you have the BGI file in the same folder of your program, you can just leave it as "" only. You need not mention *graphmode, if you give * graphdriver as DETECT. In graphics mode, all the screen coordinates are mentioned in terms of pixels. Number of pixels in the screen decides resolution of the screen. In the example simple.c described earlier, a circle is drawn with x-coordinate of the center 200, y-coordinate 100, and radius 150 pixels. All the coordinates are mentioned with respect to top-left corner of the screen.

Basic shapes and colors:

 Now let us write a program to draw some basic shapes. 

/ * shapes. c * / 

#include<graphics.h> 

#include <conio.h>

void main ( )


    int gd=DETECT, gm; 
    int poly[12]={ 350,450, 350, 410, 430, 400 , 350 , 350 , 300, 430, 350, 450 } ;
     initgraph( &gd, &gm,” “); 
    circle(100, 100, 50) ;
     outtextxy(75, 170, "Circle") ; 
     rectangle(200, 50, 350, 150) ; 
    outtextxy(240, 170, "Rectangle") ; 
    ellipse(500, 100, 0,360,100,50) ; 
    outtextxy (480, 170, "Ellipse") ; 
    line (100, 250, 540, 250) ;
     outtextxy (300, 260, "Line") ; 

    sector(150, 400, 30, 300, 100 , 50 ) ; 
    outtextxy(120, 460, "Sector") ; 
    drawpoly(6, poly) ; 
    outtextxy(340, 460, "Polygon") ; 
    getch( ) ; 
    closegraph( ) ; 
Stages of stereo 3D presentation
    Here, the circle( ) function takes x, y coordinates of the circle with respect to the left top of the screen and radius of the circle in terms of pixels as arguments. Note that, in graphics, almost all the screen parameters are measured in terms of pixels. 
    The outtextxy ( ) function displays a string in graphical mode. We can use different fonts, text sizes, alignments, colors, and directions of the text that we will study later. 
    Parameters passed are x and y coordinates of the position on the screen where text is to be displayed. There is another function outtext ( ) that displays a text in the current Position. Current position is the place where last drawing is ended. These functions are declared as follows: 
void far outtextxy (int x, int y, char *text) ; 
void far out text (char *text) ;

Circle, arc, and pie slice are declared as follows: 
Declaration: 
void far arc (int x, int y, int stangle, int endangle, int radius) ; 
void far circle (int x, int y, int radius) ; 
void far pies lice (int x, int y, int stangle, int endangle, int radius) ;\

Remarks

arc draws a circular arc in the current drawing color. 
circle draws a circle in the current drawing color. 
pieslice draws a pie slice in the current drawing color, then fills it using the current fill pattern and fill color.

Arguments: 

(x, y) : Center point of arc, circle, or pieslice 

stangle : Start angle in degrees 

endangle : End angle in degrees

radius : Radius of arc, circle, and pieslice

Arguments:
 (left, top) is the upper-left corner of the rectangle, and (right, bottom) is its lower-right corner. 
numpoints: Specifies number of points 
*polypoints : Points to a sequence of (numpoints X 2) integers. Each pair of integers gives the x and y coordinates of a point on the polygon. 
To draw a closed polygon with N points, numpoints should be N + 1 and the array polypoints [ ] should contain 2(N+ 1) integers with first two integers equal to last two integers. 

Here is some idea about colors. There are 16 colors declared in graphics.h as shown in Table:


To use these colors, use functions setcolor ( ) , setbkcolor ( ) , and setfillstyle ( ). The set color ( ) function sets the current drawing color. If we use setcolor (RED) and draw any shape, line, or text after that, the drawing will be in red color. We can either use color as defined above or number like setcolor(4).setbkcolor( ) sets background color for drawing. setfillstyle sets fill pattern and fill colors. After calling setfillstyle, if we use functions like floodfill, fillpoly, bar, etc, shapes will be filled with fill color and pattern set using setfillstyle.

These function declarations are as follows: 
Declaration: 
void far setfillstyle(int pattern, int color); 
void far setcolor(int color); 
void far setbcolor(int color); 

Remarks: 
setfillstyle sets the current fill pattern and fill color 
setcolor sets the current drawing color to color, which can range from 0 to getmaxcolor. 
setbkcolor sets the background to the color specified by the color.

Table 1.3 shows the parameter pattern in setfillstyle.

Here is an example program with colors, pixels, bar, cleardevice, etc. stdlib.h is used for random number generation. The function random(no) returns a random number between 0 and no. The effect is by drawing random radius, random circles with same center and random pixels. kbhit ( ) function (defined in conio.h returns a nonzero value when a key is pressed in the keyboard, So, the loop will Continue until a key is pressed.

/* random.c some graphics effects using random numbers .*/
#include "graphics.h" 
#include "conio.h" 
#include "stdlib.h" 
void main ( ) 
    int gd,gm; gd=DETECT ; 
    initgraph (&gd, &gm, setcolor (3) ; 
    setfillstyle (SOLID FILL, RED) ; 
    bar(50, 50, 590, 430) ; 
    setfillstyle (1, 14) ; 
    bar(100, 100, 540, 380) ; 
    while ( ! kbhit ( ) ) 
        {
             putpixel (random (439) +101, random (279) +101, random (16) ) ; 
            setcolor (random (16) ) ; 
            circle (320, 240, random (100) ) ; 
        
    getch ( ) ; 
    closegraph ( ) ; 
}

0 Response to " Introduction to graphics libraries in C -MASTER GUIDE"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel