Вы находитесь на странице: 1из 12

GRAPHICS IN C

INTRODUCTION :
C Graphics programming is very easy and interesting. You can use graphics programming for developing your own games, in making projects, for animation etc. It's not like traditional C programming in which you have to apply complex logic in your program and then you end up with a lot of errors and warnings in your program. In C graphics programming you have to use standard library functions ( need not worry if you don't know functions ) to get your task done. Just you pass arguments to the functions and it's done. On this,you will find almost all functions with detailed explanation and a sample program showing the usage of a function. To make things easy you are provided with executable files which you can download and execute. Firstly you should know the function initgraph which is used to initialize the graphics mode . To initialize graphics mode we use initgraph function in our program. initgraph function is present in "graphics.h" header file, so your every graphics program should include "graphics.h" header file. We will discuss initgraph withe help of following sample program:Sample graphics code #include<graphics.h> #include<conio.h> void main() { int gd = DETECT, gm; initgraph(&gd, &gm, "C:\\TC\\BGI"); getch(); closegraph(); } To run this program, we need graphics.h header file, graphics.lib library file and Graphics driver (BGI file) in the program folder. These files are part of Turbo C package. In all our programs we used 640x480 VGA monitor.We need to make necessary changes to your programs according to your screen resolution. For VGA monitor, graphics driver used is EGAVGA.BGI.

InitGraph: initgraph() function initializes the graphics mode and clears the screen. Declaration: void far initgraph(int far *graphdriver, int far *graphmode, char far *pathtodriver); 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: Integer that specifies the graphics driver to be used. You can give graphdriver a value using a constant of the graphics drivers enumeration type. *graphmode : 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. You 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're 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 settextstyle searches for the stroked character font files (*.CHR). closegraph() : closegraph() function switches back the screen from graphcs mode to text mode. It clears the screen also. A graphics program should have a closegraph function at the end of graphics. Otherwise DOS screen will not go to text mode after running the program. Here, closegraph() is called after getch() since screen should not clear until 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 co-ordinates are mentioned in terms of pixels. Number of pixels in the screen decides resolution of the screen. In the example 1.0, 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.

ORIGIN :
The phrase Computer Graphics was coined in 1960 by William Fetter, a graphic designer for Boeing.[4] The field of computer graphics developed with the emergence of computer graphics hardware. Early projects like the Whirlwind and SAGE Projects introduced the CRT as a viable display and interaction interface and introduced the light pen as an input device. The term computer graphics has been used in a broad sense to describe "almost everything on computers that is not text or sound". Typically, the term computer graphics refers to several different things: the representation and manipulation of image data by a computer the various technologies used to create and manipulate images the sub-field of computer science which studies methods for digitally synthesizing and manipulating visual content, see study of computer graphics

FUNCTIONS OF GRAPHICS.H :
C graphics using graphics.h functions or WinBGIM (Windows 7 ) can be used to draw different shapes, display text in different fonts, change colors and many more. Using functions of graphics.h in turbo c compiler you can make graphics programs, animations, projects and games.You can draw circles, lines, rectangles, bars and many other geometrical figures. You can change their colors using the available functions and fill them. Following is a list of functions of graphics.h header file. Every function is discussed with the arguments it needs, its description, possible errors while using that function and a sample c graphics program with its output.

BASIC SHAPES AND COLORS:


#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(); } Here is the screenshot of output:

Here, circle() function takes x, y coordinates of the circle with respect to left top of the screen and radius of the circle in terms of pixels as arguments. Not that, in graphics, almost all the screen parameters are measured in terms of pixels. Function outtextxy() displays a string in graphical mode. You 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 displayes 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 outtext(char *text);

Circle, arc, pieslice 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 pieslice(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, circlew, or pie slice stangle: Start angle in degrees endangle: End angle in degrees radius: Radius of arc, circle, and pieslice Here, stangle and endangle are in degrees starting from the +ve x-axis in the polar coordinate system in the anti-clockwise direction. if stangle is 0, endangle is 360, it will draw a full circle. Refer this figure for clear idea: For the details of current color, fill color and fill patterns, refer the sections Lines and Colors.

Another basic shape that we come across is a rectangle. To draw a border, use rectangle with the coordinates of outline, to draw a square use rectangle with same height and width. drawpoly() and fillpoly() are two functions useful to draw any polygons. To use these functions, store coordinates of the shape in an array and pass the address of array as an argument to the function. By looking at the output of the previous program, you can understand what drawpoly is. fillpoly is similar except that it fills in the shape with current fill color. Declaration:

void far rectangle(int left, int top, int right, int bottom); void far drawpoly(int numpoints, int far *polypoints); void far fillpoly(int numpoints, int far *polypoints);

Remarks:

rectangle draws a rectangle in the current line style, thickness, and drawing color. drawpoly draws a polygon using the current line style and color. fillpoly draws the outline of a polygon using the current line style and color, then fills the polygon using the current fill pattern and fill color.

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.

Shapes Latter. There are 16 colors declared in graphics.h as listed bellow.

BLACK: 0 BLUE: 1 GREEN: 2 CYAN: 3 RED: 4 MAGENTA: 5 BROWN: 6 LIGHTGRAY: 7 DARKGRAY: 8 LIGHTBLUE: 9 LIGHTGREEN: 10 LIGHTCYAN: 11 LIGHTRED: 12 LIGHTMAGENTA: 13 YELLOW: 14 WHITE: 15
To use these colors, use functions setcolor(), setbkcolor() and setfillstyle(). setcolor() 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. You 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, shpes 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 setbkcolor(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 color.

Here is an example program with colors, pixels, bar, cleardevice etc. stdlib.h is used for random number generation. We have a function random(no), it returns a random number between 0 an no. The effect is by drawing random radius, random color 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. #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(); }

C GRAPHICS PROGRAMS :
Draw shapes Bar chart Pie chart 3d bar chart Smiling face animation captcha Circles in circles Countdown Paint program in c Press me button game Web browser program Traffic Light Simulation Mouse pointer restricted in circle

C GRAPHICS EXAMPLES :
1. Drawing concentric circles: #include <graphics.h> int main() { int gd = DETECT, gm; int x = 320, y = 240, radius; initgraph(&gd, &gm, "C:\\TC\\BGI"); for ( radius = 25; radius <= 125 ; radius = radius + 20) circle(x, y, radius); getch(); closegraph(); return 0; }

2. C graphics program moving car: #include <graphics.h> #include <dos.h> int main() { int i, j = 0, gd = DETECT, gm; initgraph(&gd,&gm,"C:\\TC\\BGI"); settextstyle(DEFAULT_FONT,HORIZ_DIR,2); outtextxy(25,240,"Press any key to view the moving car"); getch(); for( i = 0 ; i <= 420 ; i = i + 10, j++ ) { rectangle(50+i,275,150+i,400); rectangle(150+i,350,200+i,400); circle(75+i,410,10); circle(175+i,410,10); setcolor(j); delay(100);

if( i == 420 ) break; if ( j == 15 ) j = 2; cleardevice(); // clear screen } getch();

closegraph(); return 0; }

CONCLUSION :
Computer graphics is widespread today. Computer imagery is found on television, in newspapers, for example in weather reports, or for example in all kinds of medical investigation and surgical procedures. A well-constructed graph can present complex statistics in a form that is easier to understand and interpret. In the media "such graphs are used to illustrate papers, reports, thesis", and other presentation material. Many powerful tools have been developed to visualize data. Computer generated imagery can be categorized into several different types: 2D, 3D, and animated graphics. As technology has improved, 3D computer graphics have become more common, but 2D computer graphics are still widely used. Computer graphics has emerged as a sub-field of computer science which studies methods for digitally synthesizing and manipulating visual content. Over the past decade, other specialized fields have been developed like information visualization, and scientific visualization more concerned with "the visualization of three dimensional phenomena (architectural, meteorological, medical, biological, etc.), where the emphasis is on realistic renderings of volumes, surfaces, illumination sources, and so forth, perhaps with a dynamic (time) component".

REFRENCES :

http://www.programmingsimplified.com/c-graphics-programming-tutorial http://electrosofts.com/cgraphics/ http://en.kioskea.net/faq/4955-graphics-programming-in-c#simili_main http://www.mycplus.com/tutorials/c-programming-tutorials/graphics-programming/ http://www.studiesinn.com/learn/Programming-Languages/C-Language/Graphics.html http://en.wikipedia.org/wiki/Computer_graphics

Вам также может понравиться