Download - C Graphics Tutorial
-
8/10/2019 C Graphics Tutorial
1/48
C graphics tutorialThis tutorial is for all those who wish to learn c graphics programming, no knowledge of
graphics concepts is required. C Graphics programming is very easy and interesting. You
can use graphics programming for developing your own games, in making projects, for
animation etc. Its not like traditional C programming in which you have to apply
comple! 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 li"rary functions
# need not worry if you dont know functions $ to get your task done. %ust you pass
arguments to the functions and its done. &n this we"site you will find almost all
functions with detailed e!planation and a sample program showing the usage of a
function. To make things easy you are provided with e!ecuta"le files which you can
download and e!ecute. 'irstly you should know the function initgraph which is used to
initiali(e the graphics mode . To initiali(e graphics mode we use initgraph function in ourprogram. initgraph function is present in )graphics.h) header file, so your every graphics
program should include )graphics.h) header file. *e will discuss initgraph withe help of
following sample program+
Sample graphics code#include#includeint main(){ int gd = DETECT, gm;
initgraph(gd, gm, !C"TC$%&!);
getch(); closegraph(); return ';
'irstly let me tell you what is the output of this program, this program initiali(es
graphics mode and then closes it after a key is pressed. To "egin with we have declared
two varia"les of int type gd and gm for graphics driver and graphics mode respectively,
you can choose any other varia"le name as you wish. -TCT is a macro defined in
)graphics.h) header file, then we have passed three arguments to initgraph function first
is the address of gd, second is the address of gm and third is the path where your /GI
files are present # you have to adjust this accordingly where you tur"o compiler is
installed$. Initgraph function automatically decides an appropriate graphics driver and
mode such that ma!imum screen resolution is set, getch helps us to wait until a key is
pressed, closegraph function closes the graphics mode and finally return statement
returns a value 0 to main indicating successful e!ecution of your program. 1fter you
have understood initgraph function then you can use functions to draw shapes such as
-
8/10/2019 C Graphics Tutorial
2/48
circle, line , rectangle etc, then you can learn how to change colors and fonts using
suita"le functions, then you can go for functions such as getimage, putimage etc for
doing animation.
c graphics programs1ll these program have "een made using c graphics. 2rogram for various type of charts
and other interesting things and patterns.
C graphics codes
Draw shapes using c graphicsThis c graphics program draws "asic shapes such as circle, line, rectangle, ellipse and
display te!t on screen using c graphics. This can "e a first graphics program for a"eginner.
C programming code#include#includemain(){ int gd = DETECT,gm,let=*'',top=*'',right=+'',ottom=+'',-= '',/=*0',radius=0';
initgraph(gd, gm, !C"TC$%&!);
rectangle(let, top, right, ottom);
circle(-, /, radius); ar(let 1 '', top, right 1 '', ottom); line(let 2 *', top 1 *0', let 1 3*', top 1 *0'); ellipse(-, / 1 +'', ', 4', *'', 0'); outte-t-/(let 1 *'', top 1 +0, !5/ 6irst C %raphics 7rogram!);
getch(); closegraph(); return ';
-
8/10/2019 C Graphics Tutorial
3/48
&utput of program+
c program draw bar chartThis program draws "ar chart using c graphics. Chart is drawn using "ars filled with
different styles and in different colors.
C programming code#include#includemain(){ int gd = DETECT, gm;
initgraph(gd, gm, !C"TC$%&!);
setcolor(8E99:); rectangle(',',4,30'); sette-tst/le(?@EA&6@6:?T,B:A&@D&A,+); setcolor(B&TE); outte-t-/(+0,',!$ar Chart!);
setlinest/le(:9&D@9&?E,',+);
-
8/10/2019 C Graphics Tutorial
4/48
line(*'',3+',*'',4'); line(*'',3+',4'',3+'); line(',',*'',4'); line(**',',*'',4'); line(0',3*',4'',3+'); line(0',3',4'',3+');
outte-t-/(0,0,!8!); outte-t-/(4*',3'0,!!); outte-t-/(F0,3*0,!:!);
setGllst/le(9&?E@6&99,$9HE); ar(*0',*'',+'',3*);
setGllst/le(BTCB@6&99,AED); ar(++0,*0',+0,3*);
setGllst/le(&DE@D:T@6&99,%AEE?); ar('',+'',0',3*);
setGllst/le(&?TEA9EIE@6&99,5%E?T); ar(0,*+0,3+0,3*);
setGllst/le(BTCB@6&99,$A:?); ar(30',*0,0'',3*);
getch(); return ';
&utput of program+
-
8/10/2019 C Graphics Tutorial
5/48
c program to draw pie chartThis c program draws a pie chart showing percentage of various components drawn with
different filling styles and colors.
C programming code34 2rogram to draw a pie chart 43
#include#includemain(){ int gd = DETECT, gm, mid-, mid/;
initgraph(gd, gm, !C"TC$%&!);
setcolor(5%E?T);
rectangle(',3',4,30'); sette-tst/le(?@EA&6@6:?T,B:A&@D&A,+); setcolor(B&TE); outte-t-/(+0,*',!7ie Chart!);
mid- = getma--()J+; mid/ = getma-/()J+;
setGllst/le(9&?E@6&99,$9HE); pieslice(mid-, mid/, ', 0, *''); outte-t-/(mid-1*'', mid/ 2 0, !+'.FK!);
setGllst/le(BTCB@6&99,AED); pieslice(mid-, mid/, 0, ++0, *''); outte-t-/(mid-2*0, mid/ 2 0, !3*.4K!);
setGllst/le(&DE@D:T@6&99,%AEE?); pieslice(mid-, mid/, ++0, 4', *''); outte-t-/(mid-10, mid/ 1 0, !.0'K!);
getch(); return ';
-
8/10/2019 C Graphics Tutorial
6/48
&utput of program+
C program to move a car2rogram in c using graphics move a car. 1 car is made using two rectangles and two
circles which act as tyres of car. 1 for loop is used to move the car forward "y changing
the rectangle and circle coordinates and erasing the previous contents on screen using
clearviewport, you can also use cleardevice. 5peed of car can "e adjusted using delay
function, more the delay lesser will "e the speed or lesser the delay your car will move
fast. In this program color of the car also keeps on changing, this is accomplished "y
incrementing the color value "y one each time in for loop, you can also use random
function for this purpose. /efore you see a car moving you will "e asked to press a key.
C programming code#include #include #include
main(){ int i, L = ', gd = DETECT, gm;
initgraph(gd,gm,!C"TC$%&!);
sette-tst/le(DE6H9T@6:?T,B:A&@D&A,+);
outte-t-/(+0,+3',!7ress an/ Me/ to NieO the moNing car!);getch();
setNieOport(',',4,33',*);
or( i = ' ; i
-
8/10/2019 C Graphics Tutorial
7/48
setcolor(L); dela/(*'');
i( i == 3+' ) reaM;
clearNieOport();
getch(); closegraph(); return ';
c program to draw a 3d bar chartThis c program draws a 6d "ar chart.
C programming code#include#includemain(){ int gd = DETECT, gm;
initgraph(gd, gm, !C"TC$%&!);
setcolor(8E99:); rectangle(',',4,30'); sette-tst/le(?@EA&6@6:?T,B:A&@D&A,+); setcolor(B&TE); outte-t-/(+0,',!$ar Chart!);
setlinest/le(:9&D@9&?E,',+);
line(*'',3+',*'',4');
line(*'',3+',4'',3+'); line(',',*'',4'); line(**',',*'',4'); line(0',3*',4'',3+'); line(0',3',4'',3+');
outte-t-/(0,0,!8!); outte-t-/(4*',3'0,!!); outte-t-/(F0,3*0,!:!);
setGllst/le(9&?E@6&99,$9HE); ar(*0',*'',+'',3*);
setGllst/le(BTCB@6&99,AED); ar(++0,*0',+0,3*);
setGllst/le(&DE@D:T@6&99,%AEE?); ar('',+'',0',3*);
setGllst/le(&?TEA9EIE@6&99,5%E?T); ar(0,*+0,3+0,3*);
setGllst/le(BTCB@6&99,$A:?); ar(30',*0,0'',3*);
-
8/10/2019 C Graphics Tutorial
8/48
-
8/10/2019 C Graphics Tutorial
9/48
circle(0',*'',+0); setGllst/le(:9&D@6&99,8E99:); QoodGll(0',*'',8E99:);
setcolor($9CR); setGllst/le(:9&D@6&99,$9CR); Gllellipse(33,F0,+,4); Gllellipse(04,F0,+,4);
ellipse(0',*'',+'0,0,+',); ellipse(0',*'',+'0,0,+',*'); ellipse(0',*'',+'0,0,+',**);
area = imagesiSe(let, top, let 1 0', top 1 0'); p = malloc(area);
setcolor(B&TE); sette-tst/le(?@EA&6@6:?T,B:A&@D&A,+); outte-t-/(*00,30*,!miling 6ace nimation!);
setcolor($9HE); rectangle(',',4,33);
Ohile(Mhit()) { temp* = * 1 random ( 0FF ); temp+ = * 1 random ( F' );
getimage(let, top, let 1 0', top 1 0', p); putimage(let, top, p, :A@7HT); putimage(temp* , temp+, p, :A@7HT); dela/(*''); let = temp*; top = temp+;
getch(); closegraph(); return ';
&utput of program+
-
8/10/2019 C Graphics Tutorial
10/48
captcha program in cThis program generates captcha, a captcha is a random code generated using some
algorithm. *e will use random function in our code. These are used in typing tutors and
in we"site to check whether a human is operating on a we"site.
C programming code#include#include#includemain(){ int i = ', Me/, num, mid-, gd = DETECT, gm; char aU*'V;
initgraph(gd,gm,!C"TC$%&!);
mid- = getma--()J+;
sette-tst/le(CA&7T@6:?T,B:A&@D&A,0); sette-tLusti/(CE?TEA@TET,CE?TEA@TET); setcolor(%AEE?); outte-t-/(mid-,+',!C7TCB!); sette-tst/le(CA&7T@6:?T,B:A&@D&A,+); outte-t-/(mid-,*+0,!7ress an/ Me/ to change the generated random code !captcha!!); outte-t-/(mid-,*0',!7ress escape Me/ to e-it...!);
setcolor(B&TE); setNieOport(*'',+'',4'',3'',*); setcolor(AED); randomiSe();
Ohile(*)
{ Ohile(i
-
8/10/2019 C Graphics Tutorial
11/48
-
8/10/2019 C Graphics Tutorial
12/48
-
8/10/2019 C Graphics Tutorial
13/48
-
8/10/2019 C Graphics Tutorial
14/48
-
8/10/2019 C Graphics Tutorial
15/48
int initmouse(){ i.-.a- = '; intF4('-,i,o); return ( o.-.a- );
Noid get@input(){ int -, /;Ohile(*)
{ getmousepos(-,/);
JP mouse pointer in let o utton PJ
i( - >= (let2) / >= (top2) / (let1*'')) { draO@ar(); let = let 2 3;
i (let < *') let = 0';
draO@utton(let,top);
JP mouse pointer aoNe utton PJ
else i(->(let2) />=(top2) /(let2)/>(top1')/
-
8/10/2019 C Graphics Tutorial
16/48
i (top < *F')
top = +';
draO@utton(let,top);
i (Mhit()) { i (getMe/() == *) e-it(E&T@HCCE); int getMe/(){ i.h.ah = '; intF4(++,i,o);return( o.h.ah );
main(){ initialiSe();get@input();
return ';
c program to restrict mousepointer in a circle
This program restricts mouse pointer in a circle i.e you cant move mouse out of a circle.
*hen you try to "ring mouse pointer outside the circle, mouse pointer is moved to its
previous location which is inside the circle. Code to restrict mouse in circle is given "elow
+
C programming code#include#include#include#include#include
union AE% i, o;int initmouse(){ i.-.a- = '; intF4(', i, o); return ( o.-.a- );Noid shoOmouseptr()
-
8/10/2019 C Graphics Tutorial
17/48
-
8/10/2019 C Graphics Tutorial
18/48
closegraph(); return ';
C graphics function
arc function in c-eclaration + void arc#int !, int y, int stangle, int endangle, int radius$7
arc function is used to draw an arc with center #!,y$ and stangle specifies starting angle,
endangle specifies the end angle and last parameter specifies the radius of the arc. arc
function can also "e used to draw a circle "ut for that starting angle and end angle should
"e 0 and 680 respectively.
C programming source code for arc#include#includemain(){ int gd = DETECT, gm;
initgraph(gd, gm, !C"TC$%&!);
arc(*'', *'', ', *0, 0');
getch(); closegraph(); return ';
In the a"ove program #900,900$ are coordinates of center of arc, 0 is the starting angle,
96: is the end angle and :0 specifies the radius of the arc.
arcprogram e!ecuta"le.
&utput of program+
http://www.programmingsimplified.com/toolsArea/executable/ARC.EXEhttp://www.programmingsimplified.com/toolsArea/executable/ARC.EXE -
8/10/2019 C Graphics Tutorial
19/48
-
8/10/2019 C Graphics Tutorial
20/48
"ar6d function is used to draw a ;dimensional, rectangular filled in "ar . Coordinates of
left top and right "ottom corner of "ar are required to draw the "ar. left specifies the =
coordinate of top left corner, top specifies the Ycoordinate of top left corner, right
specifies the =coordinate of right "ottom corner, "ottom specifies the Ycoordinate of
right "ottom corner, depth specifies the depth of "ar in pi!els, topflag determines
whether a 6 dimensional top is put on the "ar or not # if it is non(ero then it is put
otherwise not $. Current fill pattern and fill color is used to fill the "ar. To change fill
pattern and fill color use setfillstyle.
C program of bar3d
#include#includemain(){ int gd = DETECT, gm;
initgraph(gd, gm, !C"TC$%&!);
ard(*'', *'', +'', +'', +', *);
getch(); closegraph(); return ';
&utput of program+
circle function in c-eclaration + void circle#int !, int y, int radius$7
circle function is used to draw a circle with center #!,y$ and third parameter specifies the
radius of the circle. The code given "elow draws a circle.
C program for circle
#include#include
-
8/10/2019 C Graphics Tutorial
21/48
main(){ int gd = DETECT, gm;
initgraph(gd, gm, !C"TC$%&!);
circle(*'', *'', 0');
getch(); closegraph(); return ';
In the a"ove program #900, 900$ are coordinates of center of the circle and :0 is the
radius of circle.
circleprogram.
&utput of program+
cleardevice function in c-eclaration + void cleardevice#$7
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 "ackground color.
C program for cleardevice#include#includemain()
{ int gd = DETECT, gm; initgraph(gd, gm, !C"TC$%&!);
outte-t(!7ress an/ Me/ to clear the screen.!); getch(); cleardeNice(); outte-t(!7ress an/ Me/ to e-it...!);
getch(); closegraph();
http://www.programmingsimplified.com/toolsArea/executable/CIRCLE.EXEhttp://www.programmingsimplified.com/toolsArea/executable/CIRCLE.EXEhttp://www.programmingsimplified.com/toolsArea/executable/CIRCLE.EXE -
8/10/2019 C Graphics Tutorial
22/48
return ';
>ote + -ont use clrscr in graphics mode.
closegraph function in cclosegraph function closes the graphics mode, deallocates all memory allocated "y
graphics system and restores the screen to the mode it was in "efore you called initgraph.
-eclaration + void closegraph#$7
C code of closegraph
#include#includemain(){
int gd = DETECT, gm;
initgraph(gd, gm, !C"TC$%&!);
outte-t(!7ress an/ Me/ to close the graphics mode...!);
getch(); closegraph(); return ';
drawpoly function in c
-rawpoly function is used to draw polygons i.e. triangle,rectangle,pentagon, he!agonetc.
-eclaration + void drawpoly# int num, int 4polypoints $7
num indicates #n?9$ num"er of points where n is the num"er of vertices in a polygon,
polypoints points to a sequence of #n4;$ integers . ach pair of integers gives ! and y
coordinates of a point on the polygon. *e specify #n?9$ points as first point coordinates
should "e equal to #n?9$thto draw a complete figure.
To understand more clearly we will draw a triangle using drawpoly, consider for e!amplethe array +
int points@A B 6;0, 9:0, D;0, 600, ;:0, 600, 6;0, 9:0E7
points array contains coordinates of triangle which are #6;0, 9:0$, #D;0, 600$ and #;:0,
600$. >ote that last point#6;0, 9:0$ in array is same as first. 5ee the program "elow and
then its output, it will further clear your understanding.
http://www.programmingsimplified.com/c/graphics.h/rectanglehttp://www.programmingsimplified.com/c/graphics.h/rectanglehttp://www.programmingsimplified.com/c/graphics.h/rectanglehttp://www.programmingsimplified.com/c/graphics.h/rectangle -
8/10/2019 C Graphics Tutorial
23/48
C program for drawpoly#include #include main(){ int gd=DETECT,gm,pointsUV={+',*0',3+','',+0','',+',*0';
initgraph(gd, gm, !C"TC$%&!);
draOpol/(3, points);
getch();
closegraph(); return ';
&utput of program+
ellipse function in c-eclarations of ellipse function +void ellipse#int !, int y, int stangle, int endangle, int !radius, int yradius$7
llipse is used to draw an ellipse #!,y$ are coordinates of center of the ellipse, stangle is
the starting angle, end angle is the ending angle, and fifth and si!th parameters specifies
the = and Y radius of the ellipse. To draw a complete ellipse strangles and end angle
should "e 0 and 680 respectively.
C programming code for ellipse#include#includemain(){ int gd = DETECT, gm;
initgraph(gd, gm, !C"TC$%&!);
ellipse(*'', *'', ', 4', 0', +0);
-
8/10/2019 C Graphics Tutorial
24/48
-
8/10/2019 C Graphics Tutorial
25/48
&utput of program+
fillpoly function in c'illpoly function draws and fills a polygon. It require same arguments asdrawpoly.
-eclaration + void drawpoly# int num, int 4polypoints $7
'or details of arguments seedrawpoly.
fillpoly fills using current fill pattern and color which can "e changed using setfillstyle.
C programming code#include #include main(){ int gd=DETECT,gm,pointsUV={+',*0',33',3',+',3',+',*0';
initgraph(gd, gm, !C"TC$%&!);
Gllpol/(3, points);
getch(); closegraph();
return ';
http://www.programmingsimplified.com/c/graphics.h/drawpolyhttp://www.programmingsimplified.com/c/graphics.h/drawpolyhttp://www.programmingsimplified.com/c/graphics.h/drawpolyhttp://www.programmingsimplified.com/c/graphics.h/drawpolyhttp://www.programmingsimplified.com/c/graphics.h/setfillstylehttp://www.programmingsimplified.com/c/graphics.h/setfillstylehttp://www.programmingsimplified.com/c/graphics.h/drawpolyhttp://www.programmingsimplified.com/c/graphics.h/drawpolyhttp://www.programmingsimplified.com/c/graphics.h/setfillstyle -
8/10/2019 C Graphics Tutorial
26/48
&utput of program+
floodfill function-eclaration + void floodfill#int !, int y, int "order$7
floodfill function is used to fill an enclosed area. Current fill pattern and fill color is used
to fill the area.#!, y$ is any point on the screen if #!,y$ lies inside the area then inside will
"e filled otherwise outside will "e filled,"order specifies the color of "oundary of area. To
change fill pattern and fill color use setfillstyle. Code given "elow draws a circle and then
fills it.
C programming code#include#includemain(){ int gd = DETECT, gm;
initgraph(gd, gm, !C"TC$%&!);setcolor(AED);
circle(*'',*'',0'); QoodGll(*'',*'',AED);
getch(); closegraph(); return ';
-
8/10/2019 C Graphics Tutorial
27/48
In the a"ove program a circle is drawn in F- color. 2oint #900,900$ lies inside the circle
as it is the center of circle, third argument to floodfill is F- which is color of "oundary
of circle. 5o the output of a"ove program will "e a circle filled with *IT color as it is
the default fill color.
floodfillprogram.
&utput of program+
getarcoords function in c-eclaration + void getarccoords#struct arccoordstype 4var$7
getarccoords function is used to get coordinates of arc which is drawn most recently.
arccoordstype is a predefined structure which is defined as follows+
struct arccoordst/pe{ int -, /; JP center point o arc PJ int -start, /start; JP start position PJ int -end, /end; JP end position PJ;
address of a structure varia"le of type arccoordstype is passed to function getarccoords.
C program of getarccoords
http://www.programmingsimplified.com/toolsArea/executable/FLOODFILL.EXEhttp://www.programmingsimplified.com/toolsArea/executable/FLOODFILL.EXE -
8/10/2019 C Graphics Tutorial
28/48
#include#include#includemain(){ int gd = DETECT, gm; struct arccoordst/pe a; char arrU*''V;
initgraph(gd, gm,!C"TC$%&!);
arc(+0',+'',',',*''); getarccoords(a);
sprint(arr,!(Kd, Kd)!,a.-start,a./start); outte-t-/(4',*0,arr);
sprint(arr,!(Kd, Kd)!,a.-end,a./end); outte-t-/(+30,F0,arr);
getch(); closegraph();
return ';
In the a"ove program we have drawn an arc and then we get the coordinates of end
points of arc using getarccoords.Coordinates so o"tained are displayed using outte!t!y.
getbkcolor function in cget"kcolor function returns the current "ackground color
-eclaration + int get"kcolor#$7
e.g. color B get"kcolor#$7 33 color is an int varia"le
if current "ackground color is GF> then color will "e ;.
C program for getbkcolor#include#includemain(){ int gd = DETECT, gm, Mcolor; char aU*''V;
initgraph(gd,gm,!C"TC$%&!);Mcolor = getMcolor();
sprint(a,!Current acMground color = Kd!, Mcolor);
outte-t-/( *', *', a);
getch(); closegraph(); return ';
-
8/10/2019 C Graphics Tutorial
29/48
getcolor functiongetcolor function returns the current drawing color.
-eclaration + int getcolor#$7
e.g. a B getcolor#$7 33 a is an integer varia"le
if current drawing color is *IT then a will "e 9:.
5eecolors in c graphics.
C programming code for getcolor#include#includemain(){
int gd = DETECT, gm, draOing@color; char aU*''V;
initgraph(gd,gm,!C"TC$%&!);
draOing@color = getcolor();
sprint(a,!Current draOing color = Kd!, draOing@color); outte-t-/( *', *', a );
getch(); closegraph(); return ';
getdrivername functiongetdrivername function returns a pointer to the current graphics driver.
C program for getdrivername#include#includemain(){ int gd = DETECT, gm;
char PdriNername;
initgraph(gd, gm, !C"TC$%&!);
driNername = getdriNername();
outte-t-/(+'', +'', driNername);
getch(); closegraph(); return ';
http://www.programmingsimplified.com/c/graphics.h/colorshttp://www.programmingsimplified.com/c/graphics.h/colorshttp://www.programmingsimplified.com/c/graphics.h/colors -
8/10/2019 C Graphics Tutorial
30/48
getimage function in cgetimage function saves a "it image of specified region into memory, region can "e any
rectangle.
-eclaration+ void getimage#int left, int top, int right, int "ottom, void 4"itmap$7
getimage copies an image from screen to memory.
-
8/10/2019 C Graphics Tutorial
31/48
C program for getmaxx#include#includemain(){ int gd = DETECT, gm, ma-@-;
char arra/U*''V;
initgraph(gd,gm,!C"TC$%&!);
ma-@- = getma--();
sprint(arra/, !5a-imum coordinate or current graphics mode and driNer = Kd.!,ma-@-); outte-t(arra/);
getch(); closegraph(); return ';
getmaxy function in cgetma!y function returns the ma!imum Y coordinate for current graphics mode and
driver.
-eclaration + int getma!y#$7
C program for getmaxy#include#include
main(){ int gd = DETECT, gm, ma-@/; char arra/U*''V;
initgraph(gd,gm,!C"TC$%&!);
ma-@/ = getma-/();
sprint(arra/, !5a-imum 8 coordinate or current graphics mode and driNer is = Kd.!,ma-@/); outte-t(arra/);
getch(); closegraph(); return ';
getpixel function in cgetpi!el function returns the color of pi!el present at location#!, y$.
-eclaration + int getpi!el#int !, int y$7
-
8/10/2019 C Graphics Tutorial
32/48
C program for getpixel#include#includemain(){ int gd = DETECT, gm, color;
char arra/U0'V;initgraph(gd,gm,!C"TC$%&!);
color = getpi-el(', ');
sprint(arra/,!color o pi-el at (',') = Kd!,color);
outte-t(arra/);getch();
closegraph(); return ';
1s we havent drawn anything on screen and "y default screen is /
-
8/10/2019 C Graphics Tutorial
33/48
-eclaration + int gety#$7
C programming source code for gety#include #include main(){ int gd = DETECT, gm, /; char arra/U*''V;
initgraph(gd, gm, !C"TC$%&!);
/ = get/();
sprint(arra/, !Current position o / = Kd!, /);
outte-t(arra/);
getch(); closegraph();
return ';
graphdefaults function in cgraphdefaults function resets all graphics settings to their defaults.
-eclaration + void graphdefaults#$7
It resets the following graphics settings +
5ets the viewport to the entire screen.
oves the current position to #0,0$.
5ets the default palette colors, "ackground color, and drawing color.
5ets the default fill style and pattern.
5ets the default te!t font and justification.
C programming source code for graphdefaults#include #include main()
{ int gd = DETECT, gm;
initgraph(gd, gm, !C"TC$%&!);
setcolor(AED); setMcolor(8E99:);
circle(+0', +0', 0');
getch();
-
8/10/2019 C Graphics Tutorial
34/48
graphdeaults();
getch(); closegraph(); return ';
In the a"ove program we have first changed the drawing color to F- and "ackground
color to Y
-
8/10/2019 C Graphics Tutorial
35/48
imagesi(e function returns the num"er of "ytes required to store a "itimage. This
function is used when we are usinggetimage.
-eclaration+ unsigned int imagesi(e#int left, int top, int right, int "ottom$7
C programming code for imagesize#include #include main(){ int gd = DETECT, gm, /tes; char arra/U*''V;
initgraph(gd, gm, !C"TC$%&!);
circle(+'', +'', 0'); line(*0', +'', +0', +''); line(+'', *0', +'', +0');
/tes = imagesiSe(*0', *0', +0', +0'); sprint(arra/, !?umer o /tes reXuired to store reXuired area = Kd!, /tes); outte-t-/(*', +F', arra/);
getch(); closegraph(); return ';
&utput of program+
line function in cline function is used to draw a line from a point#!9,y9$ to point#!;,y;$ i.e. #!9,y9$ and
#!;,y;$ are end points of the line.The code given "elow draws a line.
-eclaration + void line#int !9, int y9, int !;, int y;$7
http://www.programmingsimplified.com/c/graphics.h/getimagehttp://www.programmingsimplified.com/c/graphics.h/getimagehttp://www.programmingsimplified.com/c/graphics.h/getimage -
8/10/2019 C Graphics Tutorial
36/48
C programming code for line#include#includemain(){ int gd = DETECT, gm;
initgraph(gd, gm, !C"TC$%&!);
line(*'', *'', +'', +'');
getch();
closegraph(); return ';
1"ove program draws a line from #900, 900$ to #;00, ;00$.
lineprogram.
&utput of program+
lineto function in clineto function draws a line from current position#C2$ to the point#!,y$, you can getcurrent position using get! and gety function.
C programming code for lineto
#include#include
http://www.programmingsimplified.com/toolsArea/executable/LINE.EXEhttp://www.programmingsimplified.com/toolsArea/executable/LINE.EXEhttp://www.programmingsimplified.com/toolsArea/executable/LINE.EXE -
8/10/2019 C Graphics Tutorial
37/48
main(){ int gd = DETECT, gm;
initgraph(gd, gm, !C"TC$%&!);
moNeto(*'', *''); lineto(+'', +'');
getch(); closegraph(); return ';
linerel function in c
-
8/10/2019 C Graphics Tutorial
38/48
outte-t(msg);
moNeto(0', 0');
sprint(msg, ! = Kd, 8 = Kd!, get-(), get/());
outte-t(msg);
getch();
closegraph(); return ';
moverel function in cmoverel function moves the current position to a relative distance.
-eclaration + void moverel#int !, int y$7
C programming code for moverel#include #include main(){ int gd = DETECT, gm, -, /; char messageU*''V;
initgraph(gd, gm, !C"TC$%&!);
moNeto(*'', *''); moNerel(*'', 2*'');
- = get-(); / = get/();
sprint(message, !Current - position = Kd and / position = Kd!, -, /); outte-t-/(*', *', message);
getch(); closegraph(); return ';
outtext function
outte!t function displays te!t at current position.
-eclaration + void outte!t#char 4string$7
C programming code for outtext#include#includemain()
-
8/10/2019 C Graphics Tutorial
39/48
{ int gd = DETECT, gm;
initgraph(gd, gm, !C"TC$%&!);
outte-t(!To displa/ te-t at a particular position on the screen use outte-t-/!);
getch(); closegraph(); return ';
-o not use te!t mode functions like printf, goto!y etc while working in graphics mode.
1lso note Jn or other escape sequences do not work in graphics mode. You have to
ensure that the te!t doesnt go "eyond the screen while using outte!t.
outtextxy function in coutte!t!y function display te!t or string at a specified point#!,y$ on the screen.
-eclaration + void outte!t!y#int !, int y, char 4string$7
!, y are coordinates of the point and third argument contains the address of string to "e
displayed.
C programming code for outtextxy#include#includemain(){ int gd = DETECT, gm;
initgraph(gd,gm,!C"TC$%&!);
outte-t-/(*'', *'', !:utte-t-/ unction!);
getch(); closegraph(); return ';
pieslice function in c#include#includemain(){ int gd = DETECT, gm;
initgraph(gd, gm, !C"TC$%&!);
pieslice(+'',+'',',*0,*'');
getch(); closegraph(); return ';
-
8/10/2019 C Graphics Tutorial
40/48
&utput of program+
putimage function in cputimage function outputs a "it image onto the screen.
-eclaration+ void putimage#int left, int top, void 4ptr, int op$7
putimage puts the "it image previously saved with getimage "ack onto the screen, with
the upper left corner of the image placed at #left, top$. ptr points to the area in memory
where the source image is stored. The op argument specifies a operator that controls how
the color for each destination pi!el on screen is computed, "ased on pi!el already on
screen and the corresponding source pi!el in memory.
5miling face animationprogram using putimage.
putpixel function in cputpi!el function plots a pi!el at location #!, y$ of specified color.
-eclaration + void putpi!el#int !, int y, int color$7
http://www.programmingsimplified.com/c/program/smiling-face-animationhttp://www.programmingsimplified.com/c/program/smiling-face-animationhttp://www.programmingsimplified.com/c/program/smiling-face-animation -
8/10/2019 C Graphics Tutorial
41/48
'or e!ample if we want to draw a GF> color pi!el at #6:, D:$ then we will write
putpi!el#6:, 6:, GF>$7 in our c program, putpi!el function can "e used to draw
circles, lines and ellipses using various algorithms.
C programming code for putpixel#include#includemain(){ int gd = DETECT, gm;
initgraph(gd, gm, !C"TC$%&!);
putpi-el(+0, +0, AED);
getch(); closegraph(); return ';
&utput of this program will "e a F- pi!el on screen at #;:, ;:$ . Try to spot that pi!el
with your eyes at left top portion of your computer screen.
rectangle function in c-eclaration + void rectangle#int left, int top, int right, int "ottom$7
rectangle function is used to draw a rectangle. Coordinates of left top and right "ottom
corner are required to draw the rectangle. left specifies the =coordinate of top left
corner, top specifies the Ycoordinate of top left corner, right specifies the =coordinate
of right "ottom corner, "ottom specifies the Ycoordinate of right "ottom corner. The
code given "elow draws a rectangle.
c programming code for rectangle#include#includemain(){ int gd = DETECT, gm;
initgraph(gd, gm, !C"TC$%&!);
rectangle(*'',*'',+'',+'');
getch(); closegraph(); return ';
sector function in c
-
8/10/2019 C Graphics Tutorial
42/48
5ector function draws and fills an elliptical pie slice.
-eclaration + void sector# int !, int y, int stangle, int endangle, int !radius, int yradius$7
C programming code for sector#include #include main(){ int gd = DETECT, gm;
initgraph(gd, gm, !C"TC$%&!);
sector(*'', *'', ', *0, +0, 0);
getch(); closegraph(); return ';
setbkcolor function in c-eclaration + void set"kcolor#int color$7
set"kcolor function changes current "ackground color e.g. set"kcolor#Y
-
8/10/2019 C Graphics Tutorial
43/48
In Tur"o Graphics each color is assigned a num"er. Total 98 colors are availa"le. 5trictly
speaking num"er of availa"le colors depends on current graphics mode and driver.'or
!ample + /
-
8/10/2019 C Graphics Tutorial
44/48
-ifferent fill styles+
enum Gll@st/les{
E57T8@6&99,:9&D@6&99,9&?E@6&99,9T9B@6&99,9B@6&99,
$R9B@6&99,9T$R9B@6&99,BTCB@6&99,BTCB@6&99,&?TEA9EIE@6&99,
&DE@D:T@6&99,C9:E@D:T@6&99,HEA@6&99
;
C programming source code for setfillstyle#include
#includemain(){ int gd = DETECT, gm;
initgraph(gd, gm, !C"TC$%&!);
setGllst/le(BTCB@6&99, AED); circle(*'', *'', 0'); QoodGll(*'', *'', B&TE);
getch(); closegraph(); return ';
&utput of program+
setlinestyle in c
-
8/10/2019 C Graphics Tutorial
45/48
-eclaration+
void setlinestyle# int linestyle, unsigned upattern, int thickness $7
1vaila"le line styles+
enum line@st/les{
:9&D@9&?E,D:TTED@9&?E,CE?TEA@9&?E,DBED@9&?E,HEA$&T@9&?E
;
C programming code#include main(){
int gd = DETECT, gm, c , - = *'', / = 0';initgraph(gd, gm, !C"TC$%&!);
or ( c = ' ; c < 0 ; c11 )
{ setlinest/le(c, ', +);
line(-, /, -1+'', /); / = / 1 +0;
getch();closegraph();
return ';
settextstyle function in c5ette!tstyle function is used to change the way in which te!t appears, using it we can
modify the si(e of te!t, change direction of te!t and change the font of te!t.
-eclaration + void sette!tstyle# int font, int direction, int charsi(e$7
font argument specifies the font of te!t, -irection can "e &FIKL-IF #
-
8/10/2019 C Graphics Tutorial
46/48
TA&79E@CA@6:?T, C:579E@6:?T,
EHA:7E?@6:?T,$:9D@6:?T
;
C programming source code for settextstyle
#include #include main(){ int gd = DETECT, gm, - = +0, / = +0, ont = '; initgraph(gd,gm,!C"TC$%&!);
or ( ont = ' ; ont
-
8/10/2019 C Graphics Tutorial
47/48
left, top, right, "ottom are the coordinates of main diagonal of rectangle in which we
wish to restrict our drawing. 1lso note that the point #left, top$ "ecomes the new origin.
C programming source code for setviewport#include
#includemain(){ int gd = DETECT, gm, mid-, mid/;
initgraph(gd, gm, !C"TC$%&!);
mid- = getma--()J+; mid/ = getma-/()J+;
setNieOport(mid- 2 0', mid/ 2 0', mid- 1 0', mid/ 1 0', *); circle(0', 0', 00);
getch(); closegraph(); return ';
&utput of program+
textheight function in cte!theight function returns the height of a string in pi!els.
-eclaration + int te!theight#char 4string$7
-
8/10/2019 C Graphics Tutorial
48/48
C programming source code for textheight#include#includemain(){ int gd = DETECT, gm, height;
char arra/U*''V;
initgraph(gd, gm, !C"TC$%&!);
height = te-theight(!C programming!);
sprint(arra/,!Te-theight = Kd!,height); outte-t(arra/);
getch(); closegraph(); return ';
textwidth function in cte!twidth function returns the width of a string in pi!els.
-eclaration + int te!twidth#char 4string$7
C programming source code for textwidth#include#includemain(){
int gd = DETECT, gm, Oidth; char arra/U*''V;
initgraph(gd, gm, !C"TC$% &!);
Oidth = te-tOidth(!C programming!);
sprint(arra/,!Te-tOidth = Kd!,Oidth); outte-t(arra/);
getch(); closegraph(); return ';