You are on page 1of 19

Basic program of Graphics

• #include<graphics.h>
#include<conio.h>
• Void main()
{
int gd = DETECT, gm;
• initgraph(&gd, &gm, "C:\\TC\\BGI");
• line(20,30,50,60);

• getch();
closegraph();
}
Graphics Modes
• Text mode functions of graphics.
• Graphics mode functions of graphics.
Text Mode Functions
1.putch():It displays a single character which is specified in function.
syntax: putch(char);
2.puts():This function displays string at cursor position. Syntax: puts(“hello”);
3.clrscr():This function is used to clear the screen and after clearing screen cursor on
top left corner.
4.window():This function is used to create window on screen. Syntax: window(x,y,z,w).
5.gotoxy():It position the cursor at x,y co-ordinate position. Syntax:goto(x,y)
where x and y are co-ordinate point.
6.delline():It deletes a line specified at cursor position in function.
Syntax:gotoxy(x,y).
7.inline():It insert the blank line at specified cursor position in function.
Inline();
8.textbackground():It changes textbackground color of text.
textbackground(color).
Continue…
9. moveto(): It moves the cursor to the position which
is specified in function.
Syntax: moveto(x,y);
10. textcolor():This function is used to set color of text.
Graphics Mode Functions
1.initgraph(): It is used to initialize graphics mode.
Syntax: void initgraph(int *graphicsDriver, int *graphicsMode, char *driverDirectoryPath);
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. Below is the description of input parameters of initgraph function.

graphicsDriver : It is a pointer to an integer specifying the graphics driver to be used. It tells the
compiler that what graphics driver to use or to automatically detect the drive. In all our programs we
will use DETECT macro of graphics.h library that instruct compiler for auto detection of graphics driver.

graphicsMode : It is a pointer to an integer that specifies the graphics mode to be used. If *gdriver is set
to DETECT, then initgraph sets *gmode to the highest resolution available for the detected driver.

driverDirectoryPath : It specifies the directory path where graphics driver files (BGI files) are located. If
directory path is not provided, then it will search for driver files in current working directory directory.
In all our sample graphics programs, you have to change path of BGI directory accordingly where you
Turbo C++ compiler is installed.
syntax: initgraph(&gd,&gm,”c:\\tc\\bgi”);
int gm,gd=DETECT
Where gd=graphics driver , gm=graphics mode ,path=“ ”, DETECT is macro which automatically
selects the driver.
2.Closegraph():It shut down graphics mode and return
to position it was before the initgraph function was
called.
Syntax: closegraph();
3.line():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
the start and end points of the line.
Syntax: line(int x1,int y1, int x2,int y2);
4. circle() function :which draws a circle with center at
(x, y) and given radius. Syntax : circle(x, y, radius);
where, (x, y) is center of the circle. 'radius' is the Radius
of the circle.
Primitive Operations of Graphics
• Move To(x,y):It sets current beam position to
point(x,y).
• Line to(x,y): It draws a straight line from
current beam position to point (x,y).
• Draw text(s):It displays the string s with its
lower left corner at the current beam position
and reset current beam position to the string’s
lower right corner.
1.The arc() function is used to create an arc. This arc
function is included in graphics.h library in C which
contains methods that can draw figures on the output
screen.
• arc(x, y, start_angle, end_angle, radius);
2.putpixel() function in C
• The header file graphics.h
contains putpixel() function which plots a
pixel at location (x, y) of specified color.
Syntax :
• void putpixel(int x, int y, int color); where, (x,
y) is the location at which pixel is to be put ,
and color specifies the color of the pixel.
• Draw Rectangle in C graphics
• rectangle() is used to draw a rectangle.
Coordinates of left top and right bottom corner
are required to draw the rectangle. 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.
Syntax :
• rectangle(int left, int top, int right, int bottom);
• #include <graphics.h>
• #include <conio.h>
• void main ()
• {
• rectangle (200,200,300,300);
• getch ();
• }
• bar() function
• The header file graphics.h contains bar() function
which is used to draw a 2-dimensional,
rectangular filled in bar.
Syntax :
• void bar(int left, int top, int right, int bottom);
where, 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.
Draw ellipse in C graphics
• void ellipse(int x, int y, int start_angle, int end_angle, int x_radius, int
y_radius) draw :-
• #include<graphics.h>
• main()
{
int gd = DETECT, gm;
• initgraph(&gd, &gm, "C:\\TC\\BGI");
• ellipse(100, 100, 0, 360, 50, 25);
• getch();
closegraph();
return 0;
}
• Fill”
• #include<graphics.h>
• main()
{
int gd = DETECT, gm;
• initgraph(&gd, &gm, "C:\\TC\\BGI");

fillellipse(100, 100, 50, 25);


• getch();
closegraph(); return 0;}
drawpoly() function in C

• contains drawpoly() function which is used to


draw polygons i.e. triangle, rectangle,
pentagon, hexagon etc.
• void drawpoly( int number, int *polypoints );
where, number 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.
Program for draw poly
• C program for drawpoly
• #include <graphics.h>
#include <conio.h>
• main()
{
int gd=DETECT,gm,points[]={320,150,420,300,250,300,32
0,150};
• initgraph(&gd, &gm, "C:\\TC\\BGI");
• drawpoly(4, points);
• getch();
closegraph();
return 0;
}
Basic Function for drawing Various
Object
• #include<graphics.h>
• #include<conio.h>
• void main()
• {
• intgd=DETECT,gm;
• initgraph (&gd,&gm,"c:\\tc\\bgi");
• setbkcolor(GREEN);
• printf("\t\t\t\n\nLINE");
• line(50,40,190,40);
• printf("\t\t\n\n\n\nRECTANGLE");
• rectangle(125,115,215,165);
• printf("\t\t\t\n\n\n\n\n\n\nARC");
• arc(120,200,180,0,30);
• printf("\t\n\n\n\nCIRCLE");
• circle(120,270,30);
• printf("\t\n\n\n\nECLIPSE");
• ellipse(120,350,0,360,30,20);
• getch();
• }
Display File Interpreter
• A display list (or display file) is a series
of graphics commands that define an output
image.
Continue…
Display File Structure
1.The file used to store the commands necessary for drawing the
line segment is called as display file.
2.Every command of the display file consists of two parts:
operation code(OP CODE) & operands.
3.Operation Code Indicates Which Type Of The Commands It
Is,i.e.either LINE or MOVE.
4.Operands are co-ordinate sub points( x, y).
5.Three separate arrays are used for string these instructions
which are as below: a)one array for the operation
code,i.e.,DF_OP. b)second array for x-co-ordinate, i.e. DF-X.
c) Third array for x-co-ordinate,i.e. DF-Y.
Commands with opcode

Command Opcode

MOVE 1

LINE 2

PLOT 3

You might also like