Graphics Mode Initialization
Graphics Mode Initialization
Graphics Mode Initialization
graphicsMode :
It is a pointer to an integer specifies the graphics mode to be used.
If *graphdriver = DETECT, initgraph sets *graphmode to the highest resolution
availablefor the detected driver.
You can give it a value using a constant of the graphics_drivers enumeration type,
which is defined in graphics.h and listed below.
driverDirectoryPath :
It specifies the path of graphics driver files i.e. (BGI files) .
If directory path is not provided, then it will search in current working directory
directory i.e. "c://tc//BGI" or other working directory
you can change path of BGI directory accordingly your Turbo C++ compiler is
installed.
Colors in C Graphics
There are 16 colors declared in C Graphics. We use colors to set drawing color, background
color, text color, or to color a shape etc.
To specify a color, we can either use color constants like setcolor() method i.e setcolor(RED),
or their corresponding integer codes like setcolor(4).
Below is the color code constant in increasing order are listed below.
Line
line() is use to draw line from 1 coordinate to another.
syntax
void line(int x1, int y1, int x2, int y2);
Example
#include <graphics.h>
#include <conio.h>
int main()
{
int gd = DETECT, gm;
getch();
closegraph();
return 0;
}
Output:
setlinestyle() function is use to draw line of different style. syntax are given below
syntax
void setlinestyle(int linestyle, unsigned upattern, int thickness);
where,
linestyle: defines the different types of style given below
thickness: thickness define the width of lines can be normal or thick given below.
setlinestyle() function :
#include <graphics.h>
#include <conio.h>
int main()
{
int gd = DETECT, gm;
int x=50,y=50,i;
initgraph(&gd, &gm, "C:\\TC\\BGI");
cleardevice();
for(i=0;i<=4;i++)
{
y=y+20;
}
getch();
closegraph();
return 0;
}
Output:
Circle
syntax
void circle(int xcenter, int ycenter, int radius);
Example
#include <graphics.h>
#include <conio.h>
int main()
{
int gd = DETECT, gm;
circle(100, 100,50);
getch();
closegraph();
return 0;
}
Output:
arc
syntax
void arc(int x, int y, int start-angle, int end-angle, int radius);
int main()
{
int gd = DETECT, gm;
getch();
closegraph();
return 0;
}
Output:
syntax
void bar(int left, int top, int right, int bottom);
int main()
{
int gd = DETECT, gm;
getch();
closegraph();
return 0;
}
Output:
syntax
void ellipse(int x, int y, int stangle, int endangle, int xradius, int yradius);
int main()
{
int gd = DETECT, gm;
Output:
syntax
void rectangle(int left, int top, int right, int bottom);
int main()
{
int gd = DETECT, gm;
getch();
closegraph();
return 0;
}
Output:
Drawpoly function is used to draw polygons i.e. triangle, rectangle, pentagon, hexagon etc.
syntax
void drawpoly( int num, int *polypoints );
where,
num is total number of point+1 eg. traingle contain 3 point so num=3+1
polypoints is an array[] eg. int polypoints[] = {100,100, 200,100, 150,50, 100,100};
int main()
{
int gd = DETECT, gm;
int points[]={100,100, 200,100, 150,50, 100,100};
getch();
closegraph();
return 0;
}
Output:
fillpoly() function is used to draw polygons i.e. triangle, rectangle, pentagon, hexagon etc
with white filled colored.
syntax
void fillpoly( int num, int *polypoints );
where,
num is total number of point+1 eg. traingle contain 3 point so num=3+1
polypoints is an array[] eg. int polypoints[] = {100,100, 200,100, 150,50, 100,100};
int main()
{
int gd = DETECT, gm;
int points[]={100,100, 200,100, 150,50, 100,100};
Output:
getmaxx(): return the the maximum X coordinate for current graphics mode and driver.
getmaxy(): return the the maximum Y coordinate for current graphics mode and driver.
syntax
int getmaxx();
int getmaxy();
int main()
{
int gd = DETECT, gm;
int x,y;
getch();
closegraph();
return 0;
}
Output:
syntax
void setcolor(int color);
int main()
{
int gd = DETECT, gm;
int x=50,y=50,i;
y=y+20;
}
getch();
closegraph();
return 0;
}
Output:
floodfill function is used to fill an enclosed area eg rectangle,circle, polygon etc. (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 setfillstyle()
function is use.
syntax
void floodfill(int x, int y, int border);
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
cleardevice();
circle(100,100,50);
floodfill(100,100,WHITE);/* where 100,100 is point inside the circle WHITE is boundry color
of circle */
getch();
closegraph();
return 0;
}
Output:
setfillstyle() function sets the current fill pattern and fill color syntax is given below.
syntax
void setfillstyle(int pattern, int color);
int main()
{
int gd = DETECT, gm;
setfillstyle(HATCH_FILL ,RED);
circle(100,100,50);
floodfill(100,100,WHITE);
getch();
closegraph();
return 0;
}
Output: