Graphics Mode Initialization

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

C graphics

Graphics mode Initialization


The first step in any graphics program is to initialize the graphics drivers on the computer
using initgraph() method present in graphics.h header file.
It initializes the graphics system by loading the passed graphics driver
then changing the system into graphics mode. It also resets or initializes
all graphics settings like color, palette, current position etc, to their
default values

void initgraph(int *graphicsDriver, int *graphicsMode, char *driverDirectoryPath);


graphicsDriver :
 It is a pointer to an integer specifying the graphics driver to be used.
 It tells the compiler that which graphics driver to be use or automatically
detect the drive.
 DETECT is a macro of graphics.h library that instruct compiler for auto
detection of graphics driver.

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;

initgraph(&gd, &gm, "C:\\TC\\BGI");

line(100, 100, 200, 100);

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

upattern: It should be 0 or any non-zero value consider for


SOLID_LINE,DOTTED_LINE ,CENTER_LINE and DASHED_LINE but
It must be any Non-zero value for USERBIT_LINE ,
for 0 USERBIT_LINE line not display

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++)
{

setlinestyle(i,1,3); /* i: for line pattern 1: for USERBIT_LINE display 3: for THICK_WIDTH */


line(x,y,x+150,y);

y=y+20;
}
getch();
closegraph();
return 0;
}

Output:
Circle

circle() is use to draw circle. 

syntax
void circle(int xcenter, int ycenter, int radius);
Example
#include <graphics.h>
#include <conio.h>

int main()
{
int gd = DETECT, gm;

initgraph(&gd, &gm, "C:\\TC\\BGI");

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);

Example of arc() function :


#include <graphics.h>
#include <conio.h>

int main()
{
int gd = DETECT, gm;

initgraph(&gd, &gm, "C:\\TC\\BGI");

arc(100, 100, 0, 180, 50);

getch();
closegraph();
return 0;
}

Output:

  C Graphics Code for bar : 

syntax
void bar(int left, int top, int right, int bottom);

Example of bar() function :


#include <graphics.h>
#include <conio.h>

int main()
{
int gd = DETECT, gm;

initgraph(&gd, &gm, "C:\\TC\\BGI");

bar(100, 100, 100+50, 100+50);

getch();
closegraph();
return 0;
}

Output:

  C Graphics Code for ellipse : 

syntax
void ellipse(int x, int y, int stangle, int endangle, int xradius, int yradius);

Example of ellipse() function :


#include <graphics.h>
#include <conio.h>

int main()
{
int gd = DETECT, gm;

initgraph(&gd, &gm, "C:\\TC\\BGI");


cleardevice();
ellipse(100, 200, 0, 360, 50, 30);
getch();
closegraph();
return 0;
}

Output:

  C Graphics Code for rectangle : 

syntax
void rectangle(int left, int top, int right, int bottom);

Example of rectangle() function :


#include <graphics.h>
#include <conio.h>

int main()
{
int gd = DETECT, gm;

initgraph(&gd, &gm, "C:\\TC\\BGI");


cleardevice();
rectangle(100, 100, 100+100, 100+50);

getch();
closegraph();
return 0;
}
Output:

  C Graphics Code for drawpoly : 

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};

Example of drawpoly() function :


#include <graphics.h>
#include <conio.h>

int main()
{
int gd = DETECT, gm;
int points[]={100,100, 200,100, 150,50, 100,100};

initgraph(&gd, &gm, "C:\\TC\\BGI");


cleardevice();
drawpoly(4,points); /* to draw traingle no of point is 3 so 3+1=4*/

getch();
closegraph();
return 0;
}

Output:

  C Graphics Code for fillpoly : 

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};

Example of fillpoly() function :


#include <graphics.h>
#include <conio.h>

int main()
{
int gd = DETECT, gm;
int points[]={100,100, 200,100, 150,50, 100,100};

initgraph(&gd, &gm, "C:\\TC\\BGI");


cleardevice();
fillpoly(4,points); /* to draw traingle no of point is 3 so 3+1=4*/
getch();
closegraph();
return 0;
}

Output:

  C Graphics Code for getmaxx() and getmaxy() : 

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();

Example of getmaxx() & getmaxy() function :


#include <graphics.h>
#include <conio.h>

int main()
{
int gd = DETECT, gm;
int x,y;

initgraph(&gd, &gm, "C:\\TC\\BGI");


cleardevice();

x=getmaxx(); /* x=max X coordinate */


y=getmaxy(); /* y=max Y coordinate */
circle(x/2,y/2,50); /* x/2 is center of x axis and y/2 is center of y axis */

getch();
closegraph();
return 0;
}

Output:

  C Graphics Code for setcolor() : 

setcolor() function is use to set drawing color.

syntax
void setcolor(int color);

Example of setcolor() function :


#include <graphics.h>
#include <conio.h>

int main()
{
int gd = DETECT, gm;
int x=50,y=50,i;

initgraph(&gd, &gm, "");


cleardevice();
for(i=0;i<=4;i++)
{
setcolor(i+1);
setlinestyle(i,1,3); /* i: for line pattern 1: for USERBIT_LINE display 3: for THICK_WIDTH */
line(x,y,x+150,y);

y=y+20;
}
getch();
closegraph();
return 0;
}

Output:

  C Graphics Code for floodfill() : 

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);

Example of floodfill() function :


#include <graphics.h>
#include <conio.h>

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:

  C Graphics Code for setfillstyle() : 

setfillstyle() function sets the current fill pattern and fill color syntax is given below.

syntax
void setfillstyle(int pattern, int color);

Below is the table showing INTEGER VALUES corresponding to Patterns :


Example of setfillstyle() function :
#include <graphics.h>
#include <conio.h>

int main()
{
int gd = DETECT, gm;

initgraph(&gd, &gm, "C:\\TC\\BGI");


cleardevice();

setfillstyle(HATCH_FILL ,RED);
circle(100,100,50);
floodfill(100,100,WHITE);

getch();
closegraph();
return 0;
}

Output:

You might also like