Arc Function in C
Arc Function in C
Declaration :- void arc(int x, int y, int stangle, int endangle, int radius);
arc function is used to draw an arc with center (x,y) and stangle specifies starting angle,
endangle 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.
#include <graphics.h>
#include <conio.h>
main()
{
int gd = DETECT, gm;
getch();
closegraph();
return 0;
}
Bar function in c
Declaration :- void bar(int left, int top, int right, int bottom);
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.
C programming code
#include <graphics.h>
#include <conio.h>
main()
{
int gd = DETECT, gm;
getch();
closegraph();
return 0;
}
bar3d function in c
Declaration :- void bar3d(int left, int top, int right, int bottom, int depth, int topflag);
bar3d function is used to draw a 2-dimensional, rectangular filled in bar . Coordinates of left top
and right bottom corner of bar 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, depth specifies
the depth of bar in pixels, topflag determines whether a 3 dimensional top is put on the bar or
not ( if it is non-zero then it is put otherwise not ). Current fill pattern and fill color is used to fill
the bar. To change fill pattern and fill color use setfillstyle.
C program of bar3d
#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm;
getch();
closegraph();
return 0;
}
Circle function in c
Declaration :- void circle(int x, int y, int radius);
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.
main()
{
int gd = DETECT, gm;
getch();
closegraph();
return 0;
}
Cleardevice function in c
Declaration :- void 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.
main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
drawpoly function in c
Drawpoly function is used to draw polygons i.e. triangle, rectangle, pentagon, hexagon etc.
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.
To understand more clearly we will draw a triangle using drawpoly, consider for example the
array :-
int points[] = { 320, 150, 420, 300, 250, 300, 320, 150};
points array contains coordinates of triangle which are (320, 150), (420, 300) and (250, 300).
Note that last point(320, 150) in array is same as first. See the program below and then its
output, it will further clear your understanding.
main()
{
int gd=DETECT,gm,points[]={320,150,420,300,250,300,320,150};
drawpoly(4, points);
getch();
closegraph();
return 0;
}
ellipse function in c
Declarations of ellipse function :-
void ellipse(int x, int y, int stangle, int endangle, int xradius, int yradius);
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.
main()
{
int gd = DETECT, gm;
getch();
closegraph();
return 0;
}
fillellipse function in c
Declaration of fillellipse function :-
void fillellipse(int x, int y, int xradius, int yradius);
x and y are coordinates of center of the ellipse, xradius and yradius are x and y radius of ellipse
respectively.
int main()
{
int gd = DETECT, gm;
getch();
closegraph();
return 0;
}
fillpoly function in c
Fillpoly function draws and fills a polygon. It require same arguments as drawpoly.
C programming code
#include <graphics.h>
#include <conio.h>
main()
{
int gd=DETECT,gm,points[]={320,150,440,340,230,340,320,150};
fillpoly(4, points);
getch();
closegraph();
return 0;
}
Floodfill function
Declaration :- void floodfill(int x, int y, int border);
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.
C programming code
#include <graphics.h>
#include <conio.h>
main()
{
int gd = DETECT, gm;
setcolor(RED);
circle(100,100,50);
floodfill(100,100,RED);
getch();
closegraph();
return 0;
}
Getarcoords function in c
Declaration :- void getarccoords(struct arccoordstype *var);
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 arccoordstype
{
int x, y; /* center point of arc */
int xstart, ystart; /* start position */
int xend, yend; /* end position */
};
address of a structure variable of type arccoordstype is passed to function getarccoords.
C program of getarccoords
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
main()
{
int gd = DETECT, gm;
struct arccoordstype a;
char arr[100];
initgraph(&gd, &gm,"C:\\TC\\BGI");
arc(250,200,0,90,100);
getarccoords(&a);
sprintf(arr,"(%d, %d)",a.xstart,a.ystart);
outtextxy(360,195,arr);
sprintf(arr,"(%d, %d)",a.xend,a.yend);
outtextxy(245,85,arr);
getch();
closegraph();
return 0;
}
getbkcolor function in c
getbkcolor function returns the current background color
main()
{
int gd = DETECT, gm, bkcolor;
char a[100];
initgraph(&gd,&gm,"C:\\TC\\BGI");
bkcolor = getbkcolor();
getch();
closegraph();
return 0;
}
getcolor function
getcolor function returns the current drawing color.
main()
{
int gd = DETECT, gm, drawing_color;
char a[100];
initgraph(&gd,&gm,"C:\\TC\\BGI");
drawing_color = getcolor();
getch();
closegraph();
return 0;
}
getdrivername function
getdrivername function returns a pointer to the current graphics driver.
main()
{
int gd = DETECT, gm;
char *drivername;
drivername = getdrivername();
outtextxy(200, 200, drivername);
getch();
closegraph();
return 0;
}
C programming code
#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
main()
{
int gd = DETECT, gm, area, temp1, temp2, left = 25, top = 75;
void *p;
initgraph(&gd,&gm,"C:\\TC\\BGI");
setcolor(YELLOW);
circle(50,100,25);
setfillstyle(SOLID_FILL,YELLOW);
floodfill(50,100,YELLOW);
setcolor(BLACK);
setfillstyle(SOLID_FILL,BLACK);
fillellipse(44,85,2,6);
fillellipse(56,85,2,6);
ellipse(50,100,205,335,20,9);
ellipse(50,100,205,335,20,10);
ellipse(50,100,205,335,20,11);
setcolor(WHITE);
settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
outtextxy(155,451,"Smiling Face Animation");
setcolor(BLUE);
rectangle(0,0,639,449);
while(!kbhit())
{
temp1 = 1 + random ( 588 );
temp2 = 1 + random ( 380 );
getch();
closegraph();
return 0;
}
getmaxcolor function
getmaxcolor function returns maximum color value for current graphics mode and driver. Total
number of colors available for current graphics mode and driver are ( getmaxcolor() + 1 ) as
color numbering starts from zero.
main()
{
int gd = DETECT, gm, max_colors;
char a[100];
initgraph(&gd,&gm,"C:\\TC\\BGI");
max_colors = getmaxcolor();
getch();
closegraph();
return 0;
}
imagesize function in c
imagesize function returns the number of bytes required to store a bitimage. This function is
used when we are using getimage.
Declaration:- unsigned int imagesize(int left, int top, int right, int bottom);
main()
{
int gd = DETECT, gm, bytes;
char array[100];
getch();
closegraph();
return 0;
}
Line function in c
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
end points of the line.The code given below draws a line.
Declaration :- void line(int x1, int y1, int x2, int y2);
main()
{
int gd = DETECT, gm;
getch();
closegraph();
return 0;
}
lineto function in c
lineto function draws a line from current position(CP) to the point(x,y), you can get current
position using getx and gety function.
C programming code for lineto
#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm;
moveto(100, 100);
lineto(200, 200);
getch();
closegraph();
return 0;
}
moveto function in c
moveto function changes the current position (CP) to (x, y)
main()
{
int gd = DETECT, gm;
char msg[100];
outtext(msg);
moveto(50, 50);
outtext(msg);
getch();
closegraph();
return 0;
}
outtext function
outtext function displays text at current position.
main()
{
int gd = DETECT, gm;
getch();
closegraph();
return 0;
}
outtextxy function in c
outtextxy function display text or string at a specified point(x,y) on the screen.
initgraph(&gd,&gm,"C:\\TC\\BGI");
getch();
closegraph();
return 0;
}
sector function in c
Sector function draws and fills an elliptical pie slice.
Declaration :- void sector( int x, int y, int stangle, int endangle, int xradius, int yradius);
main()
{
int gd = DETECT, gm;
getch();
closegraph();
return 0;
}
Setbkcolor function in c
Declaration :- void setbkcolor(int color);
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.
main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
setbkcolor(GREEN);
getch();
closegraph();
return 0;
}
Setbkcolor function in c
Declaration :- void setbkcolor(int color);
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.
main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
outtext("Press any key to change the background color to GREEN.");
getch();
setbkcolor(GREEN);
getch();
closegraph();
return 0;
}
setcolor function in c
Declaration :- void setcolor(int color);
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.For Example
:- 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.
main()
{
int gd = DETECT, gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
getch();
closegraph();
return 0;
}
setfillstyle function in c
setfillstyle function sets the current fill pattern and fill color.
enum fill_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
};
main()
{
int gd = DETECT, gm;
setfillstyle(XHATCH_FILL, RED);
circle(100, 100, 50);
floodfill(100, 100, WHITE);
getch();
closegraph();
return 0;
}
setlinestyle in c
Declaration:
void setlinestyle( int linestyle, unsigned upattern, int thickness );
enum line_styles
{
SOLID_LINE,
DOTTED_LINE,
CENTER_LINE,
DASHED_LINE,
USERBIT_LINE
};
C programming code
#include <graphics.h>
main()
{
int gd = DETECT, gm, c , x = 100, y = 50;
getch();
closegraph();
return 0;
}
Settextstyle function in c
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.
main()
{
int gd = DETECT, gm, x = 25, y = 25, font = 0;
initgraph(&gd,&gm,"C:\\TC\\BGI");
getch();
closegraph();
return 0;
}
setviewport function in c
setviewport function sets the current viewport for graphics output.
Declaration :- void setviewport(int left, int top, int right, int bottom, int clip);
setviewport function is used to restrict drawing to a particular portion on the screen. For
example setviewport(100 , 100, 200, 200, 1);
will restrict our drawing activity inside the rectangle(100,100, 200, 200).
left, top, right, bottom are the coordinates of main diagonal of rectangle in which we wish to
restrict our drawing. Also note that the point (left, top) becomes the new origin.
main()
{
int gd = DETECT, gm, midx, midy;
midx = getmaxx()/2;
midy = getmaxy()/2;
getch();
closegraph();
return 0;
}
textheight function in c
textheight function returns the height of a string in pixels.
main()
{
int gd = DETECT, gm, height;
char array[100];
initgraph(&gd, &gm, "C:\\TC\\BGI");
sprintf(array,"Textheight = %d",height);
outtext(array);
getch();
closegraph();
return 0;
}
Textwidth function in c
Textwidth function returns the width of a string in pixels.
int main()
{
int gd = DETECT, gm, width;
char array[100];
sprintf(array,"Textwidth = %d",width);
outtext(array);
getch();
closegraph();
return 0;
}
Draw shapes using c graphics
This c graphics program draws basic shapes such as circle, line, rectangle, ellipse and display
text on screen using c graphics. This can be a first graphics program for a beginner.
C programming code
#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT,gm,left=100,top=100,right=200,bottom=200,x=
300,y=150,radius=50;
getch();
closegraph();
return 0;
}
C programming code
#include <graphics.h>
#include <conio.h>
main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
setcolor(YELLOW);
rectangle(0,30,639,450);
settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
setcolor(WHITE);
outtextxy(275,0,"Bar Chart");
setlinestyle(SOLID_LINE,0,2);
line(100,420,100,60);
line(100,420,600,420);
line(90,70,100,60);
line(110,70,100,60);
line(590,410,600,420);
line(590,430,600,420);
outtextxy(95,35,"Y");
outtextxy(610,405,"X");
outtextxy(85,415,"O");
setfillstyle(LINE_FILL,BLUE);
bar(150,100,200,419);
setfillstyle(XHATCH_FILL,RED);
bar(225,150,275,419);
setfillstyle(WIDE_DOT_FILL,GREEN);
bar(300,200,350,419);
setfillstyle(INTERLEAVE_FILL,MAGENTA);
bar(375,125,425,419);
setfillstyle(HATCH_FILL,BROWN);
bar(450,175,500,419);
getch();
return 0;
}
C programming code
/* Program to draw a pie chart */
#include<graphics.h>
#include<conio.h>
int main()
{
int gd = DETECT, gm, midx, midy;
setcolor(MAGENTA);
rectangle(0,40,639,450);
settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
setcolor(WHITE);
outtextxy(275,10,"Pie Chart");
midx = getmaxx()/2;
midy = getmaxy()/2;
setfillstyle(LINE_FILL,BLUE);
pieslice(midx, midy, 0, 75, 100);
outtextxy(midx+100, midy - 75, "20.83%");
setfillstyle(XHATCH_FILL,RED);
pieslice(midx, midy, 75, 225, 100);
outtextxy(midx-175, midy - 75, "41.67%");
setfillstyle(WIDE_DOT_FILL,GREEN);
pieslice(midx, midy, 225, 360, 100);
outtextxy(midx+75, midy + 75, "37.50%");
getch();
return 0;
}
C programming code
#include <graphics.h>
#include <conio.h>
int main()
{
int gd = DETECT, gm;
setcolor(YELLOW);
rectangle(0,30,639,450);
settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
setcolor(WHITE);
outtextxy(275,0,"Bar Chart");
setlinestyle(SOLID_LINE,0,2);
line(100,420,100,60);
line(100,420,600,420);
line(90,70,100,60);
line(110,70,100,60);
line(590,410,600,420);
line(590,430,600,420);
outtextxy(95,35,"Y");
outtextxy(610,405,"X");
outtextxy(85,415,"O");
setfillstyle(LINE_FILL,BLUE);
bar(150,100,200,419);
setfillstyle(XHATCH_FILL,RED);
bar(225,150,275,419);
setfillstyle(WIDE_DOT_FILL,GREEN);
bar(300,200,350,419);
setfillstyle(INTERLEAVE_FILL,MAGENTA);
bar(375,125,425,419);
setfillstyle(HATCH_FILL,BROWN);
bar(450,175,500,419);
getch();
return 0;
}
C smiling face animation
This animation using c draws a smiling face which appears at random position on screen. See
output below the code, it will help you in understanding the code easily.
C programming code
#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
main()
{
int gd = DETECT, gm, area, temp1, temp2, left = 25, top = 75;
void *p;
initgraph(&gd,&gm,"C:\\TC\\BGI");
setcolor(YELLOW);
circle(50,100,25);
setfillstyle(SOLID_FILL,YELLOW);
floodfill(50,100,YELLOW);
setcolor(BLACK);
setfillstyle(SOLID_FILL,BLACK);
fillellipse(44,85,2,6);
fillellipse(56,85,2,6);
ellipse(50,100,205,335,20,9);
ellipse(50,100,205,335,20,10);
ellipse(50,100,205,335,20,11);
setcolor(WHITE);
settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
outtextxy(155,451,"Smiling Face Animation");
setcolor(BLUE);
rectangle(0,0,639,449);
while(!kbhit())
{
temp1 = 1 + random ( 588 );
temp2 = 1 + random ( 380 );
getch();
closegraph();
return 0;
}
C programming code
#include<graphics.h>
#include<conio.h>
#include<dos.h>
main()
{
int gd = DETECT, gm, x, y, color, angle = 0;
struct arccoordstype a, b;
while(angle<=360)
{
setcolor(BLACK);
arc(getmaxx()/2,getmaxy()/2,angle,angle+2,100);
setcolor(RED);
getarccoords(&a);
circle(a.xstart,a.ystart,25);
setcolor(BLACK);
arc(getmaxx()/2,getmaxy()/2,angle,angle+2,150);
getarccoords(&a);
setcolor(GREEN);
circle(a.xstart,a.ystart,25);
angle = angle+5;
delay(50);
}
getch();
closegraph();
return 0;
}
C program countdown
This c graphics program performs countdown for 30 seconds.
int main()
{
int gd = DETECT, gm, i;
char a[5];
if ( i == 0 )
break;
cleardevice();
}
getch();
closegraph();
return 0;
}