Monday, December 13, 2010

C graphics


Your Screen For C graphics



C graphics.h tutorial
The most basic thing you have to learn before stepping in the C shore of graphics is about the monitor.
Loading The Header
#include<stdio.h>  
#include<graphics.h>  
void main()  
{  
}

#include<stdio.h>
#include<graphics.h>
void main()
{
}
C graphics.h
The C graphics.h is the header file which should be included to initalize your computer to start using grahipcs methods and also to initialize the monitor.These kinds of statements before the main program are called preprocessor dierctives.Its very simple as coded below.

Initializing  C graphics library
This initialization is done inside the main program.The method initgraph()  is used for this purpose there are three parameters for this , the first two are of integer type and the next is the path which you can leave blank in quoutes to take the default path.The integer parameters are initalized in this method for all the C graphics programs so you just need to memorize it and apply it for all C graphics programming from now on.

Initializing 'Graphics Driver' and 'Graphics Mode' from C graphics library
  1. #include<stdio.h>  
  2. #include<conio.h>  
  3. #include<graphics.h>  
  4. void main()  
  5. {  
  6. int gdriver=DETECT,gmode;  
  7. initgraph(&gdriver,&gmode," ");  
  8. }  
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode," ");
}
Methods In C Graphics programming
1.      cleardevice()  
2.      gotoxy(x,y)  
3.      putpixel(x,y,WHITE)  
4.      outtextxy(x,y,"HELLO")  
5.      rectangle(x,y,int x_width,int y_height)  
6.      circle(x,y,radius)  
7.      line(x1,y1,x2,y2)  
8.        moveto(dx,dy)  
9.      lineto(x,y)  
10.  ellipse(x-centre,y-center,starting_angle,ending_angle,x_radius,y_radius)  
11.  drawpoly(num_of_points + 1, points)  
12.  settextstyle(DEFAULT_FONT,HORIZ_DIR,1)  
13.  setfillstyle(SOLID_FILL,RED)  
14.  setcolor(CYAN)  
15.  floodfill(x,y,getmaxcolor())  
16.  setviewport(int left,int top,int right,int bottom, int clip)  
17.  setviewport(10,10,630,470,1)  
18.    itoa(x1,st1,10)  
19.    delay(100)  
20.    closegraph()
putpixel(x,y,WHITE)
outtextxy(x,y,"HELLO")
rectangle(x,y,int x_width,int y_height)
circle(x,y,radius)
line(x1,y1,x2,y2)
moveto(dx,dy)
lineto(x,y)
ellipse(x-centre,y-center,starting_angle,ending_angle,x_radius,y_radius)
drawpoly(num_of_points + 1, points)
settextstyle(DEFAULT_FONT,HORIZ_DIR,1)
setfillstyle(SOLID_FILL,RED)
setcolor(CYAN)
floodfill(x,y,getmaxcolor())
setviewport(int left,int top,int right,int bottom, int clip)
setviewport(10,10,630,470,1)
itoa(x1,st1,10)
delay(100)
closegraph()
Some Common C Graphics Programming Methods
Well, these are some of the most popular C Graphics Methods used in while graphics programming soon after you learn these you can think of developing small games , puzzles and so on.I'll just give only a small overview on each methods.x,y,x1,y1,x2,y2 etc are all variables you'll have to substitute with numeric values to get working programs.Further clear-cut reference can be obtained from any of the C graphics book available from amazon or your local library(those books would be pretty old and dusty by now). If you find it difficult to view the long C graphics library functions code in the window because the code is too lengthy, then click on the 'view plain' written in small fonts at the top left of the codes box on the left side of this text.
Cleardevice()
Clears all previous graphical outputs generated by the previous programs.Its a good practice to include this method at the starting of each program.
gotoxy()
This will initialize the graphics cusor to the specified co-ordiante.In C gotoxy function is used very frequently to locate the cursor at different locations whenever as necessary.
putpixex()
It will colour the pixel specified by the co-ordinates.
outtextxy()
This method is used to display a text in any position on the screen.The numeric coordinates are substituted for x and y.
rectangle()
Draws a rectangle according to the given parameter x and y are the top-left corner co-ordinates.
circle()
Draws a circle with x,y as the center .
line()
Draws a line as per the given co-ordinates.
moveto()
Cursor is moved from the current location to the specified location dx,dy.These parameters can also be used as incremental values.
lineto()
Draws a line from its current location to the co-ordinate(x,y)
ellipse()
Draws the ellipse with the specified angles and coordinates.
drawpoly()
Draws a polygon with (num_of_points +1) edges.The array 'points'
int points[ ]=(x1,y1,x2,y2,x3,y3...)
settextstyle()
The fonts available are :TRIPLEX_FONT, SMALL_FONT, SANS_SERIE_FONT, GOTHIC_FONT
The direction can be changed as HORIZ_DIR or VERT_DIR
The charecter size increases from 1 to 10
setfillstyle()
The fill styles avaliable are SOLID_FILL, LINE_FILL, HATCH_FILL, SLASH_FILL etc.
setcolor()
Sets the color
floodfill()
The function fills the enclosed area about x,y with the highest value of the color returned by the getmaxcolor().
setviewport()
This marks a rectangle starting from (10,10) having widht 630 pixels height 470 pixels.The integer specifies this identified area may be clipped. clearviewport() reverse this function.
itoa()
Converts the integer x1into an alphabet, and puts it in st1, which is an array to hold 10 characters.10 is the buffer size.
delay()
Cause a pause in execution of the program 1000ms= 1 second
closegraph()
Terminates all graphics operations and revert the hardware back to the normal mode.


1 comment:

  1. A structured stuff. Useful for exam and in terms of reference as well.

    ReplyDelete