Basic Functions of C Graphics
Basic Functions of C Graphics
Syntax of arc in c graphics is arc(int x, int y, int starting-angle,int ending-angle, int radius);. Arc is used to draw circular arc. It takes 5 arguments, all of the int type. First two arguments define the center of the arc to place on the screen. hird and fourth arguments are starting and ending angles of the arc. Fifth argument is the radius of the arc in pixels.
int main( ) { int gd=DETECT, gm; initgraph( gd, gm,!C"##tc##$gi!); arc(%&',%&',(',)*',+'); getch(); closegraph(); return '; ,
Syntax to draw a !ar is bar(int left,int top,int right,int bottom) "ar is used to draw rectangular !ar. It takes # arguments, all of the int datatypes. First two arguments are left$top corner of the !ar %ast two arguments are right$!ottom corner of the !ar.
int main( ) { int gd=DETECT, gm; initgraph( gd, gm,!C"##tc##$gi!); $ar(%'',%'',-'',.''); getch();
Syntax to draw & dimensional !ar is bar3d(int left.int top,int right,int bottom, int depth,int topflag) !ar&d is used to draw & dimensional rectangular !ar. It takes ' arguments, all of the int datatype. First two arguments are left$top corner of the !ar. hird and fourth arguments are right !ottom corner of the !ar. Fifth arguments is for specified depth of the !ar in pixels. Sixth argument is for determines whether a & dimensional top is put on the !ar or not.(if it is non )ero then it put other wise not.*
int main( ) { int gd=DETECT, gm; initgraph( gd, gm,!C"##tc##$gi!); $ar.d(%'',%'',-'',.'',-',('); getch(); closegraph(); return '; ,
Circle
C program to draw a circle in c graphics.
Syntax of drawing circle in c graphics is circle(int x, int y, int radius). he first two arguments are used to define center of the circle in x and y co$ordinates. hird argument of the circle is its radius in pixels.
int main( ) { int gd=DETECT, gm; initgraph( gd, gm,!C"##tc##$gi!); circle(%&',%&',('); getch(); closegraph(); return '; ,
fillpoly is also used to draw and fill polygon. setfillstyle is used to apply different style to fill color and pattern in polygon.
int main( ) { int gd=DETECT, gm; int points/0={%&',%.',.&',%.',.&',-'',)&',-'',%&',%.',; initgraph( gd, gm,!C"##tc##$gi!); dra1pol2(&,points); getch(); closegraph(); return '; ,
Syntax of ellipse is ellipse(int midx,int midy, int startingangle, int endingangle,int radiusx,int radiusy);
2llipse is used to draw an elliptical arc. 2llipse takes ' arguments, all of the int type. First two argument define the center of the ellipse to place on the screen. hird and fourth argument are starting and ending angles of the ellipse. Fifth argument is the radius of the ellipse in x axis and sixth argument is the radius of the ellipse in y axis.
int points/0={%&',%.',.&',%.',.&',-'',)&',-'',%&',%.',; initgraph( gd, gm,!C"##tc##$gi!); 4illpol2(&,points); getch(); closegraph(); return '; ,
int main( ) { int gd=DETECT, gm; char msg/&'0; initgraph( gd, gm,!C"##tc##$gi!); mo5eto().',)''); sprint4(msg,!6a7imum 5alue o4 8" 9d!,getma72()); outte7t(msg); getch(); closegraph(); return '; ,
getmaxy(); hese function are +ery useful when we need to draw full screen rectanlge !order for our drawing. "or #xample: rectangle( $, $, getmaxx(), getmaxy() );
int main( ) { int gd=DETECT, gm; char msg/&'0; initgraph( gd, gm,!C"##tc##$gi!); mo5eto()'',%3'); sprint4(msg,!Current position o4 :" 9d!,get2()); outte7t(msg); getch();
#include<graphics.h> #include<conio.h> main() { int gd = DETECT, gm; initgraph( gd, gm, !C"##TC##;<=!);
using getx andgety function. And we can set the current position of cursor !y using mo%eto function. And we can also set the line style !y using setlinestyle function. line function takes four argument to draw line. All are integer datatype. First two argument is for set the starting position of x and y coordinate of line and another two argument is for set the ending position of x and y coordinate of line. "ut in lineto function the starting position of line is current position of cursor.
>o+eto function is used with outtext function to display text or string at particular location on screen.
int main( ) { int gd=DETECT, gm; char msg/0=!Demonstration o4 mo5eto 4unction!; initgraph( gd, gm,!C"##tc##$gi!); set$>color(+); mo5eto()'',%-'); outte7t(!?rogrammingcampus.com!); mo5eto()'',%*'); outte7t(msg); getch(); closegraph(); return '; ,
"or example: Current position is (-11,/11* after use of mo+eref(/11,&11*, now current position of text is (&11,511*.
pieslice function of c graphics is used to draw the circle and this circle is filled with color and?or style. In contrast of pieslice circle only draw the outline of the circle. General syntax of pieslice function in c graphics is: pieslice(int xpos, int ypos, int sangle, int eangle, int radius); pieslice function takes 5 argument, all are integer datatype. First two is the center point of pieslice on 3 and 7 coordinate respecti+ely. hird and fourth argument is starting and ending angle of pieslice function respecti+ely. And fifth argument is the radius of pieslice. pieslice is fill the color and style !y using setfillstyle function of c graphics.
main() { int gd = DETECT, gm; initgraph( gd, gm, !C"##TC##;<=!); set4illst2le(),&); pieslice(.'',)&',',%3',%''); getch(); closegraph(); return '; ,
Syntax of rectangle function is rectangle(left, top, right, bottom)@ Aectangle is used to draw empty rectangle. It takes # arguments all of int types. First two arguments are left$top corner of the rectangle and last two arguments are right$!ottom corner of the rectangle.
int main( ) { int gd=DETECT, gm; initgraph( gd, gm,!C"##tc##$gi!); rectangle(%'',%'',-'',.''); getch(); closegraph(); return '; ,
General syntax of sector is sector(int x,int y,int sangle,int eangle,int xradius,int yradius) Sector is used to draw and fills elliptical pie slice. It takes ' arguments, all int data tyape. he first two arguments are used to define center of the circle in x and y co$ordinates. hird and fourth arguments are starting and ending angles of the sector. Fifth argument is the radius of the ellipse in x axis and sixth argument is the radius of the ellipse in y axis.
int main( ) { int gd=DETECT, gm; initgraph( gd, gm,!C"##tc##$gi!); sector(%&', %&', %', %-&, (&, +&); getch(); closegraph(); return '; ,
General syntax to set !ackground color is setb&color(int color). !kcolor is used to set !ackground color of the screen. Its take only one argument it may !e a int or char data type.
In set!kcolor function we can choose color !y its int +alue(1$-5* or !y color name 'he following table shown color name and it corresponding int %alue . Color BLACK BLUE GREEN CYAN RED MAGENTA BROWN LIGHTGRAY DARKGRAY LIGHTBLUE LIGHTGREEN LIGHTCYAN LIGHTRED LIGHTMAGENTA YELLOW WHITE 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 int value
int main( ) { int gd=DETECT, gm; initgraph( gd, gm,!C"##tc##$gi!); set$>color(%); getch(); closegraph();
return '; ,
9 10 11 12 13
'here are following color is a%ailable with setfillstyle function with its corresponding integer %alue. Color BLACK BLUE GREEN CYAN RED MAGENTA BROWN LIGHTGRAY DARKGRAY LIGHTBLUE LIGHTGREEN LIGHTCYAN LIGHTRED LIGHTMAGENTA YELLOW WHITE 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 int value
{ int gd=DETECT, gm; int points/0={%'',%&',.'',%&',.'',-'',)&',-'',%'',%&',; initgraph( gd, gm,!C"##tc##$gi!); set4illst2le(3,%); 4illpol2(&,points); getch(); closegraph(); return '; ,
setlinestyle function is used to set the line style. General syntax of setlinestyle function is : setlinestyle( int linestyle, unsigned upattern, int thic&ness); It takes & argument first one is the line style. here are following line style a+aila!le with the setlinestyle function with its corresponding integer +alue. Line Style OLID_LINE DOTTED_LINE CENTER_LINE DA HED_LINE U ERBIT_LINE 0 1 2 3 4 int value
he second argument is to use !it pattern and last one is for lineCs thickness it can !e - pixel or & pixel wide.
int main( ) { int gd=DETECT, gm; initgraph( gd, gm,!C"##tc##$gi!); setlinest2le(%,%,%); line(%'',%'',-'',%''); setlinest2le(D@ABEDCD=EE,%,.); line(%'',%&',-'',%&'); setlinest2le(),%,%);
In settextstyle function we can choose font style !y its int +alue or !y font style name. And direction of text !y int +alue( 1 or - * or !y direction name( 6:AID<;IA or 52A <;IA *. he following ta!le shown the font style name and its corresponding int +alue. Font Style Name DEFAULT_FONT TRIPLE!_FONT MALL_FONT AN _ ERIF_FONT GOTHIC_FONT CRIPT_FONT IMPLE!_FONT TRIPLE!_ CR_FONT COMPLE!_FONT EUROPEAN_FONT BOLD_FONT 0 1 2 3 4 5 6 7 8 9 10 int value
#include <graphics.h> #include <stdio.h> int main( ) { int gd=DETECT, gm; char msg/0=!Demonstration o4 outte7t 4unction!; initgraph( gd, gm,!C"##tc##$gi!); set$>color(+); mo5eto()'',%-'); outte7t(!?rogrammingcampus.com!); mo5eto()'',%*'); outte7t(msg); getch(); closegraph(); return '; ,
outtextxy is the function used to display the gi+en string on graphics mode at specific position . outtextxy function takes & argument, first two as int type which specify the position of string on screen. And last argument is char type which is text or string which display on screen. outtextxy function use its default font style to display gi+en string . o change font style we need to use settextstyle function !efore the outtextxy function. outtextxy function is an alternati+e of outtext function which specify particular location of string or text on the screen. General syntax of outtextxy function is : outtextxy(int x-pos, int y-pos, char string);
int main( ) { int gd=DETECT, gm; char msg/0=!Demonstration o4 outte7t72 4unction!; initgraph( gd, gm,!C"##tc##$gi!); set$>color(+); outte7t72()'',%.',!?rogrammingcampus.com!); outte7t72()'',%*',msg); getch(); closegraph(); return '; ,