Vous êtes sur la page 1sur 51

Practical file Of GRAPHICS In C LANGUAGE

SUBMITTED TO : Mrs. Pooja

SUBMITTED BY : AKASH GARG BCA III A Roll No. 2214

Pupin No. 18010000534

INDEX
Sr no. I. II. Topics Graphics in c language Functions in c
1. arc 2. cleardevice 3. bar 4. closegraph 5. circle 6. drawpoly 7. ellipse 8. getcolor 9. fillpoly 10. floodfill 11. getmaxx 12. getmaxy 13. line 14. lineto 15. linerel 16. moveto 17. outtext

Page no.

Remarks

18. outtextxy 19. pie slice 20. rectangle 21. sector 22. setbkcolor 23. setcolor 24. setfillstyle 25. setlinestyle 26. settextstyle 27. textheight 28. textwidth

III

Programs
1. WAP to draw concentric circle 2. WAP to draw histogram 3. WAP to draw hut 4. WAP to draw pie chart 5. WAP to draw different types of line 6. WAP to draw smiley 7. WAP to draw Sin and Cos Waves

Graphics in C Language
In a C Program first of all we need to initialize the graphics drivers on the computer. This is done using the initgraph method provided in graphics.h library. In the next few pages we will discuss graphics.h library in details. Important functions in graphic.h library will be discuss in details and samples programmes will be provided to show the power of C programming language. Graphics mode Initialization: First of all we have to call the initgraph function that will initialize the graphics mode on the computer. initgraph have the following prototype.

voidinitgraph(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 graph result to 0. Graphic driver: Integer that specifies the graphics driver to be used. You can give graph driver a value using a constant of the graphics drivers enumeration type. Graph mode: 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.

Path driver: Specifies the directory path where initgraph looks for graphics drivers (*.BGI) first. If theyre not there, initgraph looks in the current directory. If path to driver is null, the driver files must be in the current directory. *graphdriver and *graphmode must be set to valid graphics drivers and graphics mode values or youll get unpredictable results. (The exception is graphdriver = DETECT.) After a call to initgraph, *graphdriver is set to the current graphics driver, and *graphmode is set to the current graphics mode. You can tell initgraph to use a particular graphics driver and mode, or to auto detect the attached video adapter at run time and pick the corresponding driver. If you tell initgraph to auto detect, it calls detect graph to select a graphics driver and mode. Normally, initgraph loads a graphics driver by allocating memory for the driver, then loading the appropriate .BGI file from disk. As an alternative to this dynamic loading scheme, you can link a graphics driver file (or several of them) directly into your executable program file.

Functions of graphics.h
arc
arc function is used to draw an arc with center (x,y) and stangle specifies starting angle, end angle specifies the end angle and last parameter specifies the radius of the arc. arc function can also be used to draw a circle but for that starting angle and end angle should be 0 and 360 respectively

Declaration :- void arc(int x, int y, intstangle, intendangle, int radius); #include<graphics.h> #include<conio.h> #include<stdio.h> void main() { intgd = DETECT, gm; initgraph(&gd, &gm, "C:\\TC\\BGI"); arc(100, 100, 0, 135, 50); getch(); closegraph(); }

Output of program:

Cleardevice
cleardevice function clears the screen in graphics mode and sets the current position to (0,0). Clearing the screen consists of filling the screen with current background color.

Declaration :- void cleardevice();

#include<graphics.h> #include<conio.h> #include<stdio.h> void main() { intgd = DETECT, gm; initgraph(&gd, &gm, "C:\\TC\\BGI"); outtext("Press any key to clear the screen."); getch(); cleardevice(); outtext("Press any key to exit..."); getch(); closegraph(); return 0; }

bar
Bar function is used to draw a 2-dimensional, rectangular filled in bar. Coordinates of left top and right bottom corner are required to draw the bar. Left specifies the X-coordinate of top left corner, top specifies the Y-coordinate of top left corner, right specifies the X-coordinate of right bottom corner, bottom specifies the Y-coordinate of right bottom corner. Current fill pattern and fill color is used to fill the bar. To change fill pattern and fill color use setfillstyle. Declaration :- void bar(int left, int top, int right, int bottom);

#include <graphics.h> #include <conio.h> #include<stdio.h> void main() { intgd = DETECT, gm; initgraph(&gd, &gm, "C:\\TC\\BGI"); bar(100, 100, 200, 200); getch(); closegraph(); }

Output of program:

closegraph
closegraph function closes the graphics mode, deallocates all memory allocated by graphics system and restores the screen to the mode it was in before you called initgraph.

Declaration :- void closegraph();

#include<graphics.h> #include<conio.h> #include<stdio.h> void main() { intgd = DETECT, gm; initgraph(&gd, &gm, "C:\\TC\\BGI"); outtext("Press any key to close the graphics mode..."); getch(); closegraph(); }

circle
circle function is used to draw a circle with center (x,y) and third parameter specifies the radius of the circle. The code given below draws a circle.

Declaration :- void circle(int x, int y, int radius);

#include<graphics.h> #include<conio.h> void main() { intgd = DETECT, gm; initgraph(&gd, &gm, "C:\\TC\\BGI"); circle(100, 100, 50); getch(); closegraph(); }

Output of program:

drawpoly
Drawpoly function is used to draw polygons i.e. triangle, rectangle, pentagon, hexagon etc.

Declaration :- void drawpoly( intnum, int *polypoints ); num indicates (n+1) number of points where n is the number of vertices in a polygon, polypoints points to a sequence of (n*2) integers . Each pair of integers gives x and y coordinates of a point on the polygon. We specify (n+1) points as first point coordinates should be equal to (n+1)th to draw a complete figure.

#include <graphics.h> #include <conio.h> #include<stdio.h> void main() { intgd=DETECT,gm,points[]={320,150,420,300,250,300,320,150}; initgraph(&gd, &gm, "C:\\TC\\BGI"); drawpoly(4, points); getch(); closegraph(); }

Output of program:

ellipse
Ellipse is used to draw an ellipse (x,y) are coordinates of center of the ellipse, stangle is the starting angle, end angle is the ending angle, and fifth and sixth parameters specifies the X and Y radius of the ellipse. To draw a complete ellipse strangles and end angle should be 0 and 360 respectively.

Declarations:void ellipse(int x, int y, intstangle, intendangle, intxradius, intyradius);

#include<graphics.h> #include<conio.h> #include<stdio.h> void main() { intgd = DETECT, gm; initgraph(&gd, &gm, "C:\\TC\\BGI"); ellipse(100, 100, 0, 360, 50, 25); getch(); closegraph(); }

Output of program:

getcolor
getcolor function returns the current drawing color.

Declaration :intgetcolor(); e.g. a = getcolor(); // a is an integer variable if current drawing color is WHITE then a will be 15.

#include<graphics.h> #include<conio.h> #include<stdio.h> void main() { intgd = DETECT, gm, drawing_color; char a[100]; initgraph(&gd,&gm,"C:\\TC\\BGI"); drawing_color = getcolor(); sprintf(a,"Current drawing color = %d", drawing_color); outtextxy( 10, 10, a ); getch(); closegraph(); }

fillpoly
Fillpoly function draws and fills a polygon. It require same arguments as drawpoly.

Declaration :- void drawpoly( intnum, int *polypoints );

#include <graphics.h> #include <conio.h> #include<stdio.h> void main() { intgd=DETECT,gm,points[]={320,150,440,340,230,340,320,150}; initgraph(&gd, &gm, "C:\\TC\\BGI"); fillpoly(4, points); getch(); closegraph(); }

Output of program:

floodfill
floodfill function is used to fill an enclosed area. Current fill pattern and fill color is used to fill the area.(x, y) is any point on the screen if (x,y) lies inside the area then inside will be filled otherwise outside will be filled,border specifies the color of boundary of area. To change fill pattern and fill color use setfillstyle. Code given below draws a circle and then fills it. Declaration :- void floodfill(int x, int y, int border);

#include<graphics.h> #include<conio.h> #include<stdio.h> void main() { intgd = DETECT, gm; initgraph(&gd, &gm, "C:\\TC\\BGI"); setcolor(RED); circle(100,100,50); floodfill(100,100,RED); getch(); closegraph(); }

In the program a circle is drawn in RED color. Point (100,100) lies inside the circle as it is the center of circle, third argument to floodfill is RED which is color of boundary of circle. So the output of above program will be a circle filled with WHITE color as it is the default fill color.

Output of program:

getmaxx
getmaxx function returns the maximum X coordinate for current graphics mode and driver.

Declaration :-intgetmaxx();

#include<graphics.h> #include<conio.h> #include<stdio.h> void main() { intgd = DETECT, gm, max_x; char array[100]; initgraph(&gd,&gm,"C:\\TC\\BGI"); max_x = getmaxx(); sprintf(array, "Maximum X coordinate for current graphics mode and driver = %d.",max_x); outtext(array); getch(); closegraph(); }

getmaxy
getmaxy function returns the maximum Y coordinate for current graphics mode and driver.

Declaration :-intgetmaxy();

#include<graphics.h> #include<conio.h> #include<stdio.h> void main() { intgd = DETECT, gm, max_y; char array[100]; initgraph(&gd,&gm,"C:\\TC\\BGI"); max_y = getmaxy(); sprintf(array, "Maximum Y coordinate for current graphics mode and driver is = %d.",max_y); outtext(array); getch(); closegraph(); }

line
line function is used to draw a line from a point(x1,y1) to point(x2,y2) i.e. (x1,y1) and (x2,y2) are endpoints of the line.The code given below draws a line.

Declaration :- void line(int x1, int y1, int x2, int y2);

#include<graphics.h> #include<conio.h> #include<stdio.h> void main() { intgd = DETECT, gm; initgraph(&gd, &gm, "C:\\TC\\BGI"); line(100, 100, 200, 200); getch(); closegraph(); }

Output of program:

lineto
lineto function draws a line from current position(CP) to the point(x,y), you can get current position using getx and gety function.

#include<graphics.h> #include<conio.h> #include<stdio.h> void main() { intgd = DETECT, gm; initgraph(&gd, &gm, "C:\\TC\\BGI"); moveto(100, 100); lineto(200, 200); getch(); closegraph(); }

linerel
Linerel function draws a line from the current position(CP) to a point that is a relative distance (x, y) from the CP, then advances the CP by (x, y). You can use getx and gety to find the current position.

#include <graphics.h> #include <conio.h> #include<stdio.h> void main() { intgd = DETECT, gm; initgraph(&gd, &gm, "C:\\TC\\BGI"); moveto(250, 250); linerel(100, -100); getch(); closegraph(); }

moveto
moveto function changes the current position (CP) to (x, y)

Declaration :- void moveto(int x, int y);

#include <graphics.h> #include <conio.h> #include<stdio.h> void main() { intgd = DETECT, gm; charmsg[100]; initgraph(&gd, &gm, "C:\\TC\\BGI"); sprintf(msg, "X = %d, Y = %d",getx(),gety()); outtext(msg); moveto(50, 50); sprintf(msg, "X = %d, Y = %d", getx(), gety()); outtext(msg); getch(); closegraph(); }

outtext
outtext function displays text at current position. Declaration :- void outtext(char *string);

#include<graphics.h> #include<conio.h> #include<stdio.h> void main() { intgd = DETECT, gm; initgraph(&gd, &gm, "C:\\TC\\BGI"); outtext("to display text at a particular position use outtextxy"); getch(); closegraph();}

outtextxy
outtextxy function display text or string at a specified point(x,y) on the screen. Declaration :- void outtextxy(int x, int y, char *string); x, y are coordinates of the point and third argument contains the address of string to be displayed.

#include<graphics.h> #include<conio.h> #include<stdio.h> void main() { intgd = DETECT, gm; initgraph(&gd,&gm,"C:\\TC\\BGI"); outtextxy(100, 100, "Outtextxy function"); getch(); closegraph(); }

pieslice
declaration:-void(intx,inty,sangle,endangle,radius);

#include<graphics.h> #include<conio.h> #include<stdio.h> void main() { intgd = DETECT, gm; initgraph(&gd, &gm, "C:\\TC\\BGI"); pieslice(200,200,0,135,100); getch(); closegraph(); }

Output of program:

rectangle
rectangle function is used to draw a rectangle. Coordinates of left top and right bottom corner are required to draw the rectangle. left specifies the Xcoordinate of top left corner, top specifies the Y-coordinate of top left corner, right specifies the X-coordinate of right bottom corner, bottom specifies the Y-coordinate of right bottom corner. The code given below draws a rectangle.
Declaration :- void rectangle(int left, int top, int right, int bottom);

#include<graphics.h> #include<conio.h> #include<stdio.h> void main() { intgd = DETECT, gm; initgraph(&gd, &gm, "C:\\TC\\BGI"); rectangle(100,100,200,200); getch(); closegraph(); }

Output of program:

sector
Sector function draws and fills an elliptical pie slice. Declaration :- void sector( int x, int y, intstangle, intendangle, intxradius, intyradius);

#include <graphics.h> #include <conio.h> #include<stdio.h> void main() { intgd = DETECT, gm; initgraph(&gd, &gm, "C:\\TC\\BGI"); sector(100, 100, 0, 135, 25, 35); getch(); closegraph(); }

setbkcolor
setbkcolor function changes current background color e.g. setbkcolor(YELLLOW) changes the current background color to YELLOW. Remember that default drawing color is WHITE and background color is BLACK. Declaration :- void setbkcolor(intcolor);

#include<graphics.h> #include<conio.h> #include<stdio.h> void main() { intgd = DETECT, gm; initgraph(&gd, &gm, "C:\\TC\\BGI"); outtext("Press any key to change the background color to GREEN."); getch(); setbkcolor(GREEN); getch(); closegraph(); }

setcolor
In Turbo Graphics each color is assigned a number. Total 16 colors are available. Strictly speaking number of available colors depends on current graphics mode and driver.ForExample :- BLACK is assigned 0, RED is assigned 4 etc. setcolor function is used to change the current drawing color.e.g. setcolor(RED) or setcolor(4) changes the current drawing color to RED. Remember that default drawing color is WHITE. Declaration :- void setcolor(intcolor);

#include<graphics.h> #include<conio.h> #include<stdio.h> void main() { intgd = DETECT, gm; initgraph(&gd,&gm,"C:\\TC\\BGI"); circle(100,100,50); /* drawn in white color */ setcolor(RED); circle(200,200,50); /* drawn in red color */ getch(); closegraph(); }

Output of program:

setfillstyle
setfillstyle function sets the current fill pattern and fill color.

Declaration :- void setfillstyle( int pattern, intcolor);

Different fill styles:


enumfill_styles { EMPTY_FILL, SOLID_FILL, LINE_FILL, LTSLASH_FILL, SLASH_FILL, BKSLASH_FILL, LTBKSLASH_FILL, HATCH_FILL, XHATCH_FILL, INTERLEAVE_FILL, WIDE_DOT_FILL, CLOSE_DOT_FILL, USER_FILL };

#include<graphics.h> #include<conio.h> #include<stdio.h> void main() { intgd = DETECT, gm; initgraph(&gd, &gm, "C:\\TC\\BGI"); setfillstyle(XHATCH_FILL, RED); circle(100, 100, 50); floodfill(100, 100, WHITE); getch(); closegraph(); }

Output of program:

setlinestyle
Declaration: void setlinestyle( intlinestyle, unsigned upattern, int thickness ); Available line styles:

enumline_styles
{ SOLID_LINE, DOTTED_LINE, CENTER_LINE, DASHED_LINE, USERBIT_LINE };

#include <graphics.h> #include<conio.h> #include<stdio.h> void main() { intgd = DETECT, gm, c , x = 100, y = 50; initgraph(&gd, &gm, "C:\\TC\\BGI"); for ( c = 0 ; c < 5 ; c++ ) { setlinestyle(c, 0, 2); line(x, y, x+200, y); y = y + 25; } getch(); closegraph(); }

settextstyle
Settextstyle function is used to change the way in which text appears, using it we can modify the size of text, change direction of text and change the font of text.

Declaration :- void settextstyle( int font, int direction, intcharsize); font argument specifies the font of text, Direction can be HORIZ_DIR (Left to right) or VERT_DIR (Bottom to top).

Different fonts
enumfont_names {
DEFAULT_FONT, TRIPLEX_FONT, SMALL_FONT, SANS_SERIF_FONT, GOTHIC_FONT, SCRIPT_FONT, SIMPLEX_FONT, TRIPLEX_SCR_FONT, COMPLEX_FONT, EUROPEAN_FONT, BOLD_FONT };

#include <graphics.h> #include <conio.h> #include<stdio.h> void main() { intgd = DETECT, gm, x = 25, y = 25, font = 0; initgraph(&gd,&gm,"C:\\TC\\BGI"); for ( font = 0 ; font <= 10 ; font++) { settextstyle(font, HORIZ_DIR, 1); outtextxy(x, y, "Text with different fonts"); y = y + 25; } getch(); closegraph(); }

Output of program:

textheight
textheight function returns the height of a string in pixels.

Declaration :-inttextheight(char *string);

#include<graphics.h> #include<conio.h> #include<stdio.h> void main() { intgd = DETECT, gm, height; char array[100]; initgraph(&gd, &gm, "C:\\TC\\BGI"); height = textheight("C programming"); sprintf(array,"Textheight = %d",height); outtext(array); getch(); closegraph(); }

textwidth
textwidth function returns the width of a string in pixels.

Declaration :-inttextwidth(char *string);

#include<graphics.h> #include<conio.h> #include<stdio.h> void main() { intgd = DETECT, gm, width; char array[100]; initgraph(&gd, &gm, "C:\\TC\\BG I"); width = textwidth("C programming"); sprintf(array,"Textwidth = %d",width); outtext(array); getch();closegraph();}

Output of program:

/*WAP to draw Concentric circles*/


#include<graphics.h>
#include<conio.h> #include<stdio.h> void main() { intgd=DETECT,gm,i=1,n=6,r=100; initgraph(&gd,&gm," "); for(i=1;i<=n;i++) { Circle(500,250,r); r=r-20; } getch(); closegraph(); }

Output of program:

/*WAP to draw histogram*/


#include<graphics.h> #include<conio.h> #include<stdio.h> int values[10],n; void ingraph() { int i; settextstyle(4,0,5); outtextxy(160,5, "Bar Graph"); settextstyle(3,0,2); outtextxy(300,410, "X-Axis"); outtextxy(15,200, "Y-Axis"); settextstyle(3,1,2); line(100,60,100,400); line(100,400,500,400); for(i=120;i<500;i+=20) { line(i,400,i,405); } for(i=300;i>60;i-=20) {

line(100,i,95,i); } line(500,398,500,402); line(504,400,500,398); line(500,402,504,400); line(98,60,102,60); line(100,56,98,60); line(100,56,102,60);

} drawgraph() { int i,j; for(i=0,j=115;i<n;j+=20,i++) { rectangle(j,(400-values[i]),(j+12),400); } } void main() { int gd=DETECT,gm,i; clrscr();

initgraph(&gd,&gm, " "); printf("Enter number of objects : "); scanf("%d", &n); printf("Enter the values \n"); for(i=0;i<n;i++) { scanf("%d", &values[i]); } cleardevice(); ingraph(); drawgraph(); getch(); closegraph(); }

Output of program:
1.

2.

/*WAP to draw hut*/


#include<conio.h> #include<stdio.h> #include<graphics.h> void main() { int gd=DETECT, gm; initgraph(&gd,&gm, " "); rectangle(200,200,500,400); line(200,200,280,100); moveto(280,100); lineto(360,100); moveto(280,100); lineto(400,100); moveto(400,100); lineto(500,200); line(360,200,360,400); line(250,400,250,350); line(280,100,360,200); moveto(250,350); lineto(300,350); moveto(300,350);

lineto(300,400); arc(230,100,0,175,50); circle(180,100,5); line(175,100,150,150); line(185,100,210,150); moveto(210,150); lineto(150,150); ellipse(180,170,0,360,5,20); rectangle(390,300,470,240); line(390,280,470,280); line(390,260,470,260);getch(); closegraph(); }

Output of program:

/*WAP to draw piechart*/


#include<stdio.h> #include<conio.h> #include<graphics.h> void main() { intgd=DETECT,gm; initgraph(&gd,&gm,""); pieslice(300,200,0,30,100); setfillstyle(LINE_FILL,WHITE); pieslice(300,200,30,90,100); setfillstyle(CLOSE_DOT_FILL,WHITE); pieslice(300,200,90,180,100); setfillstyle(SLASH_FILL,WHITE); pieslice(300,200,180,360,100); outtextxy(420,165,"5%"); outtextxy(390,100,"10%"); outtextxy(200,110,"35%"); outtextxy(270,325,"50%"); getch(); closegraph(); }

Output of the program:

/*WAP to draw Different lines*/


#<include>stdio.h> #include<conio.h> #include<graphics.h> void main() { intgd=DETECT,gm; initgraph(&gd,&gm, ); printf(enter your choice) printf(\n 1 for SOLID_LINES); printf(\n 2 for DOTTED_LINES); printf(\n 3 for CENTER_LINES); printf(\n 4 for DASHED_LINES); scanf(%d,&ch); switch(ch) { case 1: { setlinestyle(SOLID_LINE,0,3); line(150,150,50,200); break; } case 2: { setlinestyle(DOTTED_LINE,0,3); line(150,150,50,200); break; } case 3: { setlinestyle(CENTER_LINE,0,3); line(150,150,50,200); break; } case 4: { setlinestyle(DASHED_LINE,0,3);

line(150,150,50,200); break; } getch(); closegraph();}

Output of program:

/*WAP to draw smiley*/


#include<graphics.h> #include<conio.h> #include<stdlib.h> void main() { int gd=DETECT, gm ,points[]={200,190,210,210,190,210,200,190}; initgraph(&gd, &gm, " "); outtextxy(40,90,"This Program is to draw a smile face "); circle(200,200,80); circle(170,180,8); circle(230,180,8); drawpoly(4,points); arc(200,230,180,360,20); getch(); closegraph(); }

Output of program:

/* WAP to draw Sin and Cos Waves */


#include<stdio.h> #include<conio.h> #include<math.h> #include<graphics.h> float sinfunc(float); float cosfunc(float); void sindisp(); void cosdisp();

float x[40],y[40]; int t1,t2; void main() { int i; int xmax,ymax; clrscr(); sindisp(); cosdisp(); getch(); }

float sinfunc(float val) { float res; res=t1*sin(val); return res; } float cosfunc(float val) { float res; res=t2*cos(val); return res; } void sindisp() { int i; int gd=DETECT,gm,errorcode; clrscr(); initgraph(&gd,&gm, " "); setcolor(CYAN);

outtextxy(40,150,"For the graph of form asinx"); outtextxy(40,40,"<- Enter the value of a");

gotoxy(1,3); fflush(stdin); scanf("%d",&t1);

x[0]=0.0;

for(i=1;i<40;i++) { x[i]=x[0]+(5.8*i); }

for(i=0;i<40;i++) { y[i]=sinfunc(x[i]); }

outtextxy(40,170,"Graph is : "); line(10+2*x[0],300+20*y[0],385,300+20*y[0]);

for(i=0;i<40;i++) { line(10+2*x[i],300+20*y[i],10+2*x[i+1],300+20*y[i+1]); }

getch(); }

void cosdisp() { int i; int gd=DETECT, gm, errorcode; clrscr(); initgraph(&gd,&gm, " "); x[0]=0.0; setcolor(5); outtextxy(40,150,"For the graph of from bcosx"); outtextxy(40,40,"<-Enter the value of b");

gotoxy(1,3); fflush(stdin); scanf("%d",&t2);

for(i=0;i<40;i++) { x[i]=x[0]+(5.8*i); }

for(i=0;i<40;i++) { y[i]=cosfunc(x[i]); }

outtextxy(40,170,"Graph is : ");

line(10+2*x[0],336+20*0,430,336+20*0);

for(i=0;i<36;i++) { line(10+2*x[i],325+20*y[i],10+2*x[i+1],325+20*y[i+1]); } getch(); }

Output of program:
1.

2.