Tutorial references

c
c++
c#

Tutorial references

chapters

Graphics in C Programming

graphics.h

First include graphics.h header file in your program.

Driver of graphics

Any graphics program first initialize Driver in your computer.

First graphics program uses initgraph() function of graphics.h header file.

syntax:

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

graphicsDriver

It tells, what driver used by compiler automatically OR what deriver detect by compiler.

It uses integer type pointer variable.

Use DETECT macro of graphics.h header file in all graphics program.

graphicsMode

It uses integer type pointer variable.

If *graphicsDriver set DETECT then initgraph() function sets *graphicsMode for highest screen resolution for detected driver.

driverDirectoryPath

It uses path of Graphics Driver Files(BGI files).

NOTE: BGI file path can be change according to C compiler.

closegraph()

It uses in end of graphics program.

When you call closegraph() then it unloads the graphics drivers and sets the screen in text mode.

Example

#include <graphics.h>
#include <conio.h>
int main(){
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");

/* code here */

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

NOTE: do not use clrscr() function in graphics program. use cleardevice() instead of clrscr().


Screen

First see and understand bellow screen.

The origin point O(0,0) is the top left corner of your output screen.

In screen X axis in right, -X axis in left, Y axis in bottom and -Y axis in top.


Colors

There are 16 colors in C graphics.

COLOR MACRO VALUE
BLACK 0
BLUE 1
GREEN 2
CYAN 3
RED 4
MAGENTA 5
BROWN 6
LIGHTGRAY 7
DARKGRAY 8
LIGHTBLUE 9
LIGHTGREEN 10
LIGHTCYAN 11
LIGHTRED 12
LIGHTMAGENTA 13
YELLOW 14
WHITE 15

line()

It is use to draw a line from point p1(x1,y1) to point p2(x2,y2).

syntax:

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:\\TURBOC3\\BGI");
line(150,150,250,250);
getch();
closegraph();
return 0;
}


arc()

arc() function used to draw an arc

syntax:

arc(int x,int y, int startAngle, int endAngle,int radius);

center of arc is center(x,y).

start angle and end angle should be in positive. Angle 0 to 360.

arc start from int startAngle point and made in anticlockwise and end on int endAngle point

Example

#include <graphics.h>
#include <conio.h>
int main(){
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
arc(100,100,0,90,80);
getch();
closegraph();
return 0;
}


bar()

It is use to draw 2-dimensional field.

Syntax:

bar(int x1, int y1, int x2, int y2);

This function required two coordinates.first coordinate is top left and second coordinate is right bottom corner.

first coordinate made by p1(x1,y1).

second coordinate made by p2(x2,y2).

Example

#include <graphics.h>
#include <conio.h>
int main(){
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
bar(10,20,60,250);
getch();
closegraph();
return 0;
}


bar3d()

It is use to draw 3-dimensional rectangular field.

Syntax:

bar3d(int x1, int y1, int x2, int y2, int depth, int topflag);

This function required two coordinates.first coordinate is top left and second coordinate is right bottom corner.

first coordinate made by p1(x1,y1).

second coordinate made by p2(x2,y2).

int depth used for depth of bar in pixels

int topflag determines 3-D. int topflag > 0.

Example

#include <graphics.h>
#include <conio.h>
int main(){
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
bar3d(100,100,300,300,60,1);
getch();
closegraph();
return 0;
}


circle()

It is used to draw a circle.

Syntax:

circle(int x, int y, int radius);

int x and int y create a coordinate c(x,y) which is used for center of circle.

int radius is used for radius of circle.

Example

#include <graphics.h>
#include <conio.h>
int main(){
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
circle(150,150,100);
getch();
closegraph();
return 0;
}


setcolor()

It use use to set boundary color.

syntax:

setcolor(colorMacro);

Example

#include <graphics.h>
#include <conio.h>
int main(){
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
setcolor(BLUE); circle(150,150,100);
getch();
closegraph();
return 0;
}


drawpoly()

It is use to draw polygons.

Syntax:

drawpoly(int num, int pointsArray );

num indicates (n+1) number of points where n is the number of vertices(sides) of polygon.Each point has p(x,y) coordinate.

We specify the last point( means n+1 point) is the same as first point of polygon.

now you can understand from bellow. We will draw a rectangle using drawpoly function.

int points[] = {100,150,400,150,400,350,100,350,100,150};

points contains x,y from array. p1(100,150), p2(400,150), p3(400,350), p4(100,350), p5(100,150)

four points are required in rectangle but in array five points are given. You can see last point p5 is same as first point p1. If first point and last point is not same then you can not create polygon.

Example

#include <graphics.h>
#include <conio.h>
int main(){
int gd = DETECT, gm;
int points[] = {100,150,400,150,400,350,100,350,100,150};
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
drowpoly(5,points);
getch();
closegraph();
return 0;
}


ellipse()

It is use to drow an ellipse.

syntax:

ellipse(int x, int y, int startAngle, int endAngle, int xRadius, int yRadius);

first and second parameter is use to calculate center of ellipse,third and fourth parameter is use for start angle and end angle respectively as like arc() function.fifth and sixth parameter specifies the X and Y radius respectively.

Example

#include <graphics.h>
#include <conio.h>
int main(){
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
ellipse(150,150,0,360,80,40);
getch();
closegraph();
return 0;
}


fillellipse()

syntax:

fillellipse(int x, int y, int xRadius, int yRadius);

int x and int y is the center coordinate of ellipse and xRadius and yRadius is the radius of X and Y respectively.

Example

#include <graphics.h>
#include <conio.h>
int main(){
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
fillellipse(150,150,80,40);
getch();
closegraph();
return 0;
}


fillpoly()

syntax:

fillpoly(int num, int pointsArray );

It drow a polygon and fill it.

It flow syntax rule same as drawpoly().

Example

#include <graphics.h>
#include <conio.h>
int main(){
int gd = DETECT, gm;
int points[] = {100, 150, 400, 150, 400, 350, 100, 350, 100, 150};
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
fillpoly(5,points);
getch();
closegraph();
return 0;
}


About Us

This website(www.sabbla.com) is very useful for beginners and professional developers. Sabbla provides tutorial in simple language. It provides latest technology tutorial which very useful for developers.

See More...

Contact Us

+91 9971366710

+91 9717983128

info@sabbla.com

Buxar, Bihar, India, Pin No.- 802131

Services

Sabbla is providing free latest technology tutorials. All tutorials are free for beginner and professionals.


Terms of Use

sabbla.com is not responsible for any mistake. Users can inform to sabbla.com for any mistake. But sabbla.com always try for 0-0 mistake.

Privacy policy

Currently sabbla.com does not collect any data from users.

                   

copyright © 2017-2018